1

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");
      }
   }
}
oers
  • 18,436
  • 13
  • 66
  • 75
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • Related: http://stackoverflow.com/questions/2126607/official-reasons-for-software-caused-connection-abort-socket-write-error – BalusC Jun 30 '10 at 16:57
  • Thanks - I think it must be something simpler in my case - the "server" always receives the first string I type and I get an exception on the second - it is not that I get mixed up my strings or anything – Mr_and_Mrs_D Jun 30 '10 at 17:02
  • @dardana: It's very hard to know what's going on with no idea of what the server is. – Jon Skeet Jun 30 '10 at 17:11
  • Thanks for reading - posting the server – Mr_and_Mrs_D Jun 30 '10 at 17:18
  • Unrelated to your problem, but nevertheless: you should use `DataOutputStream` only if you intend to serialize Java types. If you just want a output streams to write bytes, just use the OutputStream returned by `cSocket.getOutputStream()`. – leonbloy Jun 30 '10 at 18:38
  • @leonbloy: That's not true. That's what an ObjectOutputStream is for. But you do note an absolutely critical error. DataInput and OutputStream **MUST** be used symmetrically. If you write with a DataOutputStream, you must read with a DataInputStream. What @dardana is doing is writing using writeBytes and reading using readLine. What you should be doing instead is using `PrintWriter.println(String)` to write a string and `BufferedReader.readLine()` to read a string. – Mark Peters Jul 01 '10 at 14:24
  • I replaced all ''DataInputStream with Print Writer - like PrintWriter outToServer = new PrintWriter(cSocket.getOutputStream());'' and outToServer.writeBytes(...); with outToServer.println(...); but nothing is displayed in server (??) – Mr_and_Mrs_D Jul 02 '10 at 13:34

3 Answers3

4

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
  • 80,126
  • 17
  • 159
  • 190
  • Thanks indeed - I am a newbie and it shows :) I'll try what you say – Mr_and_Mrs_D Jun 30 '10 at 17:45
  • 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 Jun 30 '10 at 19:00
1

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
  • 8,355
  • 7
  • 29
  • 38
0

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
  • 3,623
  • 9
  • 35
  • 53
  • 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 Jul 01 '10 at 06:36