• Post Reply Bookmark Topic Watch Topic
  • New Topic

Messages from Client only are receiveds after the connection is dropped

 
Fernando Skackauskas
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I have a program (Server) in Netbeans that is in listening and is waiting for a message from another computer (Client) via socket.

However, the sent message is received and all commands are processed and displayed in Netbeans console only when the connection is dropped.

So I can not send back the message to the Client because the connection is no longer active.

The program should receive and send messages in real time, not after the broadcast was overthrown

What is wrong with the program?

How to fix it?

Thanks,

Fernando

Bellow the code:

Note: All commands inside the "while loop" are executed only after the broadcast ends

 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by broadcast, but if you want to be able to process multiple echo requests with a single connection then don't close the output stream after processing the first request.

The code is still quite clunky, but I think it demonstrates what you are wanting to do.
 
Fernando Skackauskas
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Ron Mcleod,

I ran the program as described by you.

I am testing the sending and receiving messages.

But it is happening a very strange thing.

When the program is running, it waits for a message by the command vetor = input.readLine()

But when I am sending the message, the program does not process.

The command vetor = input.readLine() will be processed only after the socket is destroyed.

But it should not be that way. I think that each message is send to the server, should just process exactly at the moment of sending message, and not after the socket is switched off.

I am following the bellow steps:

I connect to the server;

I send a message.

Nothing is processed and the message is not received.

After destroy the socket, the program performs the command vetor = input.readLine();

Why is this happening?

Thanks,

Fernando
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the server flush messages after they're written? Otherwise they may only be flushed when the connection closes.
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fernado -- I ran the code and tested using several clients (Putty on Windows, netcat on Linux, Linux telnet client) and the experience was as-expected. What client are you using -- could it be buffering your data?

Another thought - since your code is single threaded and only accepts one connection at a time, a second client trying to connect while the first one is already connect might experience behaviour similar to what you are describing. For example, if I use netcat to connect to the server while there is already a connection, the data that enter is buffered by the client until the connection is made, and then sent all at once:
# nc 169.254.1.200 2712
One
Two
Three
Four
<< other client disconnects >>
Return Message: One
Return Message: Two
Return Message: Three
Return Message: Four
 
Fernando Skackauskas
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sr. Ron McLeod.

I do not know what am I doing wrong. In fact, I am using a program - tcpipbuilder.exe That program request the IP and port number of a server and a client.

After, I write a message 1 and send to the program. At this point the program should return to the port that was sent the return message. The same procedures is done for message 2 and 3.

However, nothing is returned to the port and it is only shown in the console of netbeans after the socket is destroyed. And the messages arrive together in a unique line.

I made a print screen of the last action.

Please, I need to urgently find a solution to this problem.

thank you,

Fernando
page-7.jpg
[Thumbnail for page-7.jpg]
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fernando Skackauskas wrote:
When the program is running, it waits for a message by the command vetor = input.readLine()


Is your client sending a carriage return?
public String readLine() throws IOException
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

java.io.BufferedReader.readLine()
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your problem is with the client - it is probably not the best choice, and you are possibly using it incorrectly.

I tried the TCP/IP Builder application and it worked fine. This is what I did:
   - Enter Local IP and TCP port
   - Enter Server IP and TCP port
   - Click on Create Socket
   - Click on Connect
   - Type something in the Send data text area and then press the keyboard's Enter key
   - Click on Send

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!