dcsimg
Socket Problem
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, July 31, 2001 01:14 AM

I am writing a client/server application using socket and URLConection. It works fine when a one-way communication takes place between the client and server. However, when I try to read and write simultaneously, either client or server goes into deep meditation (goes into waiting state). I am embedding my code here. Please tell me what is wrong with this code. Server Code while (true) { Socket sock = serv.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream())); while((input = br.readLine()) != null) { System.out.println("Input from client = "+ input); } br.close(); PrintWriter w = new PrintWriter(s   More>>

I am writing a client/server application using socket and URLConection. It works fine when a one-way communication takes place between the client and server. However, when I try to read and write simultaneously, either client or server goes into deep meditation (goes into waiting state).


I am embedding my code here. Please tell me what is wrong with this code.

Server Code

			
while (true) {
Socket sock = serv.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
while((input = br.readLine()) != null) {
System.out.println("Input from client = "+ input);
}
br.close();
PrintWriter w = new PrintWriter(sock.getOutputStream(), true);
w.println("Hi this is your server.");
w.close();
sock.close();
}//end while

Client Code

			
URLConnection con = url.openConnection();

con.setDoInput(true);
con.setDoOutput(true);

PrintWriter w = new PrintWriter(con.getOutputStream(), true);
w.println("Hello Server Uncle!");
w.close();

BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
while((input = br.readLine()) != null) {
System.out.println("Input = "+ input);
}
br.close();
   <<Less

Re: Socket Problem

Posted By:   Tim_Rohaly  
Posted On:   Tuesday, July 31, 2001 01:01 PM

You have lots of things wrong here. I suggest you
try reading the Java Tutorial networking section
(link on the right side of this page) then ask again
if you still can't get it.
About | Sitemap | Contact