M
M
Meridian3122014-06-17 21:58:45
Java
Meridian312, 2014-06-17 21:58:45

How to organize a socket handler with multitasking?

I read the Internet, but still need an explanation. I am a student. The description in brackets is purely symbolic.
What I'm trying to understand and do:
There is a server class (Server) that, when receiving a socket, delegates connection handling to (Server.Worker). The worker is in a loop and constantly waits for a new object from the client and then responds according to the rule. There are no problems with obtaining objects. In addition to exchanging objects, I need to implement small file transfers and would like to do this using the same (Worker.Socket.OutputStream) socket (Worker.Socket) that the Worker handles.
I don't know if it's possible to create a child-thread (Worker.CurrentThread.Fork) and have it pass file bytes without preventing it from sending objects to client requests in parent-thread (Worker.CurrentThread)?
If this is not possible, then please tell me how can I implement a handler so that it can respond to the client and send the file at the same time? For these purposes, on the client, I need to open 2 sockets (Object Exchange, File Transfer) and forward a special object that could describe the transfer of files, followed by delegating the task to child-thread (Worker.CurrentThread.Fork) for a new socket? If so, then how can the server associate these 2 sockets as one client?
PS: I would like to give preference to the standard library, but do not mind looking at other options.
Any recommendations about the architecture will be only a plus.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-06-18
@Meridian312

If you want to multiplex the transfer of different data into one connection, then you will need to develop your own TCP-based protocol, form your own packets, marking what it is, data or pieces of a file, what data it is, what files it is, etc. And it doesn't matter if you write from one stream or from different ones. The essence remains the same, you on the client should be able to share the data that comes to you.
If you do not have any special requirements, then it is easier to make a connection for each action (receiving / downloading a file, transferring data, etc.).
ps Do not be afraid of phrases like "to develop a protocol." It's not that hard, actually. You only need to define the format of the header, that is, the usual structure, in which, for simplicity, the length of the packet (excluding the header), and the type of it (what it contains) should be indicated. It's minimum.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question