• Post Reply Bookmark Topic Watch Topic
  • New Topic

Distinguish message which receive from socket.getInputStream()

 
Anh Duc
Greenhorn
Posts: 9
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,

I get bit problem, The problem is as follows:

At server socket, server receive data type as byte array:

If client send text message in one thread then server run well, but if client run >=2 thread concurrent to send text message then at server, the message were mixed, it mean clientThead1 send "ABC", clientThead2 send "XYZ" => server receive "AXBC" OR "AXYZ",... => It is not expected message.
How to solve this problem???

P/S: I have tested with server receive message as text, it work well:

But i can not use it, because server serve muti platform client, so i want to server use byte array to receive data
Thanks all,
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Tutorial chapter on Networking briefly touches on this, basically saying to create a separate socket per conversation.
In my opinion, Remote Method Invocation (RMI) is much easier to use than bare sockets. Java is Object Oriented. We shouldn't need to twiddle bits.
 
Anh Duc
Greenhorn
Posts: 9
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Ess wrote:The Java Tutorial chapter on Networking briefly touches on this, basically saying to create a separate socket per conversation.
In my opinion, Remote Method Invocation (RMI) is much easier to use than bare sockets. Java is Object Oriented. We shouldn't need to twiddle bits.


Thanks for your response,

But this is not answer i need
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anh Duc wrote:
But this is not answer i need


Is there a reason the solutions I proposed will not work? You haven't given us much information to go off of.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crossposted to stackoverflow.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative answer is to put a threadsafe queue in front of the socket. Each thread writes into the queue rather than the socket, and you have a single thread reading from the queue and writing to the socket.
 
Anh Duc
Greenhorn
Posts: 9
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:An alternative answer is to put a threadsafe queue in front of the socket. Each thread writes into the queue rather than the socket, and you have a single thread reading from the queue and writing to the socket.


Thanks for your response,
I will try to test with your solution,
Thanks very much,
 
Anh Duc
Greenhorn
Posts: 9
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:An alternative answer is to put a threadsafe queue in front of the socket. Each thread writes into the queue rather than the socket, and you have a single thread reading from the queue and writing to the socket.


Hi Sir,
I have applied message queue in client but server was still same problem,
what have you any solution for this???





 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't posted the code that calls those methods so I can't see exactly how your code will behave. I will make the following two points though...

1) There should only be one thread reading from the queue, because there must only be one thread writing to the socket. If your removeMessageQueue is called multiple times then program may behave in exactly the same way it did when you just had multiple threads writing to the socket.

2) the addMessageQueue method is creating a new thread that simply adds one message to the queue. What is the point of the new thread? It would be much more efficient and much less complicated to just add the message to the queue. I had assumed you already had multiple threads producing messages.
 
Anh Duc
Greenhorn
Posts: 9
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:You haven't posted the code that calls those methods so I can't see exactly how your code will behave. I will make the following two points though...

1) There should only be one thread reading from the queue, because there must only be one thread writing to the socket. If your removeMessageQueue is called multiple times then program may behave in exactly the same way it did when you just had multiple threads writing to the socket.

2) the addMessageQueue method is creating a new thread that simply adds one message to the queue. What is the point of the new thread? It would be much more efficient and much less complicated to just add the message to the queue. I had assumed you already had multiple threads producing messages.


Thanks for your response,
Maybe i will research new way to send and receive message at server,
Thanks you very much,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!