⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multithreadclient.cs

📁 visual c# 网络编程技术与实践实例包括几个源码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace threadtcpclient
{
    class multithreadclient
    {
        static void Main(string[] args)
        {
            Socket client;
            byte[] buf = new byte[1024];
            string input;
            IPAddress local = IPAddress.Parse("127.0.0.1");
            IPEndPoint iep = new IPEndPoint(local, 13000);
            try
            {
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(iep);
            }
            catch (SocketException)
            {
                Console.WriteLine("无法连接到服务器!");
                return;
            }
            while (true)
            {
                //在控制台上输入一条消息
                input = Console.ReadLine();
                //输入exit,可以断开与服务器的连接
                if (input == "exit")
                {
                    break;
                }
                client.Send(Encoding.ASCII.GetBytes(input));
                //得到实际收到的字节总数
                int rec = client.Receive(buf);
                Console.WriteLine(Encoding.ASCII.GetString(buf, 0, rec));
            }
            Console.WriteLine("断开与服务器的连接......");
            client.Close();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -