• Socket


    客户端

    try
                {
                    // TcpListener
                    // TcpClient
                    int port = 2000;
                    string host = "127.0.0.1";
                    IPAddress ip = IPAddress.Parse(host);
                    IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例
                    Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
                    Console.WriteLine("Conneting");
                    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);//显示服务器返回信息
                    c.Close();
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("ArgumentNullException: {0}", e);
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
                Console.WriteLine("Press Enter to Exit");
                Console.ReadLine();
    

      服务端:

     try
                {
                    int port = 2000;
                    string host = "127.0.0.1";
                    IPAddress ip = IPAddress.Parse(host);
                    IPEndPoint ipe = new IPEndPoint(ip, port);
                    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket类
                    s.Bind(ipe);//绑定2000端口
                    s.Listen(0);//开始监听
                    Console.WriteLine("Wait for connect");
                    Socket temp = s.Accept();//为新建连接创建新的Socket。
                    Console.WriteLine("Get a connect");
                    string recvStr = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes;
                    bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息
                    recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                    Console.WriteLine("Server Get Message:{0}", recvStr);//把客户端传来的信息显示出来
                    string sendStr = "Ok!Client Send Message Sucessful!";
                    byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                    temp.Send(bs, bs.Length, 0);//返回客户端成功信息
                    temp.Close();
                    s.Close();
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("ArgumentNullException: {0}", e);
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
                Console.WriteLine("Press Enter to Exit");
                Console.ReadLine();

    上述代码只是同步的Socket通信,我也只是初学

  • 相关阅读:
    很实用的html meta标签实现页面跳转
    oracle 实例名和服务名以及数据库名区别
    Oracle 创建 DBLink 的方法
    Java (六):java中Math常用方法
    Java (四):String,StringBuilder,StringBuffer三者的区别
    ROS Learning-001 安装 ROS indigo
    Windows cmd 将命令(/指令)写到一个文件里,直接运行这个文件。提高工作效率
    Blender 基础 骨架-02 骨架的各种呈现方式
    Blender 基础 骨架 01
    Python 解决 :NameError: name 'reload' is not defined 问题
  • 原文地址:https://www.cnblogs.com/LoveAndPeace/p/8398744.html
Copyright © 2020-2023  润新知