C#:Socket通信

C#:Socket通信

之前一直想自己搞把C#的Socket代码,一直没有下手,今晚终于实践了一把。现把流程编写出来,以备后用。

很简单的源码。

工具:Vs2010

建立项目:C# 控制台应用程序


Server代码

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

  


 

Client代码:

 

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

 

  


posted on 2016-03-21 17:16  song2013  阅读(253)  评论(0编辑  收藏  举报

导航