2013-04-08 102 views
0

我是C#編程新手。到目前爲止,我有一臺服務器/單個客戶端,但我需要將它變成一臺服務器/多個客戶端。這裏是我的代碼:C#中的單個服務器/多個客戶端#

TankServer

 using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Net; 
    using System.Net.Sockets; 
    using System.Threading; 

    namespace TankServer 
    { 
     class Program 
    { 
     static Socket sck; 
     static Socket acc; 
     static int port = 1700; 
     static IPAddress ip; 
     static Thread rec; 
     static string name; 

     static string GetIp() 
     { 
      string strHostName = System.Net.Dns.GetHostName(); 
      IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName); 
      IPAddress[] addr = ipEntry.AddressList; 
      return addr[addr.Length - 1].ToString(); 
     } 


     static void recV() 
     { 
      while (true) 
      { 
       Thread.Sleep(500); 
       byte[] Buffer = new byte[255]; 
       int rec = acc.Receive(Buffer, 0, Buffer.Length, 0); 
       Array.Resize(ref Buffer, rec); 
       Console.WriteLine(Encoding.Default.GetString(Buffer)); 
      } 
     } 

     static void Main(string[] args) 
     { 
      rec = new Thread(recV); 
      Console.WriteLine("Your Local Ip is : " + GetIp()); 
      Console.WriteLine("Please enter yout name"); 
      name = Console.ReadLine(); 
      Console.WriteLine("Please Enter Your Host Port"); 
      string inputPort = Console.ReadLine(); 
      try { port = Convert.ToInt32(inputPort); } 
      catch { port = 1700; } 

      ip=IPAddress.Parse(GetIp()); 
      sck= new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 
      sck.Bind(new IPEndPoint(ip, port)); 
      sck.Listen(0); 
      acc=sck.Accept(); 
      rec.Start(); 

      while(true){ 
       byte[] sdata=Encoding.ASCII.GetBytes("<"+name+">"+Console.ReadLine()); 
       acc.Send(sdata,0,sdata.Length,0); 
      } 
     } 
    } 
} 

TankClient

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 

namespace TankClient 
{ 
    class Program 
    { 
    static string name = ""; 
    static int port = 1700; 
    static IPAddress ip; 
    static Socket sck; 
    static Thread rec; 

    static void recV() 
    { 
     while (true) 
     { 
      Thread.Sleep(500); 
      byte[] Buffer = new byte[255]; 
      int rec = sck.Receive(Buffer, 0, Buffer.Length, 0); 
      Array.Resize(ref Buffer, rec); 
      Console.WriteLine(Encoding.Default.GetString(Buffer)); 
     } 
    } 

    static void Main(string[] args) 
    { 

     rec = new Thread(recV); 
     while(true){ 
     Console.WriteLine("Please enter your name"); 
     name = Console.ReadLine(); 
     Console.WriteLine("Please enter the ip of the server"); 
     ip = IPAddress.Parse(Console.ReadLine()); 
     Console.WriteLine("Please Enter The Port"); 
     string inputPort = Console.ReadLine(); 
     try { port = Convert.ToInt32(inputPort); } 
     catch { port = 1700; } 

     sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     sck.Connect(new IPEndPoint(ip, port)); 
     rec.Start(); 
     byte[] conmsg = Encoding.Default.GetBytes("<" + name + ">" + "Connected"); 
     sck.Send(conmsg, 0, conmsg.Length, 0); 

     while (sck.Connected) 
     { 
      byte[] sdata = Encoding.Default.GetBytes("<" + name + ">"+Console.ReadLine()); 
      sck.Send(sdata, 0, sdata.Length, 0); 
     } 
    } 
    } 
} 
    } 

我在互聯網上搜索,但我真的不知道如何去做。

+0

如果您打算使用自定義協議,請嘗試[ZeroMQ](http://www.zeromq.org/)的一個端口[netmq](https://github.com/zeromq/netmq)到。淨。儘管有這個名字,ZeroMQ更多的是關於[IPC](http://en.wikipedia.org/wiki/Inter-process_communication)而不是關於隊列。 – 2013-04-08 11:55:23

回答

2

您需要使用線程來處理服務器端的客戶端。

在這裏你可以找到教程如何使服務器上線

http://csharp.net-informations.com/communications/csharp-multi-threaded-server-socket.htm

這是非常好的。

基本上。服務器正在監聽while(true)循環時連接Server創建新線程來處理與客戶端的通信。

如果您是初學者,我的好習慣是使用異步函數接收/發送數據以防止UI停止響應。像BeginConnect,EndConnect,BeginRecieve,EndRecieve函數。等

+0

雖然您可以使用這種方式的線程,但對於少量客戶端來說足夠好,但它並不能很好地擴展。 – Matt 2014-11-23 21:03:58

0

連通的通插座 - 爲例,您可以檢查:

http://ludwigstuyck.wordpress.com/2012/09/21/communicating-through-sockets/

這就像一個快速啓動的代碼樣本。

+0

它不起作用...... SocketPacket有一些錯誤 – 2013-04-08 12:28:15

+0

你在說什麼錯誤? – 2013-04-08 12:30:22

+0

visual studio無法識別socketPacket變量...我默認創建了一個類,但它仍然不起作用 – 2013-04-08 13:57:14