Socket编程服务器和客户端(多个客户端可以同时连接一个服务器的同一端口)

来源:互联网 发布:矩阵齐次分解 编辑:程序博客网 时间:2024/05/05 11:15

服务器端代码

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Net;  
  5. using System.Net.Sockets;  
  6. using System.Threading;  
  7. namespace TCP通信  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             try  
  14.             {  
  15.                 //把ip地址转换为实例  
  16.                 IPAddress ipa = IPAddress.Parse("127.0.0.1");  
  17.                 //监听端口8001  
  18.                 TcpListener mylsn = new TcpListener(ipa, 8001);  
  19.                 //开启监听  
  20.                 mylsn.Start();  
  21.                 //输出监听成功的信息  
  22.                 Console.WriteLine("在8001启动服务,等待连接");  
  23.                 //等待处理接入连接请求  
  24.                 while (true)  
  25.                 {  
  26.                     Socket mysock = mylsn.AcceptSocket();  
  27.                     Console.WriteLine("有连接,连接来自" + mysock.RemoteEndPoint);  
  28.                     work w = new work();  
  29.                     w.mysock = mysock;  
  30.                     w.mylsn = mylsn;  
  31.                     Thread t = new Thread(new ThreadStart(w.main));  
  32.                     t.Start();    
  33.                 }  
  34.             }  
  35.             catch  
  36.             { }  
  37.             finally  
  38.             { }  
  39.         }  
  40.     }  
  41.     public class work  
  42.     {  
  43.         public Socket mysock;  
  44.         public TcpListener mylsn;  
  45.         public void main()  
  46.         {  
  47.             //接收客户端消息  
  48.             byte[] data = new byte[100];  
  49.             mysock.Receive(data);  
  50.             string rt = System.Text.UTF8Encoding.UTF8.GetString(data);  
  51.             Console.WriteLine(rt);  
  52.             //给客户端发消息  
  53.             mysock.Send(UTF8Encoding.UTF8.GetBytes("回发给客户端"));  
  54.             //释放资源  
  55.             mysock.Close();  
  56.             mylsn.Stop();  
  57.         }  
  58.     }  
  59. }  
 

客户端代码

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.IO;  
  5. using System.Net;  
  6. using System.Net.Sockets;  
  7. namespace TCP通信客户端  
  8. {  
  9.     public class Class1  
  10.     {  
  11.         public static void Main()  
  12.         {   
  13.             //新建客户端套接字  
  14.             TcpClient tclient = new TcpClient();  
  15.             //连接服务器  
  16.             tclient.Connect("127.0.0.1", 8001);  
  17.             Console.WriteLine("输入要发送的消息");  
  18.             //读入要传输的字符  
  19.             string str = Console.ReadLine();  
  20.               
  21.             //得到流  
  22.             Stream stm = tclient.GetStream();  
  23.             //发送字符串  
  24.             ASCIIEncoding asen = new ASCIIEncoding();  
  25.             byte[] data = asen.GetBytes(str);  
  26.             stm.Write(data, 0, data.Length);  
  27.             //接受服务器返回的消息  
  28.             byte[] back = new byte[100];  
  29.             int k = stm.Read(back, 0, 100);  
  30.             //输出服务器返回的消息  
  31.             for (int i = 0; i < k; i++)  
  32.             {  
  33.                 Console.Write(Convert.ToChar(back[i]));  
  34.             }  
  35.             //关闭连接  
  36.             tclient.Close();  
  37.         }  
  38.     }  
  39. }  
 

根据TCP/IP设计者的设计初衷,当mysock.Close()以后用命令netstat -a -n查看本地连接的时候对应的链接状态为 TIME_WAIT。该连接由系统自动关闭。