About java socket programming
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mynx_Mynx
Posted On:   Sunday, February 3, 2002 01:49 AM

I meet a problem to program a Server/Client application by socket, I use the following code in Server: BufferedReader socketReader= new BufferedReader(new InputStreamReader(client.getInputStream())); //Client is returned by accept() String line = null; while ((line = socketReader.readLine()) != null) { System.out.println(line); } This is a simple program, Client send some string, and Server receive them. But when do the last readLine(),It should renturn null and the loop end.When i debug it,this method is blocked and not return.And application is also blocked.But if Server send string and Client receive,code is same.This time,readLine()return null and loop end .All   More>>

I meet a problem to program a Server/Client application by socket, I use the following code in Server:


BufferedReader socketReader=
new BufferedReader(new InputStreamReader(client.getInputStream()));


//Client is returned by accept()


String line = null;


while ((line = socketReader.readLine()) != null)


{


System.out.println(line);


}


This is a simple program, Client send some string, and Server receive them.
But when do the last readLine(),It should renturn null and the loop end.When i debug it,this method is blocked and not return.And application is also blocked.But if Server send string and Client receive,code is same.This time,readLine()return null and loop end .All things seem properly.
What is matter and how to know that the input stream is over.I means that flags,such as "over" should not be used .Can anybody help me!Thanks    <<Less

Re: About java socket programming

Posted By:   Krzysztof_Raciniewski  
Posted On:   Monday, February 4, 2002 05:05 AM

May be you have problem with BufferedReader. There is a
bug
, you can overwrite the readLine method.

Re: About java socket programming

Posted By:   Duncan_Forster  
Posted On:   Sunday, February 3, 2002 02:32 AM

Network protocols normally store the size of the data somehow. For example length encode your data. What I mean is on the client side turn your string into a byte array, get the length and then write the int value and the byte array to the socket. On the server side read int value from socket, create new byte array the size of int value, then read all the data into the array and then convert to string.
About | Sitemap | Contact