0

I have the following host

package clserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class Main {
//instance vars
static ServerSocket sSocket = null;
static int serverPort = 0;
static Socket cSocket = null;
static PrintWriter out = null;
static BufferedReader in = null;


public static void main(String[] args) throws IOException {
        System.out.println("\n\n\nTCP Server Client\n\nEnter port number:");
        Scanner scan = new Scanner(System.in);
        serverPort = scan.nextInt();
        try {
            //connect server to port
            sSocket = new ServerSocket(serverPort);
        } catch (IOException ex) {
            System.err.println("That port is busy");
        }
        try {
            //accept client connection
            cSocket = sSocket.accept();
        } catch (IOException ex) {
            System.err.println("Connection failed");
        }

        out = new PrintWriter(cSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));

        System.out.println(in.readLine());

      }
}

and this client code

package clclient;

import java.io.*;
import java.net.*;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Main {

    //instance vars
    static Socket cSocket =null;
static PrintWriter out = null;
static BufferedReader in = null;

//server info
static String serverName = null;
static int serverPort = 0;


public static void main(String[] args) {
    try {
        System.out.println("\n\n\nTCP Chat Client\nEnter server name:");
        Scanner scan = new Scanner(System.in);
        //get server info from user
        serverName = scan.nextLine();
        System.out.println("\nEnter port number:");
        serverPort = scan.nextInt();
        //make connection to server
        cSocket = new Socket(serverName, serverPort);
        out = new PrintWriter(cSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
    } catch (UnknownHostException ex) {
        System.err.println("\ncan't find that host\n");

    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    String nm = "testing";
    out.print(nm);


}

}

I am trying to send messages back and forth between them but when I send a message the host crashes. It throws the exception Java.net.SocketException:connection reset

4
  • ...and what exception does it throw when it crashes? Commented Feb 13, 2010 at 4:43
  • It throws the exception Java.net.SocketException:connection reset Commented Feb 13, 2010 at 4:46
  • What port are you using? Commented Feb 13, 2010 at 4:48
  • port 22222.just chose it randomly, just tried 21212 that didn't work as well Commented Feb 13, 2010 at 4:50

2 Answers 2

2

Nope. print() just sends the data. println() sends the data and a line terminator. readLine() blocks until a line terminator is received. So somewhere along the line you have to call println(), or send a line terminator some other way.

Sign up to request clarification or add additional context in comments.

Comments

1

I haven't used Java sockets for a while, but the following fixes it on my machine:

In the client, call out.println(nm) instead of out.print(nm).

I think it may have something to do with automatic flushing, where println autoflushes but print does not. Still, not sure off the top of my head why print would cause a Socket exception.

Edit: you really should be doing everything with the Sockets within a try, and have a finally that calls close() on the Sockets.

1 Comment

that actually does fix it on my machine too but I don't understand why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.