0

我是 C# 编程的新手。到目前为止,我有一个服务器/单个客户端,但我需要将它变成一个服务器/多个客户端。这是我的代码:

坦克服务器

     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);
            }
        }
    }
}

坦克客户端

 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);
        }
    }
  }
 }
  }

我在互联网上搜索,但我真的不明白该怎么做。

4

2 回答 2

2

您需要使用线程来处理服务器端的客户端。

在这里您可以找到如何在线程上制作服务器的教程

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

这很好。

基本上。服务器在连接时正在侦听 while(true) 循环 服务器创建新线程来处理与客户端的通信。

如果您是初学者,我的好建议是使用异步函数来接收/发送数据以防止 UI 停止响应。BeginConnect、EndConnect、BeginRecieve、EndRecieve 等功能。等等

于 2013-04-08T11:49:28.503 回答
0

通过套接字进行通信 - 例如,您可以检查:

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

这就像代码示例的快速入门。

于 2013-04-08T11:51:00.093 回答