TCP(TransmissionControl Protocol)传输控制协议。
是一种可靠的、面向连接的协议(eg:打电话)、传输效率低全双工通信(发送缓存&接收缓存)、面向字节流。使用TCP的应用:Web浏览器;电子邮件、文件传输程序。

TCP编程的服务器端一般步骤是:
1、创建一个socket,用函数socket()。
2、设置socket属性。
3、绑定本机的IP地址、端口等信息到socket上,用函数bind()。
4、开启监听,用函数listen()。
5、接收客户端上来的连接,用函数accept()。
6、通过accept()返回相应客户端的socket建立专用的通信通道。
7、收发数据,用函数send()和recv(),或者read()和write()。
8、关闭网络连接。
9、关闭监听。
TCP编程的客户端一般步骤是:
1、创建一个socket,用函数socket()。
2、设置socket属性。
3、设置要连接的对方的IP地址和端口等属性。
4、连接服务器,用函数connect()。
5、收发数据,用函数send()和recv(),或者read()和write()。
6、关闭网络连接。
---------------------
作者:subin_iecas
来源:CSDN
原文:https://blog.csdn.net/subin_iecas/article/details/80289513
下面是一个简单的TCP的通信程序源码
主要功能是模拟一个群聊,当多个客户端连接进来后,任意一个客户端发送的消息都将被服务器端接收并群发给所有客户端
服务器端:

1 1 using System; 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 4 using System.Text; 5 5 using System.Threading.Tasks; 6 6 using System.Net; 7 7 using System.Net.Sockets; 8 8 using System.Threading; 9 9 1010 namespace _037_test 1111 { 1212 class Program 1313 { 1414 static void Main(string[] args) 1515 { 1616 Server s = new Server("192.168.0.105", 8888); 1717 s.Start(); 1818 } 1919 } 2020 }
Program.CS

1 1 using System; 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 4 using System.Text; 5 5 using System.Threading.Tasks; 6 6 using System.Net; 7 7 using System.Net.Sockets; 8 8 using System.Threading; 9 9 10 10 namespace _037_test 11 11 { 12 12 class Server 13 13 { 14 14 Socket serverSock; 15 15 List<Socket> clientList = new List<Socket>(); 16 16 public Server(string ip, int port) 17 17 { 18 18 //创建socket套接字 19 19 serverSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 20 20 //检测ip地址是否有误 21 21 IPAddress ipAdd; 22 22 if (!IPAddress.TryParse(ip, out ipAdd)) 23 23 { 24 24 Console.WriteLine("ip有误,服务器创建失败"); 25 25 return; 26 26 } 27 27 //绑定ip地址和端口; 28 28 IPEndPoint point = new IPEndPoint(ipAdd, port); 29 29 serverSock.Bind(point); 30 30 //无限监听接收 31 31 serverSock.Listen(0); 32 32 33 33 } 34 34 35 35 public void Start() 36 36 { 37 37 Thread th = new Thread(AcceptClient); 38 38 th.Start(); 39 39 SendToAll(); 40 40 } 41 41 42 42 public void AcceptClient() 43 43 { 44 44 Console.WriteLine("等待连接"); 45 45 while (true) 46 46 { 47 47 Socket client = serverSock.Accept();//等待接收用户的连接,并返回连接成功的用户 48 48 IPEndPoint clientPoint = client.RemoteEndPoint as IPEndPoint;//获取所连接的用户的ip 49 49 Console.WriteLine(clientPoint.Address+"已连接"); 50 50 clientList.Add(client); 51 51 Thread clientT = new Thread(ReceiveMessage); 52 52 clientT.Start(client); 53 53 } 54 54 } 55 55 56 56 public void SendToAll() 57 57 { 58 58 while (true) 59 59 { 60 60 string message = "管理员:"+Console.ReadLine(); 61 61 byte[] messageBytes = Encoding.Default.GetBytes(message); 62 62 for (int i = 0; i < clientList.Count; i++) 63 63 { 64 64 try 65 65 { 66 66 clientList[i].Send(messageBytes); 67 67 } 68 68 catch (SocketException) 69 69 { 70 70 Console.WriteLine("有一个客户端下线"); 71 71 clientList.RemoveAt(i); 72 72 i--; 73 73 } 74 74 catch (Exception e) 75 75 { 76 76 Console.WriteLine(e.Message); 77 77 } 78 78 } 79 79 } 80 80 } 81 81 82 82 public void ReceiveMessage(object obj) 83 83 { 84 84 while (true) 85 85 { 86 86 Socket client = obj as Socket;//将线程的start方法传入的obj转回socket类型 87 87 byte[] messageBytes = new byte[100 * 1024];//数据容器 88 88 try 89 89 { 90 90 int num = client.Receive(messageBytes);//receive方法返回字节长度,并把内容存在传入的数组中 91 91 IPEndPoint clientPoint = client.RemoteEndPoint as IPEndPoint;//获取所连接的用户的ip 92 92 Console.WriteLine(clientPoint.Address+":" + Encoding.Default.GetString(messageBytes, 0, num)); 93 93 94 94 //服务器端负责将从客户端收到的消息转发给所有客户端 95 95 string message = clientPoint.Address + ":" + Encoding.Default.GetString(messageBytes, 0, num); 96 96 foreach (var item in clientList) 97 97 { 98 98 if (item!=client) 99 99 { 100100 item.Send(Encoding.Default.GetBytes(message)); 101101 } 102102 } 103103 } 104104 catch (Exception) 105105 { 106106 Console.WriteLine("有一个客户端离开"); 107107 clientList.Remove(client); 108108 break; 109109 } 110110 } 111111 112112 } 113113 } 114114 }
Server.CS
客户端:

1 1 using System; 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 4 using System.Text; 5 5 using System.Threading.Tasks; 6 6 using System.Net.Sockets; 7 7 using System.Net; 8 8 using System.Threading; 9 9 1010 namespace _038_test 1111 { 1212 class Program 1313 { 1414 static void Main(string[] args) 1515 { 1616 Client c = new Client("192.168.0.105", 8888); 1717 c.Start(); 1818 } 1919 } 2020 }
Program.CS

1 1 using System; 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 4 using System.Text; 5 5 using System.Threading.Tasks; 6 6 using System.Net.Sockets; 7 7 using System.Net; 8 8 using System.Threading; 9 9 1010 namespace _038_test 1111 { 1212 class Client 1313 { 1414 Socket clientSock; 1515 public Client(string ip, int port) 1616 { 1717 clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 1818 try 1919 { 2020 IPEndPoint point = new IPEndPoint(IPAddress.Parse(ip), port); 2121 clientSock.Connect(point); 2222 } 2323 catch (Exception) 2424 { 2525 Console.WriteLine("服务器没有开启"); 2626 return; 2727 } 2828 2929 Console.WriteLine("已连接服务器"); 3030 } 3131 3232 public void Start() 3333 { 3434 Thread th = new Thread(SendMessage); 3535 th.Start(); 3636 ReceiveMessage(); 3737 } 3838 3939 public void SendMessage() 4040 { 4141 try 4242 { 4343 while (true) 4444 { 4545 string message = Console.ReadLine(); 4646 byte[] messageBytes = Encoding.Default.GetBytes(message); 4747 clientSock.Send(messageBytes); 4848 } 4949 } 5050 catch (Exception) 5151 { 5252 Console.WriteLine("服务器断开"); 5353 return; 5454 } 5555 5656 } 5757 5858 public void ReceiveMessage() 5959 { 6060 try 6161 { 6262 while (true) 6363 { 6464 byte[] messageBytes = new byte[100 * 1024]; 6565 int num = clientSock.Receive(messageBytes); 6666 Console.WriteLine(Encoding.Default.GetString(messageBytes, 0, num)); 6767 } 6868 } 6969 catch (Exception) 7070 { 7171 Console.WriteLine("服务器断开"); 7272 return; 7373 } 7474 } 7575 7676 } 7777 }
Client.CS
运行结果:

