For Programmers: Free Programming Magazines  


Home > Archive > Java Help > September 2006 > Client blocks!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Client blocks!
eksamor@yahoo.com

2006-09-18, 4:02 am

My client block after typing the first input...any ideas?


import java.io.*;
import java.net.*;


public class Server {

public static void main(String[] args) {

ServerSocket serverSocket = null;
Socket ss = null;
BufferedReader in = null;
PrintWriter out = null;


try {
serverSocket = new ServerSocket(4444);

ss = serverSocket.accept();
in = new BufferedReader(new
InputStreamReader(ss.getInputStream()));
out = new PrintWriter(ss.getOutputStream());

String bob = null;

while ((bob = in.readLine()) != null)
{
out.println(bob + "from server");
System.out.println(bob);
}
serverSocket.close();
}

catch (IOException e) {
e.printStackTrace();
}
}

}





import java.io.*;
import java.net.*;

public class Client {

public static void main(String[] args) {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader user = null;
try
{
String fromServer, fromUser;

// Connect to server.
socket = new Socket("localhost",4444);

// Out channel.
out = new PrintWriter(socket.getOutputStream());

// In channel.
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

// Input from user.
user = new BufferedReader(new InputStreamReader(System.in));

while ((fromServer = in.readLine()) != null)
{
System.out.println(fromServer);
fromUser = user.readLine();

if (fromUser != null)
{
out.println(fromUser);
}
}


}catch(Exception e)
{
System.out.println(e.getMessage());
}
}

}

Gordon Beaton

2006-09-18, 8:01 am

On 18 Sep 2006 02:24:55 -0700, eksamor@yahoo.com wrote:
> My client block after typing the first input...any ideas?


Your server is waiting for input from the client.

Your client reads input from the user, does nothing with it, then
waits for input from the server.

In other words, both client and server are waiting for each other to
send something that will never arrive.

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
eksamor@yahoo.com

2006-09-18, 8:01 am


Gordon Beaton skrev:

> On 18 Sep 2006 02:24:55 -0700, eksamor@yahoo.com wrote:
>
> Your server is waiting for input from the client.
>
> Your client reads input from the user, does nothing with it, then
> waits for input from the server.
>
> In other words, both client and server are waiting for each other to
> send something that will never arrive.
>
> /gordon



Ok I have now changed the while loop in the client to:

while ((fromUser = user.readLine()) !=null)
{
System.out.println("test");
out.println(fromUser);


fromServer = in.readLine();
System.out.println(fromServer);

}

and the while loop in the Server to:

String fromClient = null;
while ((fromClient = in.readLine()) != null)
{
System.out.println("read from in channel");
out.println(fromClient + " received at server!");
System.out.println(fromClient);
}

now the procedure is as follows:

1) Server starts and block until Client connects (accept() blocks)
2) Client connects and blocks until user types input.
3) Server continues to while loop and blocks until there is data in the
in channel.
4) A client types "bong" and "test" gets printed and "bong" get send to
the server. The it block in the line "fromServer = in.readLine();"
5) Now the server should stop blocking since "bong" is in its in
channel, but it does not!

the while loop never gets executed on the server.

Gordon Beaton

2006-09-18, 8:01 am

On 18 Sep 2006 03:59:21 -0700, eksamor@yahoo.com wrote:
> 4) A client types "bong" and "test" gets printed and "bong" get send
> to the server. The it block in the line "fromServer =
> in.readLine();"


Confirm using Ethereal or similar tool, that the data really gets sent
to the server.

Try doing out.flush(), or setting "autoFlush" when you create the
PrintWriters (at both ends of the connection).

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Andrew Thompson

2006-09-18, 10:03 pm

eksamor@yahoo.com wrote:
> I have this simple client/server application,...


Please refrain from multi-posting.

(X-post to c.l.j.p./h. w/ f-u to c.l.h.help only.)

Andrew T.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com