Mr_and_Mrs_D
Mr_and_Mrs_D

Reputation: 34076

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

Upvotes: 1

Views: 4081

Answers (3)

Justian Meyer
Justian Meyer

Reputation: 3703

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

Upvotes: 0

Georgy Bolyuba
Georgy Bolyuba

Reputation: 8541

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

Upvotes: 1

Mark Peters
Mark Peters

Reputation: 81154

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.

Upvotes: 4

Related Questions