• Socket学习


    服务器端代码:(ctrl + shift + space  重载   ctrl + k+ s 外侧代码)

    //第一个参数寻址方式  第二个参数是数据传输方式   第三个是传输协议

    Socket socket = new Socket(AddressFaimly.InterNetwork,SocketType.Stream, ProtocoType.Tcp);

    //绑定端口和IP

    socket.Bind(new IPEndpoint(ipstring,portNum));

    //侦听

    socket.Listen(10);

    //线程池创建线程:由于socket.Accept会阻塞主线程,所以开启一个新线程

    ThreadPool.QueueuerworkItem(p=>{

    Socket serversocket = (Socket) p;

    while(true)

    {

    //接受客户端连接,返回客户端通讯socket

    Socket proxSocket = socket.Accept();

      WriteText("有客户端连接",proxsocket.RemoteEndpoint.Tostring());

      Task task = new Task(s=>{

      //客户端通讯socket

      Socket pSocket = (socket)s;

        while(true)

        {

         try

          {

            byte[] buffer = new byte[1024*1024];

          int realLen = pSocket.Receive(buffer);

          //客户端正常退出,返回一个空字节

          if(realLen<=0)

          {

            if(pSocket != null && pSocket.connected)

            {

              WriteText("客户端退出",psocket.RemoteEndpoint.Tostring());

              pSocke.shutdown(socketshutdown.Both);

              pSocket.close();

              return;

            }

          }

          string  strmsg = Encoding.Default.Getstring(buffer,0,realLen);

          

          WriteText(strmsg,proxsocket.RemoteEndpoint.Tostring());

          }

          catch(Exception e)

          {  

            if(psocket != null && psocket.connected)

            {  

              WriteText("客户端异常",psocket.RemoteEndpoint.Tostring());

              psocket.shutdown(socketshutdown.both);

              pSocket.close();

              return;

            }

          }

        }

      },proxSocket);

    thread.isbackground = true;

    task.Start();

    }

    },socket );

    public void WriteText(string text ,string form)

    {

    if(txtLog.InvokeRequired)

    {

    //跨线程调用控件

    txtLot.Invoke(new Action(()=>{

    this.txtLog.Text = string.Format("当前一个客户连接上{0} ",text,from ,this.txtLog.Text);

    }));

    }

    else

    this.txtLog.Text = string.Format("当前一个客户连接上{0} ",text,from ,this.txtLog.Text);

    客户端:

    public socket currentSocket{set;get;};

    try

     {

       

    //第一个参数寻址方式  第二个参数是数据传输方式   第三个是传输协议

    Socket socket = new Socket(AddressFaimly.InterNetwork,SocketType.Stream, ProtocoType.Tcp);

    //绑定端口和IP

    socket.Connect(IPstring,portNum);

    currentSocket = socket;

    //socket.send(10);

    //线程池创建线程:由于socket.Accept会阻塞主线程,所以开启一个新线程

    ThreadPool.QueueuerworkItem(p=>{

    Socket serversocket = (Socket) p;

      //Task.Factory.Startnew();

      Thread thread = new Thread(()=>{

        while(true)

        {

          try

          {

          byte[] buffer = new byte[1024*1024];

          int realLen = pSocket.Receive(buffer);

          if(realLen<=0)

          {

            if(pSocket != null && pSocket.connected)

            {

              WriteText("客户端退出",psocket.RemoteEndpoint.Tostring());

              pSocke.shutdown(socketshutdown.Both);

              pSocket.close();

              return;

            }

          }

          string  strmsg = Encoding.Default.Getstring(buffer,0,realLen);

          

          WriteText(strmsg,proxsocket.RemoteEndpoint.Tostring());

         }

          }

        catch

        {}

    });

      thread.isbackground = true;

      thread.start();

     }

     catch(Exception e)

     {  

      }

         

  • 相关阅读:
    MySQL基础之排序检索数据
    网络编程之并发网络编程
    网络编程之粘包问题
    MySQL基础之检索数据
    MySQL基础之使用MySQL
    MySQL基础之MySQL简介
    MySQL基础之了解MySQL
    网络编程之socket编程
    网络编程之网络通信原理
    别找了,最全的搜集关键词方法在这里
  • 原文地址:https://www.cnblogs.com/mojie/p/10590915.html
Copyright © 2020-2023  润新知