V
V
Victor2015-02-15 19:53:55
Java
Victor, 2015-02-15 19:53:55

How to correctly distribute the simultaneous receipt of data on the Socket?

Faced here with such problem. There is a server and n-number of clients. Each client can request some information from the server, for example, the current time. But, besides this, the server itself sends time to the client every 10 minutes. A few more details, the client sends a request - and the server sends the time in response to it.
But what happens when the client sends a request to the server and at the same time the server sends its 10-minute broadcast?
It turns out the following - the server can try to send data on request and broadcast in one packet (TCP / IP) - the result will be time, which is duplicated and one more nuance remains. The client then expects only one line, for example, and wants to receive only one of it, but several come.

I already thought that it is worth allocating a new connection for mailing, but it seems to me that it is too resource-intensive.
Help advice please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Victor, 2015-03-02
@davinctor

The answer turned out to be simple. No way. Data cannot be obtained from the same connection at the same time, because each connection has only one input stream and one output stream.
The solution turned out to be this - both the weather distribution and simple server responses were sent with a special command prefix (plain text, for example, a normal response for a client request - response: Response text; and the distribution began with delivery: Mailing response text). I read the answers in two places - the place for waiting for the mailing and the place for waiting for a simple response to the client's command, but the processing took place in one place . That is, I parsed my answer in one place. And I have two listeners. Thanks everyone for the replies.

A
Armenian Radio, 2015-02-15
@gbg

Strictly speaking, in TCP there is no concept of a single packet - it refers to a higher level of application interaction. TCP provides the ability to transmit a stream of bytes . Yes, inside TCP this stream is broken up and transmitted inside packets, but the application should not get into this process.
The programmer can organize the breakdown of messages into their own packages for sending. The format of such a packet is a minimal header

struct mheader
{
    uint32_t type;
    uint32_t data;
};
and data immediately after it.
If the classical architecture of a multi-threaded server is used (each connection has its own thread), it will not be difficult to organize this.
Good Book - Jon Sneijder, Effective TCP/IP Programming
1004494083.jpg

D
dtestyk, 2015-02-15
@dtestyk

Judging by the question, the data is duplicated. You can try the options: -
read only the first line where it is not designed for more
- enter a timeout for any server sending to one client (if the data is not completely duplicated, you can aggregate them during the timeout)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question