zoukankan      html  css  js  c++  java
  • C#最基本的Socket编程

    示例程序是同步套接字程序,功能很简单,只是客户端发给服务器一条信息,服务器向客户端返回一条信息,是一个简单示例,也是一个最基本的socket编程流程。

    简单步骤说明:

    1.用指定的port, ip 建立一个EndPoint对象

    2.建立一个Socket对象;

    3.用Socket对象的Bind()方法绑定EndPoint

    4.用Socket对象的Listen()方法开始监听

    5.接收到客户端的连接,用socket对象的Access()方法创建新的socket对象用于和请求的客户端进行通信

    6.通信结束后记得关闭socket

    代码:

     1  int port = 2000;
     2             string host = "127.0.0.1";
     3             // create endPoint 
     4             IPAddress ip = IPAddress.Parse(host);
     5             IPEndPoint ipe = new IPEndPoint(ip, port);
     6             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     7             s.Bind(ipe);
     8 
     9             s.Listen(0);
    10 
    11             Console.WriteLine("wait a client to connect..");
    12             Socket temp = s.Accept();
    13             Console.WriteLine("create a connection");
    14 
    15             //receive message
    16             string recvStr = "";
    17             byte[] recvBytes= new byte[1024];
    18             int bytes;
    19             bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
    20             recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
    21             Console.WriteLine("server have get message:{0}",recvStr );
    22 
    23             // send message
    24             string sendStr = "ok! client send message cuccessful";
    25             byte[] bs = Encoding.ASCII.GetBytes(sendStr);
    26             temp.Send(bs, bs.Length, 0);
    27 
    28             //end
    29             temp.Close();
    30             s.Close();
    31         
    32             Console.WriteLine("end socket com!");
    33             Console.ReadLine();

     客户端:

    1.用指定的port, ip 建立一个EndPoint对象

    2.建立一个socket对象

    3.用socket对象的Connect()方法以上面的EndPoint对象作为参数,向服务器发送连接请求;

    4.如果连接成功,就用socket对象的Send()方法向服务器发送信息

    5.用socket对象的Receive()方法接收服务器发来的信息

    6.通信结束记得关闭socket:

    代码::

    //init server port and ip
                    int port = 2000;
                    string host = "127.0.0.1";
                    IPAddress ip = IPAddress.Parse(host);
    
                    // create ipendpoint
                    IPEndPoint ipe = new IPEndPoint(ip, port);
    
                    // create client sockets
                    Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    Console.WriteLine("Connecting...");
                    
                    // connect to server
                    c.Connect(ipe);
                    
                    // send to server
                    string sendStr = "Hello, this is a socket test";
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                    Console.WriteLine("Send Message");
                    c.Send(bs, bs.Length, 0);
    
                    // receive a message from server
                    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);
    
                    //close Client
                    c.Close();
    
                }
                catch (ArgumentException e)
                {
    
                    Console.WriteLine("argumentNullException:{0}",e);
                }
                catch (SocketException e)
                {
                    Console.WriteLine("socketException:{0}",e);
                }
                Console.WriteLine("Press enter to exit");
                Console.ReadLine();

    来源:内容来自网络

  • 相关阅读:
    macOS Sierra 如何打开任何来源
    centos 安装git服务器,配置使用证书登录并你用hook实现代码自动部署
    Linux下修改Mysql的用户(root)的密码
    mysql主从复制
    CentOS7下安装MySQL5.7安装与配置
    gulp安装和使用
    libiconv库的安装和使用
    Android 开发中常见的注意点
    扯一扯 C#委托和事件?策略模式?接口回调?
    Python 学习开篇
  • 原文地址:https://www.cnblogs.com/lucode/p/socket.html
Copyright © 2011-2022 走看看