C#利用服务器实现客户端之间通信

编程学习 2021-07-04 22:41www.dzhlxh.cn编程入门
这篇文章主要为大家详细介绍了C#利用服务器实现客户端之间通信,感兴趣的小伙伴们可以参考一下

先来讲述下我自己对于整个Socket通信过程的理解,毕竟初学,说错见谅,知道错了会改正~ 

首先在服务端新建一个serverSocket,对其进行初始化(一般包含AddressFamily:IP地址类型,SocketType:Socket传输数据方式,ProtoType:传输协议); 

接着我们要设置server端要绑定的IP:port;然后开始监听,并设置最多同时监听多少个Client.

这时,服务端就在等待状态,直到某一个Client连接到这个ip:port上,这时serverSocket.Accept()工作,获得这个连接。(此时的连接是有地址信息的哦!记得保存) 

获得连接之后,server就可以和这个Client进行通信了,当加入第二个Client(我们称其为ClientB)时,Server接收到ClientB的消息,可以将这个消息转发给前一个Client(我们称其为ClientA),当受到ClientA的消息,也可以转发给ClientB。这样就实现了Clients之间的通信了。(重点在保存连接信息!!)

那么现在贴上代码讲解:

Server端代码 

namespace SocketServer
{
  class Program
  {
    private static byte[] result = new byte[1024];
   
    static Socket serverSocket;
    private static string client;
    private static Socket clientSocket;
    //我这里存了两个Client,因为自己电脑开了两个Client,不会有多的
    //理论上应该开一个Socket[]来保存信息,最好用一个二元组将client的信息和连接绑定起来
    //这样就可以实现断开连接后下次登陆还是可以识别是这个Client
    private static Socket clientSocketA=null;
    private static Socket clientSocketB=null;
    
    static void Main(string[] args)
    {
      Program.SetPort(8885);
    }
    private static void SetPort(int port)
    {
      IPAddress ip = IPAddress.Parse("127.0.0.1");//set ip
      serverSocket = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp);//initialize
      serverSocket.Bind(new IPEndPoint(ip, port));//bind
      serverSocket.Listen(10);
      //进入监听状态
      Console.WriteLine("监听{0}成功", serverSocket.LocalEndPoint.ToString());
      //开启一个线程来监听客户端连接
      Thread myThread = new Thread(ListenClientConnect);
      myThread.Start();
      Console.ReadLine();

    }
    /// <summary>
    /// 监听客户端连接
    /// </summary>
    private static void ListenClientConnect()
    {
      while (true)
      {
        //Client连接上后 得到这个连接
        clientSocket = serverSocket.Accept();

        //这里我因为只有两个Client,所以就简单写了
        if (clientSocketA == null)
        {
          clientSocketA = clientSocket;
        }
        else if (clientSocketB == null)
        {
          clientSocketB = clientSocket;
        }
        else
        {
          //当其中一个断开了,又重新连接时,需要再次保存连接
          if (clientSocketB.IsBound)
          {
            clientSocketA = clientSocketB;
            clientSocketB = clientSocket;
          }
          else
          {
            clientSocketB = clientSocketA;
            clientSocketA = clientSocket;
          }
          
        }
        clientSocket.Send(Encoding.ASCII.GetBytes("say hello"));
        //开个线程接收Client信息
        Thread receivedThread = new Thread(ReceiveMessage);
        receivedThread.Start(clientSocket);

      }
    }
   
    private static void ReceiveMessage(object clientSocket)
    {
      Socket myClientSocket = (Socket) clientSocket;
      
      while (true)
      {
        try
        {
          int revceiveNumber = myClientSocket.Receive(result);
          //Console.WriteLine("接受客户端{0}消息{1}", myClientSocket.RemoteEndPoint.ToString()
          //  , Encoding.ASCII.GetString(result, 0, revceiveNumber));
          Console.WriteLine(Encoding.ASCII.GetString(result, 0, revceiveNumber));
          if (myClientSocket == clientSocketA)
          {
            Console.WriteLine("receive from A");
            if (clientSocketB!=null&&clientSocketB.IsBound)
            {
              Console.WriteLine("a IS BOUND");
              clientSocketB.Send(result, 0, revceiveNumber, SocketFlags.None);
            }
            else
            {
              myClientSocket.Send(Encoding.ASCII.GetBytes("the people is not online! Send Failed!"));
              Console.WriteLine("对方不在线上,发送失败!");
            }
          }
          else
          {
            Console.WriteLine("receive from B");
            if (clientSocketA != null && clientSocketA.IsBound)
            {
              Console.WriteLine("a IS BOUND");
              clientSocketA.Send(result, 0, revceiveNumber, SocketFlags.None);
            }  
            else
            {
              myClientSocket.Send(Encoding.ASCII.GetBytes("the people is not online! Send Failed!"));
              Console.WriteLine("对方不在线上,发送失败!");
            }

          }
          
        }
        catch(Exception ex)
        {
          Console.WriteLine(ex.Message);
          myClientSocket.Shutdown(SocketShutdown.Both);
          myClientSocket.Close();
          break;

        }
      }

    }
  }
}

Client端代码(因为都差不多 就只贴一个了) 

namespace SocketClient
{
  class Program
  {
    private static byte[] result = new byte[1024];
    private static Socket clientSocket;
    private static void ListenServer()
    {
      while (true)
      {
        result = new byte[1024];
        int receiveLength = clientSocket.Receive(result);
        
        Console.WriteLine("{0}", Encoding.ASCII.GetString(result, 0, receiveLength));
      }
      
    }
    static void Main(string[] args)
    {

      IPAddress ip = IPAddress.Parse("127.0.0.1");
      clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      try
      {
        clientSocket.Connect(ip, 8885);
        Console.WriteLine("连接成功!");

      }
      catch (Exception e)
      {
        Console.WriteLine("连接失败!");
        return;
      }
      Thread threadRead = new Thread(ListenServer);
      threadRead.Start();
     

      while(true)
      {
        try
        {
          Thread.Sleep(1000);
          string sendMessage = Console.ReadLine();
          clientSocket.Send(Encoding.ASCII.GetBytes("Sylvia:"+sendMessage));
          
        }
        catch (Exception ex)
        {
          clientSocket.Shutdown(SocketShutdown.Both);
          clientSocket.Close();
          break;
        }

      }
      Console.WriteLine("发送完毕 按回车退出");
      Console.ReadKey();
    }
  }
}

写的时候要特别注意一下Send信息的时候,注意byte[]的传输大小,很容易变成byte[]数组的大小而不是内容的大小。 

这个大家就自己尝试吧。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板