Вопрос

I am new to socket programming in java so facing a problem seems not difficult but unable to solve due to unfamiliarity. Following are codes for Client and Server.

Server Code:

public class connectionServer {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    String clientSentence;
    String capitalizedSentence;
    BufferedReader inFromClient;
    DataOutputStream outToClient;
    BufferedReader inFromUser;

    try {
        ServerSocket welcomeSocket = new ServerSocket(70);
        Socket connectionSocket;
        connectionSocket = welcomeSocket.accept();
        System.out.println("Connection accepted for " + connectionSocket.getInetAddress() + ": " + connectionSocket.getPort());
        InputStreamReader input = new InputStreamReader(connectionSocket.getInputStream());
        inFromClient = new BufferedReader(input);



        while (true) 
        {        

            if(input.ready())
            {
                clientSentence = inFromClient.readLine();                
                System.out.println(clientSentence);
            }

            String tempString = "FROM SERVER: What's problem......";

            try
            {
                Thread.currentThread().sleep(1000);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }

            outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            if(outToClient != null)
            {
                outToClient.writeBytes(tempString + "\n");
                outToClient.flush(); 

            }
        }
    } 
    catch (IOException e) 
    {
        System.out.println(e);
        e.printStackTrace();
    }
}

}

Client Code:

public class connectionClient {

static Socket clientSocket = null;
//////////////////////////////////////////

//////////////////////////////////////////
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    String sentence;
    String modifiedSentence;
    attachShutDownHook();
    try
    {
        clientSocket = new Socket("127.0.0.1", 70);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());;
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        outToServer.writeBytes("FROM CLIENT 2: Hello \n\n");

        while(clientSocket.isConnected())
        {               

            sentence = "Please reply me....";
            try
            {
                Thread.currentThread().sleep(1000);
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
            if(inFromServer.ready())
            {
                modifiedSentence = inFromServer.readLine();
                System.out.println(modifiedSentence);
            }
            outToServer.writeBytes("FROM CLIENT 2:" + sentence + "\n");
            outToServer.flush();
        }

    }
    catch(IOException e)
    {
        System.out.println(e);
    }
    finally
    {
        System.exit (0) ;
    }
}

}

When I stop the client following exception is thrown on server side

    java.net.SocketException: Connection reset by peer: socket write error
    java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:132)
at java.io.DataOutputStream.writeBytes(DataOutputStream.java:276)
at connectionserver.connectionServer.main(connectionServer.java:57)

Any help in this regard will be highly appreciated. Thanks

Это было полезно?

Решение

This usually means you have written to an connection that had already been closed by the peer. In other words, an application protocol error.

Your code needs work.

  1. Don't use ready(): just block in read() until data arrives. At present you're smoking the CPU while ready() returns false. This is probably causing the error.

  2. isConnected() is always true after you connect the socket. It won't magically return false when the peer closes his end. You have to detected that via EOS or an exception. Looping on while (isConnnected()) isn't valid.

  3. Don't mix streams and readers and writers. You're using a BufferedInputStream: use a BufferedWriter at the other end.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top