n0ob
n0ob

Reputation: 1285

java tcp client and server

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

Upvotes: 0

Views: 1600

Answers (2)

user207421
user207421

Reputation: 311050

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.

Upvotes: 2

Kaleb Brasee
Kaleb Brasee

Reputation: 51965

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.

Upvotes: 1

Related Questions