TCP服务器&客户端MultiThreaded [英] TCP Server & Client MultiThreaded

查看:100
本文介绍了TCP服务器&客户端MultiThreaded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gday男孩/女孩,



我正在尝试创建TCP服务器和客户端。我希望服务器能够24/7处理多个活动客户端(大约10-15个客户端,可能更多)。服务器将向客户端发送字符串,客户端根据发送的字符串运行命令。我有一个基本的TCP服务器和客户端。 TCP服务器一次只接受1个客户端,有人可以提供示例或修改我的代码以允许服务器允许多个客户端。也有人可以提供一个如何使服务器发送命令和客户端等待命令的示例,因为我只能让它以相反的方式工作。非常感谢任何帮助。



此外,客户端需要保持连接,以便服务器可以随时发送命令。



Matt





服务器代码

Gday Guys/Girls,

I am trying to create a TCP server and clients. I want the server to be able to handle multiple active clients 24/7 (approx 10-15 clients, possibly more). The server will send a string to the client and the client runs a command based on the string sent. I have a basic TCP Server and Client. The TCP Server only accepts 1 client at a time, could someone please provide and example or modify my code to allow the server to allow multiple clients. Also could someone provide an example on how to make the server send the command and the client wait for the command as I can only get it to work the other way around. Any help is greatly appreciated.

Also, the client will need to stay connected so that the server can send commands at any time.

Matt


SERVER CODE

public static void Main(string[] args)
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("127.0.0.1");
                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");
               // Connection();
               // 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...");
                string command = string.Empty;
                for (int i = 0; i < k; i++)
                {
                    command = command + Convert.ToChar(b[i]);
                }
                Console.WriteLine(command);
                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes("String Recievced"));
                Console.WriteLine("Sent Acknowledgement");
                s.Close();
                myList.Stop();
                Console.ReadLine();
            }

            catch(Exception ex)
            {
                Console.WriteLine("ERROR : " + ex.Message);
            }
        }





客户代码





CLIENT CODE

static void Main(string[] args)
        {
            try
            {
                TcpClient tcp = new TcpClient();
                tcp.Connect("127.0.0.1", 8001);
                string str = Console.ReadLine();
                Stream stm = tcp.GetStream();
                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                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]));
                
                tcp.Close();

                Console.ReadLine();
            }
            catch(Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
            }
        }

推荐答案

Microsoft有一些很好的套接字示例,包括同步和异步套接字。 />
我认为这是一个很好的开始。



套接字代码示例 [ ^ ]



说到你希望服务器发送一个命令和客户端等待,你基本上想要的是让客户端成为服务器,反之亦然。

客户端 - 服务器模型意味着服务器等待做某事和客户端做请求。



您可以做的是让客户端连接到服务器,然后创建一个新的连接,其中客户端是服务器,然后关闭第一个连接。

这对我来说似乎是一个复杂的设置但是技术上是可行的。
Microsoft has some pretty good examples for sockets, both synchronous and asynchronous.
It is a good start for you I think.

Socket Code Examples[^]

When it comes to your wish to make the server send a command and the client to wait, what you basically want is to let the client become the server and vice versa.
The client-server model means that the server waits for something to do and the clients make requests.

What you can do is to let the client hook up to the server and then create a new connection where the client is the server, and then close the first connection.
It seems like a complicated setup to me, but it is technically possible.


你想使用多线程并在服务器端的单个线程中做两件事很奇怪:监听新连接和发送/接收消息到/来自网络流。他们当然应该是单独的线程。有关详细信息,请参阅我过去的答案:

套接字编程中的业余问题 [ ^ ],

同一端口号码的多个客户端 [ ^ ]。





-SA
It's quite weird that you want to use multithreading and do two things in a single thread of a server side: listening for new connection and sending/receiving messages to/from a network stream. They certainly should be separate threads. For further detail, please see my past answers:
an amateur question in socket programming[^],
Multple clients from same port Number[^].


—SA


这篇关于TCP服务器&amp;客户端MultiThreaded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆