[英]Multithreaded Server Client in MyBatis
I have coded a single threaded client server model which does following: 我已经编写了一个单线程客户端服务器模型,该模型执行以下操作:
Now I want to convert this server to multithreaded one and I am really confused looking at some examples that I found while googling (ie chat server etc.). 现在,我想将此服务器转换为多线程服务器,而我在查看谷歌搜索时发现的一些示例(即聊天服务器等)时确实感到困惑。
So, can anyone please help me out where exactly to start from to convert server into a multithreaded one? 因此,有人可以帮助我将服务器转换为多线程服务器的确切起点吗?
I can post the code here if required. 如果需要,我可以在此处发布代码。
You could start a thread for every incoming connection. 您可以为每个传入的连接启动线程。 At the bottom of this page you can find an example with source code: Writing the Server Side of a Socket
在此页面的底部,您可以找到带有源代码的示例: 编写套接字的服务器端
As suggested by Gille, you can write a class that extends the Class Thread. 正如Gille所建议的,您可以编写一个扩展Class Thread的类。 This thread, started by the Server class, is listening for connections, available on the port you setted.
由Server类启动的此线程正在侦听您设置的端口上可用的连接。
For example, a thread is listening for connection on port 7000, another thread on port 7500, and so on. 例如,一个线程正在侦听端口7000上的连接,另一个线程正在侦听端口7500上的连接,依此类推。
Obviously you have to import the Socket package in the Thread class, to send/receive data between client and server. 显然,您必须在Thread类中导入Socket包,才能在客户端和服务器之间发送/接收数据。 Read this page for further information about the Socket package.
阅读此页面以获取有关Socket软件包的更多信息。
This is the page API for Thread Class for JavaSE 6. 这是JavaSE 6的线程类的页面 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.