Socket学习进阶之基础通信

服务端代码:

 1 using System;
 2 using System.Text;
 3 using System.Net;
 4 using System.Net.Sockets;
 5 
 6 public class server
 7 {
 8     public static void Main() 
 9     {
10         try 
11         {
12             // 把IP地址转换为IPAddress的实例
13             IPAddress ipAd = IPAddress.Parse("127.0.0.1");
14             
15             // 初始化监听器, 端口为8001
16             TcpListener myList=new TcpListener(ipAd,8001);
17             
18             // 开始监听服务器端口
19             myList.Start();
20 
21             // 输出服务器启动信息
22             Console.WriteLine("启动端口服务...");    
23             Console.WriteLine("本地节点为:" + myList.LocalEndpoint );
24             Console.WriteLine("等待连接.....");
25             
26             // 等待处理接入连接请求
27             // 新建立的连接用套接字s表示
28             Socket s=myList.AcceptSocket();
29             Console.WriteLine("连接来自 "+s.RemoteEndPoint);
30             
31             // 接收客户端信息
32             byte[] b=new byte[100];
33             int k=s.Receive(b);
34             Console.WriteLine("已接收...");
35             for (int i=0;i<k;i++)
36             {
37                 Console.Write(Convert.ToChar(b[i]));
38             }
39             
40             // 处理客户端请求,给客户端回应
41             ASCIIEncoding asen=new ASCIIEncoding();
42             s.Send(asen.GetBytes("The string was recieved by the server."));
43             Console.WriteLine("
已发送回应信息");
44             
45             // 释放资源 结束侦听
46             s.Close();
47             myList.Stop();
48         }
49         catch (Exception e)
50         {
51             Console.WriteLine("Error..... " + e.StackTrace);
52         }    
53         Console.ReadLine();
54 
55     }
56 }

客户端代码:

 1 using System;
 2 using System.IO;
 3 using System.Net;
 4 using System.Text;
 5 using System.Net.Sockets;
 6 
 7 public class client 
 8 {
 9     public static void Main() 
10     {
11         try 
12         {
13             // 新建客户端套接字
14             TcpClient tcpclnt = new TcpClient();
15           Console.WriteLine("连接.....");
16             
17             // 连接服务器
18           tcpclnt.Connect("127.0.0.1",8001);
19           Console.WriteLine("已连接");
20           Console.Write("请输入要传输的字符串 : ");
21             
22             // 读入字符串
23           String str=Console.ReadLine();
24 
25             // 得到客户端的流
26           Stream stm = tcpclnt.GetStream();
27             
28            // 发送字符串
29           ASCIIEncoding asen= new ASCIIEncoding();
30           byte[] ba=asen.GetBytes(str);
31           Console.WriteLine("传输中.....");            
32           stm.Write(ba,0,ba.Length);
33             
34            // 接收从服务器返回的信息
35           byte[] bb=new byte[100];
36           int k=stm.Read(bb,0,100);
37             
38            // 输出服务器返回信息
39            for (int i=0;i<k;i++)
40            {
41                Console.Write(Convert.ToChar(bb[i]));
42            }
43             
44             // 关闭客户端连接
45           tcpclnt.Close();
46       }        
47       catch (Exception e) 
48         {
49           Console.WriteLine("Error..... " + e.StackTrace);
50       }
51 
52       Console.ReadLine();
53     }
54 }    

测试:

原文地址:https://www.cnblogs.com/ziranquliu/p/4709706.html