Multithreaded Server Client in MyBatis
I have coded a single threaded client server model which does following:
- Server loops for client waiting..
- When client runs, it sends current data available (i.e. a string which has 10 fields seperated by comma)
- Server reads the data from client and decodes it (simply by checking for comma)
- Then using myb开发者_如何学Goatis server updates database.
Now I want to convert this server to multithreaded one and I am really confused looking at some examples that I found while googling (i.e. 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. This thread, started by the Server class, is listening for connections, available on the port you setted.
For example, a thread is listening for connection on port 7000, another thread on port 7500, and so on.
Obviously you have to import the Socket package in the Thread class, to send/receive data between client and server. Read this page for further information about the Socket package.
This is the page API for Thread Class for JavaSE 6.
Tutorial O'Reilly for threads here.
精彩评论