Sagar Tanwar
Sagar Tanwar

Reputation: 31

Java Client Server Socket related problem

import java.io.*;
import java.net.*;

public class Client {

    public static void main(String[] args) throws Exception {
        
        String ip = "localhost";
        int port = 5643;
        Socket s = new Socket(ip, port);
        String str = "Sagar Tanwar";
        OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
        PrintWriter pw = new PrintWriter(os);
        os.write(str);
        os.flush();
    }
}
import java.net.*;
import java.io.*;

public class Server {

    public static void main(String[] args) throws Exception {
        
        System.out.println("Server is started");
        ServerSocket ss = new ServerSocket(5643);
        
        System.out.println("Server is waiting");
        Socket s = ss.accept();
        
        System.out.println("Client Connected");
        
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String str = br.readLine();
        
        System.out.println("Client Data : "+str);
    }
}
Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:606)
        at java.net.Socket.connect(Socket.java:555)
        at java.net.Socket.<init>(Socket.java:451)
        at java.net.Socket.<init>(Socket.java:228)

Upvotes: 1

Views: 252

Answers (3)

Janez Kuhar
Janez Kuhar

Reputation: 4286

In Client.java change this line:

String str = "Sagar Tanwar";

to:

String str = "Sagar Tanwar\r\n";

Your server expects to read a full line (br.readLine()) and it continues to read until your client stays connected. Once the client disconnects, an exception is thrown because the server was unable to read a new line.

Side notes

On my system:

  • your client throws: java.net.ConnectException: Connection refused: connect - if the server is not active
  • your server throws: java.net.SocketException: Connection reset - if the server can't read a full line from the client

Upvotes: 2

ArmDuke
ArmDuke

Reputation: 108

Everything will work for you if you close the connections at the end of the client class.

    String ip = "localhost";
    int port = 5643;
    Socket s = new Socket(ip, port);
    String str = "Sagar Tanwar";
    OutputStreamWriter os = null;
    try {
        os = new OutputStreamWriter(s.getOutputStream());
        os.write(str);
        os.flush();
    } finally {
        if (os != null)
            os.close();
    }

or with "try with resources"

    String ip = "localhost";
    int port = 5643;
    Socket s = new Socket(ip, port);
    String str = "Sagar Tanwar";
    try (OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream())) {
        os.write(str);
        os.flush();
    }

Upvotes: -1

RAIMT TEAM
RAIMT TEAM

Reputation: 1

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws Exception {

        String ip = "localhost";
        int port = 5643;
        Socket s = new Socket(ip, port);
        String str = "Sagar Tanwar";
        OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
        PrintWriter pw = new PrintWriter(os);
        os.write(str);
        os.flush();
    }
}

Upvotes: -4

Related Questions