• C# Socket通信 小案例


    本文将编写2个控制台应用程序,一个是服务器端(server),一个是客户端(client),

    通过server的监听,有新的client连接后,接收client发出的信息。

     

    server代码如下:

     1 using System;
     2 using System.Net;
     3 using System.Net.Sockets;
     4 using System.Text;
     5 using System.Threading;
     6 
     7 namespace Server
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Socket server = new Socket(
    14                 //寻址方式,InterNetwork是指IPv4的方式
    15                 AddressFamily.InterNetwork,
    16                 //套接字类型,一般都是采取stream,即流的形式
    17                 SocketType.Stream,
    18                 //通信协议,这里使用TCP
    19                 ProtocolType.Tcp
    20                 );
    21             //绑定端口,参数是IP地址和端口号,IP地址即本机的IP地址,端口号则随便,只要不是已经被占用的即可
    22             server.Bind(new IPEndPoint(IPAddress.Parse("192.168.1.101"), 12312));
    23             //设置监听的client上限
    24             server.Listen(5);
    25             Console.WriteLine("server is listening");
    26             //用于接收client连接的线程
    27             Thread tAccept = new Thread(() =>
    28             {
    29                 //使用死循环
    30                 while (true)
    31                 {
    32                     //接收
    33                     Socket client = server.Accept();
    34                     Console.WriteLine("new client is connection,ip is" + client.RemoteEndPoint);
    35                     //用于接收信息的线程
    36                     Thread tReceive = new Thread(() =>
    37                     {
    38                         while (true)
    39                         {
    40                             byte[] bs = new byte[1024];
    41                             //使用字节数组接收
    42                             int length = client.Receive(bs);
    43                             string str = Encoding.UTF8.GetString(bs);
    44                             Console.WriteLine(str);
    45                         }
    46                     }
    47                         );
    48                     tReceive.IsBackground = true;
    49                     tReceive.Start();
    50                 }
    51             }
    52                 );
    53             tAccept.IsBackground = true;
    54             tAccept.Start();
    55             Console.ReadKey();
    56         }
    57     }
    58 }

     

    client代码如下:

     1 using System;
     2 using System.Net;
     3 using System.Net.Sockets;
     4 using System.Text;
     5 
     6 namespace Client
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             Socket client = new Socket(
    13                 AddressFamily.InterNetwork,
    14                 SocketType.Stream,
    15                 ProtocolType.Tcp
    16                 );
    17             client.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.101"), 12312));
    18             while (true)
    19             {
    20                 Console.Write("please type something:");
    21                 string str = Console.ReadLine();
    22                 if (str.Equals("quit"))
    23                 {
    24                     break;
    25                 }
    26                 byte[] bs = Encoding.UTF8.GetBytes(str);
    27                 client.Send(bs);
    28             }
    29 
    30             //关闭通信的方式,这里不需用,因为会引发异常
    31             //client.Close();
    32             //client.Shutdown(SocketShutdown.Both);
    33             Console.ReadKey();
    34         }
    35     }
    36 }

     

    运行:

    1、先运行server;

    2、再运行client,输入内容后,server会显示内容,输入“quit”则关闭通信连接。

    完成

  • 相关阅读:
    NOI 2016 区间 解题报告
    有关莫队
    [JSOI2008]最大数 线段树解法
    HDU P3341 Lost's revenge 题解+数据生成器
    BZOJ P1212 [HNOI2004] L语言
    洛谷P3168 [CQOI2015]任务查询系统
    普通平衡树Tyvj1728、luogu P3369 (splay)
    洛谷P3384 树链剖分
    BZOJ P2157 旅游
    【算法导论】第6章,堆排序
  • 原文地址:https://www.cnblogs.com/chenyucong/p/5728331.html
Copyright © 2020-2023  润新知