Home arrow static arrow Java Programming [Archive] - problem in java network programming
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - problem in java network programming
This topic has 4 replies on 1 page.

Posts:3
Registered: 7/31/04
problem in java network programming  
Jul 31, 2004 9:19 AM



 
Hi, there,
It is the first time I post in this forum.
I am now working on a project in which a java client GUI program communicates with a server program written in C++. I used TCP in the communication. The communication procedure is: client sends a packet to server to say " He, this is xxx", and server reply with a packet : "hello, xxx". In my problem, the server program receives the hello packet from client, and it does reply a welcome packet. But the client program can not receive the packet. In fact the packet has arrived at the client side. I know this because if I stop the server program, the client can get the packet sent from server. The problem is that the client program can not be informed that there is a packet ready to be read.

I attach my client problem in the following, and I am very appreciate for your kind help.

Thanks
ping

try
{
sock = new Socket("130.203.65.90", 6789);
os = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
}
catch
.....

try
{

String iString;
String oString="hello, server, this is XXX";

os.println(oString);
os.flush();

while ((iString = is.readLine()) != null)
{
System.out.println("Receive message from server: ");
System.out.println(iString);
}
}
catch (IOException e)
{
System.err.println("I/O failed");
}

 

Posts:37,103
Registered: 3/30/99
Re: problem in java network programming  
Jul 31, 2004 9:28 AM (reply 1 of 4)



 
But the client
program can not receive the packet. In fact the packet
has arrived at the client side. I know this because if
I stop the server program, the client can get the
packet sent from server. The problem is that the
client program can not be informed that there is a
packet ready to be read.

It's not clear what you mean by this. "The client can't receive the packet, but the packet has arrived, but if I stop the server the client can get the packet"? That really doesn't make any sense. Can you explain more clearly what behavior you're seeing?

If it's just that the client doesn't seem to have received the packet until the server shuts down, then it sounds like either the server isn't sending a line terminator, or isn't sending the right line terminator, or isn't flushing its output stream. It's hard to tell based on what you said though.

When you post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read.
 

Posts:15
Registered: 12/20/99
Re: problem in java network programming  
Jul 31, 2004 9:39 AM (reply 2 of 4)



 
Sounds like you may need to explicitly flush the output from the server.
Or try putting the in and out of the client socket in its own thread.
 

Posts:47
Registered: 4/1/03
Re: problem in java network programming  
Jul 31, 2004 9:45 AM (reply 3 of 4)



 
method readLine() means that the server program must be sent the "\n" at the last string,like "hello, xxx\n"

fix your server program or the client try to use other method,like read() etc.
 

Posts:47
Registered: 4/1/03
Re: problem in java network programming  
Jul 31, 2004 10:07 AM (reply 4 of 4)



 
if you use the method of readLine() , it will alway reading until get the character "\n"

while( (s = instance.readLine() ) !=null ){ // go ahead if get the character "\n"

//do something

}

so , you have two ways to solve the problem

1 add the character "\n" to your server program
2 try and use other method in your client,like read()
 
This topic has 4 replies on 1 page.