Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to write a socket program where a string is sent to the server, reversed and the reversed string is sent back to the client.

Here's my server code:

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

class ClientSystem
{
    public static void main(String[] args)
    {
            String hostname = "127.0.0.1";
            int port = 1234;

            Socket clientsocket = null;
            DataOutputStream output =null;
            BufferedReader input = null;

            try
            {
                    clientsocket = new Socket(hostname,port);
                    output = new DataOutputStream(clientsocket.getOutputStream());
                    input = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
            }
            catch(Exception e)
               {
                    System.out.println("Error occured"+e);
            }

            try
            {
                    while(true)
                    {
                            System.out.println("Enter input string ('exit' to terminate connection): ");
                            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                            String inputstring = br.readLine();
                            output.writeBytes(inputstring+"\n");

                            //int n = Integer.parseInt(inputstring);
                            if(inputstring.equals("exit"))
                                    break;

                            String response = input.readLine();
                            System.out.println("Reversed string is: "+response);

                    }

                    output.close();
                    input.close();
                    clientsocket.close();

            }
            catch(Exception e)
            {
                    System.out.println("Error occured."+e);
            }
            /*finally
            {
                    output.close();
                    input.close();
                    clientsocket.close();
            }*/

    }
}

Here's my server code:

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

public class ServerSystem
{
    ServerSocket server = null;
    Socket clientsocket = null;
    int numOfConnections = 0, port;

    public ServerSystem(int port)
    {
            this.port = port;
    }

    public static void main(String[] args)
    {
            int port = 1234;
            ServerSystem ss = new ServerSystem(port);
            ss.startServer();
    }

    public void startServer()
    {
            try
            {
                    server = new ServerSocket(port);
            }
            catch(Exception e)
            {
                    System.out.println("Error occured."+e);
            }

            System.out.println("Server has started. Ready to accept connections.");

            while(true)
            {
                    try
                    {
                            clientsocket = server.accept();
                            numOfConnections++;
                            ServerConnection sc = new ServerConnection(clientsocket, numOfConnections, this);
                            new Thread(sc).start();
                    }
                    catch(Exception e)
                    {
                            System.out.println("Error occured."+e);
                    }
            }
    }

    public void stopServer()
    {
            System.out.println("Terminating connection");
            System.exit(0);
    }
}

class ServerConnection extends Thread
{
    BufferedReader br;
    PrintStream ps;
    Socket clientsocket;
    int id;
    ServerSystem ss;

    public ServerConnection(Socket clientsocket, int numOfConnections, ServerSystem ss)
    {
            this.clientsocket = clientsocket;
            id = numOfConnections;
            this.ss = ss;

            System.out.println("Connection "+id+" established with "+clientsocket);
            try
            {
                    br = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
                    ps = new PrintStream(clientsocket.getOutputStream());
            }
            catch(Exception e)
            {
                    System.out.println("Error occured."+e);
            }
    }

    public void run()
    {
            String line;
            try
            {
                    boolean stopserver = false;
                    while(true)
                    {
                            line = br.readLine();
                            System.out.println("Received string: "+line+" from connection "+id);
                            long threadID = Thread.currentThread().getId();
                            System.out.println("Thread ID: "+threadID+" is doing the current task.");

                            if(line.equals("exit"))
                            {
                                    stopserver = true;
                                    break;
                            }
                            else
                            {
                                    int len = line.length();
                                    String reversedstring = "";
                                    for (int i=len-1; i>=0; i--)
                                            reversedstring = reversedstring + line.charAt(i);
                                            ps.println(""+reversedstring);

                            }
}
                    System.out.println("Connection "+id+" is closed.");
                    br.close();
                    ps.close();
                    clientsocket.close();

                    if(stopserver)
                            ss.stopServer();
            }
            catch(Exception e)
            {
                    System.out.println("Error occured."+e);
            }
    }
}

I'm trying to open two clients. When I type "exit" in one of the clients (say client1), the server itself is terminating. But I don't want the server to close but just the connection to client1 to close. When I next type a string in client2, I get "java.net.SocketException: Connection Reset" .

How do I get rid of the exception and the connection at server be still open for client2? Can anyone please help me out here? Thank you in advance! :)

share|improve this question

1 Answer 1

It's your code:

while(true){
    ...
    if(line.equals("exit"))
    {
            stopserver = true;
            break;
    }
    ...
}
...
if(stopserver)
ss.stopServer();
share|improve this answer
    
What changes do i need to make? –  user2201650 Oct 11 '13 at 3:20
    
Use another command (for example bye)to exit the server –  crybird Oct 11 '13 at 3:24
    
But it would still give the same exception. –  user2201650 Oct 11 '13 at 3:29
    
Can anyone help me out here? –  user2201650 Oct 11 '13 at 4:08
    
After changing you server code, sure that the server still running after client1 exits? –  crybird Oct 11 '13 at 5:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.