简体   繁体   中英

java DataOutputStream exception

The exception is thrown in line 24 the second time I type something (after I have typed the host name) - server works right. Code

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

class TCPclient {
    public static void main(String[] args) throws Exception {
        String hostname, msg;
        InetAddress hostaddress;
        BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in));
        System.out.println("Please type host\n");
        hostname = inFromUser.readLine();   //I type localhost
        hostaddress = InetAddress.getByName(hostname);
        Socket cSocket = new Socket(hostaddress, 44444);
        String cAddress = cSocket.getLocalSocketAddress().toString();
        DataOutputStream outToServer = new DataOutputStream (cSocket.getOutputStream());
        while (true)
        {
            msg = inFromUser.readLine();
            System.out.println(msg);
            if (msg.equals("exit"))
            {
                System.out.println("exit");
                break;
            }
            outToServer.writeBytes(cAddress + " said : " + msg + '\n'); //this line throws an exception the second time it runs
        }
        cSocket.close();
    }
}

I am new in java so I am missing something obvious I guess. Exception reads :

Exception in thread "main" java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) at java.net.SocketOutputStream.write(SocketOutputStream.java:115) at java.io.DataOutputStream.writeBytes(DataOutputStream.java:259) at TCPclient.main(TCPClient.java:52) Java Result: 1

Server :

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

class TCPServer {
   public static void main(String argv[]) throws Exception {
      String clientSentence;
      ServerSocket welcomeSocket = new ServerSocket(44444);
      while(true) {
         Socket connectionSocket = welcomeSocket.accept();
         BufferedReader inFromClient = new BufferedReader(
                new InputStreamReader(connectionSocket.getInputStream( ) ) );
         clientSentence = inFromClient.readLine();
         System.out.println(clientSentence + "\n");
      }
   }
}

Your client creates one socket and writes over and over again to that one socket. Your server, on the other hand, does this:

ServerSocket welcomeSocket = new ServerSocket(44444);
while(true) {
   Socket connectionSocket = welcomeSocket.accept();

That accepts the incoming connection, reads one line, and then abandons it (and I'm guessing on the socket's finalize when being garbage collected it closes the connection). Then it waits for a new connection.

So to fix your immediate problem, try moving

    Socket connectionSocket = welcomeSocket.accept();
    BufferedReader inFromClient = new BufferedReader(
            new InputStreamReader(connectionSocket.getInputStream( ) ) );

before the while loop.

How long do you wait between typing second line? It might have something to do with socket being idle.

Also with the server code like this you will see only first message. Try this:

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

class TCPServer {
    public static void main(String argv[]) throws Exception {
        String clientSentence;
        ServerSocket welcomeSocket = new ServerSocket(44444);
        Socket connectionSocket = welcomeSocket.accept();
        BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        while (true) {
            clientSentence = inFromClient.readLine();
            System.out.println(clientSentence + "\n");
        }
    }
}

Try:

while (true)
{
    if(inFromUser.readLine() != null)
    {        
        msg = inFromUser.readLine();

        System.out.println(msg);
        if (msg.equals("exit"))
        {
           System.out.println("exit");
           break;
        }

        outToServer.writeBytes(cAddress + " said : " + msg + "\n");
    }
}

Note the changes:

if(inFromUser.readLine() != null)
{ 

and

... "\n"); 

not

... '\n');

Give it a shot. It's probably too simple a solution, but it's something :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM