views:

71

answers:

3

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

Thanks for any help !

PS 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");
      }
   }
}
+4  A: 

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.

Mark Peters
Thanks indeed - I am a newbie and it shows :) I'll try what you say
Yes - works !!!So the connection was closed - that's why I got the exception ?
That's right. The server endpoint abandoned the connection and stopped reading from it. Recall that a server socket has the ability to accept incoming connections from *multiple* client sockets. Once you've accepted a connection through `accept`, however, the socket retrieved is persistent (through the magic of TCP/IP). There is no need to reestablish the connection every time you want to send data down the line.
Mark Peters
Thanks ! clear now :)
A: 

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");
        }
    }
}
Georgy Bolyuba
Thank you very much - I will try your suggestions immediately - thanks
Works - thanks - it is not the socket being idle but the connection closing (???)
I tend to agree with Mark Peters: You simply drop all the references to the server socket after receiving first message. At some point socket will be closed and you will get the exception.
Georgy Bolyuba
A: 

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

Justian Meyer
Sending second line of every two lines and converting Character literal to String literal will not help the socket problem I am afraid.
Georgy Bolyuba