• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#:Socket通信C#:Socket通信

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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);
            }
        }
    }
}

 

  


之前一直想自己搞把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);
            }
        }
    }
}

 

  



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VisualC#2010新特性之命名和可选参数与类型等价支持发布时间:2022-07-10
下一篇:
C#中用GDI+生成饼状图和柱状图发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap