浅谈C#Socket

Server代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 //添加Socket类
 6 using System.Net;
 7 using System.Net.Sockets;
 8   
 9 namespace SockeConsoleServer
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             int port = 8899;
16             string host = "127.0.0.1";
17   
18             //创建终结点
19             IPAddress ip = IPAddress.Parse(host);
20             IPEndPoint ipe = new IPEndPoint(ip,port); 
21   
22             //创建Socket并开始监听
23   
24             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket对象,如果用UDP协议,则要用SocketTyype.Dgram类型的套接字
25             s.Bind(ipe);    //绑定EndPoint对象(8899端口和ip地址)
26             s.Listen(0);    //开始监听(开始监听客户端的连接)
27   
28             Console.WriteLine("等待客户端连接");
29   
30             //接受到Client连接,为此连接建立新的Socket,并接受消息
31             Socket temp = s.Accept();   //为新建立的连接创建新的Socket
32             Console.WriteLine("建立连接");
33             string recvStr = "";
34             byte[] recvBytes = new byte[1024];
35             int bytes;
36             bytes = temp.Receive(recvBytes,recvBytes.Length,0); //从客户端接受消息
37             recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
38   
39             //给Client端返回信息
40             Console.WriteLine("server get message:{0}",recvStr);    //把客户端传来的信息显示出来
41             string sendStr = "ok!Client send message successful!";
42             byte[] bs = Encoding.ASCII.GetBytes(sendStr);
43             temp.Send(bs,bs.Length,0);  //返回信息给客户端
44             temp.Close();
45             s.Close();
46             Console.ReadLine();
47   
48         }
49     }
50 }

Client代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 //添加用于Socket的类
 6 using System.Net;
 7 using System.Net.Sockets;
 8   
 9 namespace SocketConsoleClient
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             try
16             {
17                 int port = 8899;
18                 string host = "127.0.0.1";
19                 //创建终结点EndPoint
20                 IPAddress ip = IPAddress.Parse(host);
21                 IPEndPoint ipe = new IPEndPoint(ip, port);   //把ip和端口转化为IPEndPoint的实例
22   
23                 //创建Socket并连接到服务器
24                 Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);   //  创建Socket
25                 Console.WriteLine("Connecting...");
26                 c.Connect(ipe); //连接到服务器
27   
28                 //向服务器发送信息
29                 string sendStr = "Hello,this is a socket test";
30                 byte[] bs = Encoding.ASCII.GetBytes(sendStr);   //把字符串编码为字节
31   
32                 Console.WriteLine("Send message");
33                 c.Send(bs, bs.Length, 0); //发送信息
34   
35                 //接受从服务器返回的信息
36                 string recvStr = "";
37                 byte[] recvBytes = new byte[1024];
38                 int bytes;
39                 bytes = c.Receive(recvBytes, recvBytes.Length, 0);    //从服务器端接受返回信息
40                 recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
41                 Console.WriteLine("client get message:{0}", recvStr);    //回显服务器的返回信息
42   
43                 Console.ReadLine();
44                 //一定记着用完Socket后要关闭
45                 c.Close();
46             }
47             catch (ArgumentException e)
48             {
49                 Console.WriteLine("argumentNullException:{0}", e);
50             }
51             catch (SocketException e)
52             {
53                 Console.WriteLine("SocketException:{0}",e);
54             }
55         }
56     }
57 }

posted @ 2017-02-24 11:04  法号阿兴  阅读(566)  评论(0编辑  收藏  举报