如何在C#中将多个客户端连接到单个服务器

时间:2015-12-23 07:18:02

标签: c# network-programming

我有两个应用程序,一个是tcpclient,另一个是tcp服务器。我正在将字符串作为数据从tcpclient发送到tcp服务器。在单个客户端的情况下,所有工作正常但当我尝试从不同的机器运行另一个客户端时,我得到异常"由远程主机强制封闭的连接"请帮我这个如何将多个客户端连接到单个tcp服务器这里是我的代码。

/*TCP SERVER*/
   static void Main(string[] args)
        {
            //192.168.0.105
            IPAddress ipAd = IPAddress.Parse("192.168.0.100");
        // use local m/c IP address, and 
        // use the same in the client
            FileStream f = File.Open("D:/GPSResearchandDevelopment/GPSService/GPSService/abc.txt", FileMode.OpenOrCreate);
            IAsyncResult a = null;
            connection:
            Console.WriteLine("reCHED TO THE CONNECTION");

            TcpListener myList = new TcpListener(ipAd, 8001);


            myList.Start();


            Console.WriteLine("The server is running at port 8001...");
            Console.WriteLine("The local End point is  :" +
                              myList.LocalEndpoint);
            Console.WriteLine("Waiting for a connection.....");

            Socket s = myList.AcceptSocket();

            Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

            byte[] b = new byte[100];
            int k = s.Receive(b);

            Console.WriteLine("Recieved...");

            a = f.BeginWrite(b, 0, b.Length, null, null);
            f.EndWrite(a);

            String[] str = Encoding.UTF8.GetString(b).Split('|');
            for (int i = 0; i < str.Length; i++)
                Console.WriteLine(str[i]);

            ASCIIEncoding asen = new ASCIIEncoding();
            s.Send(asen.GetBytes("The string was recieved by the server."));
            Console.WriteLine("\nSent Acknowledgement");

            //s.Close();
            myList.Stop();

            goto connection;


        }
/*TCP CLIENT*/

 static void Main(string[] args)
        {
        CONNECTAGAIN:
            TcpClient tcpclnt = new TcpClient();

            Console.WriteLine("Connecting.....");


            tcpclnt.Connect("192.168.0.100", 8001);
            // use the ipaddress as in th1e server program

            Console.WriteLine("Connected");
            Console.Write("Enter the string to be transmitted : ");

            String str = Console.ReadLine();
            //String str = "supriya|laxman|medankar";
            Stream stm = tcpclnt.GetStream();

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");

            stm.Write(ba, 0, ba.Length);

            byte[] bb = new byte[100];
            int k = stm.Read(bb, 0, 100);

            for (int i = 0; i < k; i++)
            {
                Console.Write(Convert.ToChar(bb[i]));


            }

            tcpclnt.Close();
//            Console.ReadLine();
            goto CONNECTAGAIN;
        }

2 个答案:

答案 0 :(得分:1)

可以在tcpclient的地方使用socket

答案 1 :(得分:0)

基本上主线程应该做的大部分是

create a server socket //TcpListener mylist

while(true)
    listen for new connection and create a client socket //Socket s
    spawn thread to handle client socket connection

您应该将生成的线程封装在类中,这样您就可以将套接字存储为成员,以便于访问和清理。

您生成/创建的线程应该运行与客户端交互的逻辑。

即。所有这些代码。

 Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

            byte[] b = new byte[100];
            int k = s.Receive(b);

            Console.WriteLine("Recieved...");

            a = f.BeginWrite(b, 0, b.Length, null, null);
            f.EndWrite(a);

            String[] str = Encoding.UTF8.GetString(b).Split('|');
            for (int i = 0; i < str.Length; i++)
                Console.WriteLine(str[i]);

            ASCIIEncoding asen = new ASCIIEncoding();
            s.Send(asen.GetBytes("The string was recieved by the server."));
            Console.WriteLine("\nSent Acknowledgement");

            //s.Close();
            myList.Stop();

如果您这样做,您可以持续监听新的客户端连接,而不依赖于其他客户端之间的交互。