Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Client blocks!

Thread view: 
eksamor@yahoo.com - 18 Sep 2006 10:24 GMT
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 - 18 Sep 2006 11:39 GMT
> 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

Signature

[ 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 - 18 Sep 2006 11:59 GMT
Gordon Beaton skrev:

> > My client block after typing the first input...any ideas?
>
[quoted text clipped - 7 lines]
>
> /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 - 18 Sep 2006 12:14 GMT
> 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

Signature

[ 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.