网络编程(Socket)
这里对网络编程做下简单的整理,填补下自己这方面的知识空白。如果想了解请参考张子阳的.Net之美.
***
概念讲述
- TCP:面向连接的传输协议(网络协议的一种)
- 远程主机: 跨进程访问即为远程访问
套接字: 传输层与应用层的纽带,可使应用层编程不用关心传输层
服务器与客户端链接
服务端开启监听服务,客户端链接服务端。新建两个控制台程序Server/Client。
Server:static void Main(string[] args) { Console.WriteLine("Server is running ..."); IPAddress ip = IPAddress.Parse("127.0.0.1"); int port = 8500; TcpListener listener = new TcpListener(ip, port); listener.Start(); Console.WriteLine("\n\n Enter \"Q\" exit."); ConsoleKey key; do { key = Console.ReadKey(true).Key; } while (key != ConsoleKey.Q); }
Client:
static void Main(string[] args) { Console.WriteLine("Client Running"); TcpClient client = new TcpClient(); IPAddress serverIp = IPAddress.Parse("127.0.0.1"); int serverPort = 8500; try { client.Connect(serverIp, serverPort); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } Console.WriteLine("Server Connected!{0} --> {1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint); Console.WriteLine("\n\n Enter \"Q\" exit."); ConsoleKey key; do { key = Console.ReadKey(true).Key; } while (key != ConsoleKey.Q); }
命令行运行
netstat -a
查看端口连接状态服务端获取客户端链接
AcceptTcpClient()
为阻塞方法,调用后线程等待客户端链接
Server:static void Main(string[] args) { Console.WriteLine("Server is running ..."); IPAddress ip = IPAddress.Parse("127.0.0.1"); int port = 8500; TcpListener listener = new TcpListener(ip, port); listener.Start(); Console.WriteLine("Start Listening ..."); //目的获取多个客户端信息 while(true) { try { //获取一个连接,中断方法(等待客户端的链接) TcpClient remoteClient = listener.AcceptTcpClient(); //打印链接到的客户端信息 Console.WriteLine("Client Connected!{0}<-{1}", remoteClient.Client.LocalEndPoint, remoteClient.Client.RemoteEndPoint); } catch (Exception ex) { Console.WriteLine("Error:" + ex.Message); } } }
客户端发送信息服务端接受
Server:
static void Main(string[] args) { int BufferSize = 8192; Console.WriteLine("Server is running ..."); IPAddress ip = IPAddress.Parse("127.0.0.1"); int port = 8500; TcpListener listener = new TcpListener(ip, port); listener.Start(); Console.WriteLine("Start Listening ..."); //目的获取多个客户端信息 while(true) { try { //获取一个连接,中断方法(等待客户端的链接) TcpClient remoteClient = listener.AcceptTcpClient(); //打印链接到的客户端信息 Console.WriteLine("Client Connected!{0}<-{1}", remoteClient.Client.LocalEndPoint, remoteClient.Client.RemoteEndPoint); //获得流并写入buffer中 NetworkStream streamToClient = remoteClient.GetStream(); byte[] buffer = new byte[BufferSize]; int bytesRead = streamToClient.Read(buffer, 0, buffer.Length); Console.WriteLine("Reading data,{0}bytes",bytesRead); //获得请求的字符串 string msg = Encoding.Unicode.GetString(buffer, 0, bytesRead); Console.WriteLine("Received:{0}",msg); } catch (Exception ex) { Console.WriteLine("Error:" + ex.Message); } } }
Client:
static void Main(string[] args) { Console.WriteLine("Client Running"); TcpClient client = new TcpClient(); IPAddress serverIp = IPAddress.Parse("127.0.0.1"); int serverPort = 8500; try { client.Connect(serverIp, serverPort); } catch (Exception ex) { Console.WriteLine(ex.Message); return; } Console.WriteLine("Server Connected!{0} --> {1}", client.Client.LocalEndPoint, client.Client.RemoteEndPoint); string msg = "\"Welcome To TraceFact.Net\""; NetworkStream streamToServer = client.GetStream(); byte[] buffer = Encoding.Unicode.GetBytes(msg); streamToServer.Write(buffer, 0, buffer.Length); Console.WriteLine("Sent:{0}",msg); Console.WriteLine("\n\n Enter \"Q\" exit."); ConsoleKey key; do { key = Console.ReadKey(true).Key; } while (key != ConsoleKey.Q); }