tags:

views:

65

answers:

2
+1  Q: 

Client/Server app

Guys could anyone tell me what's wrong am I doing here? Below are four files Client, main, Server, main. I'm getting an error on Client side after trying to fromServer.readLine().
Error: Error in Client Software caused connection abort: recv failed

            package client;
            import java.io.*;
            import java.net.*;
            import java.util.Scanner;

            public class Client
            {
            private PrintWriter toServer;
            private BufferedReader fromServer;
            private Socket socket;
            public Client( )throws IOException
                {
                socket = new Socket("127.0.0.1",3000);
            }
            public void openStreams() throws IOException
                {

            //
            //        InputStream is = socket.getInputStream();
            //        OutputStream os = socket.getOutputStream();
            //        fromServer = new BufferedReader(new InputStreamReader(is));
            //        toServer = new PrintWriter(os, true);

                toServer = new PrintWriter(socket.getOutputStream(),true);
                fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            }
            public void closeStreams() throws IOException
                {
                fromServer.close();
                toServer.close();
                socket.close();
            }
            public void run()throws IOException
                {
                openStreams();
                String msg = "";
                Scanner scanner = new Scanner(System.in);
                toServer.println("Hello from Client.");
            //    msg = scanner.nextLine();
                while (msg != "exit")
                {
                    System.out.println(">");
            //        msg = scanner.nextLine();
                    toServer.println("msg");
                    String tmp = fromServer.readLine();
                    System.out.println("Server said: " + tmp);
                }
                closeStreams();
            }
            }



        package server;
        import java.net.*;
        import java.io.*;

        public class Server
        {
        private ServerSocket serverSocket;
        private Socket socket;
        private PrintWriter toClient;
        private BufferedReader fromClient;
        public void run() throws IOException
            {
            System.out.println("Server is waiting for connections...");
            while (true)
            {
                openStreams();
                processClient();
                closeStreams();
            }

        }
        public void openStreams() throws IOException
            {
            serverSocket = new ServerSocket(3000);
            socket = serverSocket.accept();
            toClient = new PrintWriter(socket.getOutputStream(),true);
            fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }
        public void closeStreams() throws IOException
            {
            fromClient.close();
            toClient.close();
            socket.close();
            serverSocket.close();
        }
        public void processClient()throws IOException
            {
            System.out.println("Connection established.");
            String msg = fromClient.readLine();
            toClient.println("Client said " + msg);
        }
        }


    package client;

    import java.io.IOException;


    public class Main {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here

            try
            {
                 Client client = new Client();
                 client.run();
            }
            catch(IOException e)
            {
                 System.err.println("Error in Client " + e.getMessage());
            }
        }

    }



package server;

import java.io.IOException;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Server server = new Server();
        try
        {
        server.run();
        }
        catch(IOException e)
        {
            System.err.println("Error in Server " + e.getMessage());
        }
    }

}
+1  A: 

Your server side is all mixed up. You need another class, call it Connection, or ConnectedClient, that implements Runnable; takes a Socket as a constructor parameter; and does all the server-side I/O for a particular client. At present you have this all mixed up with your Server class. All that your Server class should do is create a ServerSocket, and enter a loop accepting Sockets and creating Connection objects and starting threads for them. The Socket object in the Server's run() method is just a local object, and the associated input/output streams or readers/writers belong in the Connection class, not the Server class.

So that way you can handle multiple clients simultaneously without them treading on each other's toes.

Your next issue is that readLine() returns null when the other end has closed the connection. You must test this first before doing any other processing every time you call readLine().

Next, any time you get a SocketException or IOException other than SocketTimeoutException when using a Socket it is dead - close it, forget about that client and exit the thread if it happens in the server.

EJP
+2  A: 

The code as posted is running in an infinite loop on the client side. After guessing which line to uncomment (last scanner.nextLine() in Client.java), I've noticed the following problems:

  1. the client keeps the connection open, but the server closes after receiving the first message.
  2. the predicate msg != "exit" is always true in Java - use String.equals()

A working Server.processClient is:

public void processClient()throws IOException
{
    String msg = null;
    do {
        msg = fromClient.readLine();
        toClient.println("Client said " + msg);
    } while (!"exit".equals(msg));
}

A working Client.run is:

public void run()throws IOException
{
    openStreams();
    String msg = "";
    Scanner scanner = new Scanner(System.in);
    while (!"exit".equals(msg))
    {
        System.out.print(">");
        msg = scanner.nextLine();
        toServer.println(msg);
        String tmp = fromServer.readLine();
        System.out.println("Server said: " + tmp);
    }
    closeStreams();
}

+Comment: I've concentrated on correcting the obvious errors. EJP's answer gives you good guidelines how to make your code better, including handling simultaneous connections, proper use of threads, etc.

Miklos