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 / General / September 2006

Tip: Looking for answers? Try searching our database.

Client blocks!

Thread view: 
eksamor@yahoo.com - 18 Sep 2006 10:23 GMT
I have this simple client/server application, but for some reason the
client blocks after typing the first input (of course the server is
running an the client is connected):

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);

            System.out.println("CRAP1");
            ss = serverSocket.accept();
            System.out.println("CRAP2");
            in = new BufferedReader(new
InputStreamReader(ss.getInputStream()));
            out = new PrintWriter(ss.getOutputStream());

            String bob = null;

            while ((bob = in.readLine()) != null)
            {
                System.out.println("CRAP3");
                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());
        }
    }

}
Martin Lansler - 18 Sep 2006 11:29 GMT
Hi,

The basic problem is that you are trying to read a line of text from
both the server and client at the same time without sending a line
first. I.e server starts by waiting for a line of text and the client
also starts by waiting for a line of text, hence it hangs...

I modified the programs slighly so that the client starts by sending a
line of text until the user enters a null or empty line, in which case
the client closes the stream and socket. In this case the server will
also close down, in a real application you probably want to start a new
worker thread for each acceted client connection....

Regards,
Martin Lansler

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 (out != null) {
               fromUser = user.readLine();

               if (fromUser != null && !"".equals(fromUser)) {
                   out.println(fromUser);
                   out.flush();
                   fromServer = in.readLine();
                   System.out.println(fromServer);
               } else {
                   out.close();
                   out = null;
                   socket.close();
               }
           }

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

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);

           System.out.println("CRAP1");
           ss = serverSocket.accept();
           System.out.println("CRAP2");
           //
           in = new BufferedReader(new
InputStreamReader(ss.getInputStream()));
           out = new PrintWriter(ss.getOutputStream());

           String bob = null;

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

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

}

> I have this simple client/server application, but for some reason the
> client blocks after typing the first input (of course the server is
[quoted text clipped - 85 lines]
>
> }
Andrew Thompson - 18 Sep 2006 23:39 GMT
> 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.


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.