2014-05-07 48 views
0

我写了一个使用C#的客户机服务器程序,程序运行在同一台机器上,客户机发送消息,服务器显示te消息,我该如何编辑我的程序使服务器和不同的机器上运行的客户端,也即10个客户端可以连接到服务器Clien-Server运行在不同的机器和多个客户机上

这是我的客户端代码:

public partial class ClientForm : Form 
    { 
     const int BUFFER_SIZE = 1024; 
     byte[] sendBuffer = new byte[BUFFER_SIZE]; 
     byte[] rcvBuffer = new byte[BUFFER_SIZE]; 

     Socket clientSocket; 
     const int PORT = 3333; 

     public ClientForm() 
     { 
      InitializeComponent(); 
     } 

     void btnConnect_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
       // Connect to the local host 
       clientSocket.Connect(new IPEndPoint(IPAddress.Loopback, PORT)); 
      } 
      catch (Exception ex) { AppendToTextBox(ex.Message); } 

      EnableSendButton(); 
     } 

     void btnSend_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       // Serialize the textBox text before sending 
       sendBuffer = Encoding.ASCII.GetBytes(textBoxInput.Text); 

       // Sends contents of textbox to the server 
       clientSocket.Send(sendBuffer); 

       // Prepares to receive something, i.e. the echo 
       clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null);//.Receive(rcvBuffer); 
      } 
      catch (Exception ex) { AppendToTextBox(ex.Message); } 
     } 

     // About Asynchronous Callbacks: http://stackoverflow.com/questions/1047662/what-is-asynccallback 
     void ReceiveCallback(IAsyncResult AR) 
     { 
      try 
      { 
       int bytesReceived = clientSocket.EndReceive(AR); // Number of bytes received 
       byte[] rcvBufferTrim = new byte[bytesReceived]; 
       Array.Copy(rcvBuffer, rcvBufferTrim, bytesReceived); // Removes trailing nulls 
       string text = Encoding.ASCII.GetString(rcvBufferTrim); // Convert bytes into string 
       AppendToTextBox(DateTime.Now.ToString("HH:mm:ss") + ": " + text);  // Displays buffer contents as text 

       // Starts receiving data again 
       clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null); 
      } 
      catch (Exception ex) { AppendToTextBox(ex.Message); } 
     } 
} 

和服务器代码:

public partial class ServerForm : Form 
    { 
const int BUFFER_SIZE = 1024; 
     byte[] rcvBuffer = new byte[BUFFER_SIZE]; // To recieve data from the client 

     Socket serverSocket; 
     Socket clientSocket; // We will only accept one client socket for now 
     const int PORT = 3333; 

     public ServerForm() 
     { 
      InitializeComponent(); 

      StartServer(); 
     } 



     void StartServer() 
     { 
      try 
      { 

       serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
       serverSocket.Bind(new IPEndPoint(IPAddress.Any, PORT)); 
       serverSocket.Listen(10);  // Listen(1) means a maximum of one client pending 
       serverSocket.BeginAccept(AcceptCallback, null); 








      } 
      catch (Exception ex) { AppendToTextBox(ex.Message); } 
     } 

     // About Asynchronous Callbacks: http://stackoverflow.com/questions/1047662/what-is-asynccallback 
     void AcceptCallback(IAsyncResult AR) 
     { 

      try 
      { 

       clientSocket = serverSocket.EndAccept(AR); // Client connected successfully, waiting for requests 
       AppendToTextBox("Client connected successfully..."); 
       clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null); 
      } 
      catch (Exception ex) { AppendToTextBox(ex.Message); } 
     } 


     void ReceiveCallback(IAsyncResult AR) 
     { 
      try 
      { 
       int bytesReceived = clientSocket.EndReceive(AR); // Number of bytes received 
       byte[] rcvBufferTrim = new byte[bytesReceived]; 
       Array.Copy(rcvBuffer, rcvBufferTrim, bytesReceived); // Removes trailing nulls 
       string text = Encoding.ASCII.GetString(rcvBufferTrim); // Convert bytes into string 
       AppendToTextBox("Received Text: " + text); 


       if (text == "getdate") 
        text = "\t"+ System.DateTime.Now.ToString("dd.MM.yyyy"); 
       else if (text == "gettime") 
        text = "\t"+ System.DateTime.Now.ToString("h:mm:ss "); 

       /////////////////////////////////////////////////////////////////////////////////// 

       // Reply with echo 
       byte[] echoBuffer = Encoding.ASCII.GetBytes("Echo... " + text); 
       clientSocket.Send(echoBuffer); 

       // Starts receiving data again 
       clientSocket.BeginReceive(rcvBuffer, 0, rcvBuffer.Length, SocketFlags.None, ReceiveCallback, null); 
      } 
      catch (Exception ex) { AppendToTextBox("Exception: " + ex.Message + "..."); } 
     } 

     // Provides a thread-safe way to append text to the textbox 
     void AppendToTextBox(string text) 
     { 
      MethodInvoker invoker = new MethodInvoker(delegate 
      { 
       textBox.Text += text + "\r\n\r\n"; 
       textBox.SelectionStart = textBox.TextLength; 
       textBox.ScrollToCaret(); 

      }); // "\r\n" are for new lines 
      this.Invoke(invoker); 
     } 
} 
+0

试试这个:http://stackoverflow.com/questions/19218589/tcp-server-with-multiple-clients –

+0

或者这:http://stackoverflow.com/questions/7104293/b​​est-way-to-接受-多TCP客户机 –

回答

0

允许服务器和客户端是在两个不同的机器上,你需要改变clientSocket.Connect(new IPEndPoint(IPAddress.Loopback, PORT));

IPAddress ipAd = IPAddress.Parse(YOUR IP HERE); 
clientSocket.Connect(ipAd, YOUR PORT HERE); 

而且香港专业教育学院在我的图书馆做了一个简单的客户端,如果你想看看:https://github.com/TheRealNOIG/TylerGregorcyksLibrary/blob/master/TylerGregorcyksLibrary/Main/SimpleClient.cs

现在制造能够处理多个客户端的服务器是有点困难,但幸运的我已经打了一个比方

class Server 
{ 
    private static int bufferSize = 2048; 
    private static byte[] buffer = new byte[bufferSize]; 
    private static List<Socket> clientSockets = new List<Socket>(); 
    private static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 


    static void Main(string[] args) 
    { 
     Console.Title = "Server"; 
     SetupServer(); 
     Console.ReadKey(); 
     CloseAllSockets(); 
    } 

    private static void SetupServer() 
    { 
     Console.WriteLine("Settings Up Server Plz Wait"); 
     serverSocket.Bind(new IPEndPoint(IPAddress.Any, YOUR PORT HERE)); 
     serverSocket.Listen(10); 
     serverSocket.BeginAccept(new AsyncCallback(CallBack), null); 
     Console.WriteLine("Server Made"); 
    } 

    private static void CallBack(IAsyncResult e) 
    { 
     try 
     { 
      Socket socket = serverSocket.EndAccept(e); 
      clientSockets.Add(socket); 
      Console.WriteLine("Client Connected"); 
      socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket); 
      serverSocket.BeginAccept(new AsyncCallback(CallBack), null); 
     } 
     catch (Exception ex) { Console.WriteLine(ex.Message); } 
    } 

    private static void ReceiveCallBack(IAsyncResult e) 
    { 
     try 
     { 
      Socket socket = (Socket)e.AsyncState; 
      int received; 
      try 
      { 
       received = socket.EndReceive(e); 
      } 
      catch (SocketException) 
      { 
       Console.WriteLine("Client forcefully disconnected"); 
       socket.Close(); 
       clientSockets.Remove(socket); 
       return; 
      } 
      byte[] dataBuf = new byte[received]; 
      Array.Copy(buffer, dataBuf, received); 

      string text = Encoding.ASCII.GetString(dataBuf); 
      Console.WriteLine("Client request: " + text); 

      string response = string.Empty; 


      try 
      { 
       if (command.ToLower() == "What Time is it") 
       { 
        response = DateTime.Now.ToString(); 
       } 
       else if (command.ToLower() == "Whats your name") 
       { 
        response = "Tyler Gregorcyk"; 
       } 
       else 
       { 
        response = "Invaled"; 
       } 
      } 
      catch (Exception et) { Console.WriteLine(et.Message); socket.Close(); clientSockets.Remove(socket); } 


      if(response == string.Empty) 
      { 
       response = "Invaled"; 
      } 

      if (response != string.Empty) 
      { 
       Console.WriteLine("Sent To Client: " + response); 
       byte[] data = Encoding.ASCII.GetBytes(response); 
       socket.Send(data); 
       serverSocket.BeginAccept(new AsyncCallback(CallBack), null); 
      } 

      conn.Close(); 
      socket.BeginReceive(buffer, 0, bufferSize, SocketFlags.None, ReceiveCallBack, socket); 
     } 
     catch (Exception) { } 
    } 

    private static void CloseAllSockets() 
    { 
     foreach (Socket socket in clientSockets) 
     { 
      socket.Shutdown(SocketShutdown.Both); 
      socket.Close(); 
     } 

     serverSocket.Close(); 
    } 
} 

顺便说一句,这是YouTube上的教程的编辑后的版本:https://www.youtube.com/watch?v=xgLRe7QV6QI

相关问题