HAKIM IRFAN
HAKIM IRFAN

Reputation: 11

Why the socket is always close when i send the data in java TCP

server class : the program is about to receive data from client and then reply. the server is okay..., but the problem is when the server send the reply to the client. the client always says 'the socket is close'. i try to delete secket close statement but the result is same.., the output says that 'the socket is close'. so please help me to solve this problem...

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

 public class Nomor3Server {

    public static final int SERVICE_PORT = 2020;

public static void main(String[] args) {
    try {
        ServerSocket server = new ServerSocket(SERVICE_PORT);
        System.out.println("DAytime service started");

        for (;;) {
            Socket nextClient = server.accept();

            BufferedReader pesan = new BufferedReader(new InputStreamReader(nextClient.getInputStream()));
            String messageIn = pesan.readLine();
            System.out.println("Received request from "
                    + nextClient.getInetAddress() + " : "
                    + nextClient.getPort()
                    + "\nIsi Pesan : " + messageIn);
            String messageOut = "انا لا ادر";

            switch (messageIn) {
                case "saya":
                    messageOut = "أنا";
                    break;
                case "kamu":
                    messageOut = "أنت";
                    break;
                default:
                    break;

            }

            OutputStream out = nextClient.getOutputStream();
            PrintStream pout = new PrintStream(out);

            pout.print(messageOut);

            out.flush();
            out.close();
            System.out.println("Message sent");
            //nextClient.close();
        }
    } catch (BindException e) {
        System.err.println("Server Already Running on port : " + SERVICE_PORT);
    } catch (IOException ioe) {
        System.err.println("error : " + ioe);

    }
}

}

client class :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;


public class Nomor3Client {

    public static final int SERVICE_PORT = 2020;

public static void main(String[] args) {
    try {
        String hostname = "localhost";

        System.out.println("Connection Established");

        //for (;;) {

            System.out.println("Enter Your Message : ");
            BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
            String pesan = read.readLine();
            Socket daytime = new Socket(hostname, SERVICE_PORT);

            if (pesan.equals("exit")) {
                System.exit(0);
            } else {

                daytime.setSoTimeout(2000);

                OutputStream out = daytime.getOutputStream();
                PrintStream pout = new PrintStream(out);

                pout.print(pesan);

                out.flush();
                out.close();



                BufferedReader messageIn = new BufferedReader(new InputStreamReader(daytime.getInputStream()));
                System.out.println("Respond : " + messageIn.readLine());
                System.out.println("diterima");


            }


            daytime.close();
        //}

    } catch (IOException e) {
        System.err.println("Error : " + e);
    }
}

}

Upvotes: 1

Views: 594

Answers (3)

Jay Prakash
Jay Prakash

Reputation: 805

Server Class

            OutputStream out = nextClient.getOutputStream();
            PrintStream pout = new PrintStream(out);

            pout.print(messageOut);

            out.flush();
            out.close(); //Don't close this

Client Code :

OutputStream out = daytime.getOutputStream();
PrintStream pout = new PrintStream(out);

pout.print(pesan);

out.flush();
out.close(); //Don't close this

Upvotes: -1

Wojtek
Wojtek

Reputation: 1308

Socket get closed in such situations:

  • when you close the socket,
  • when you close the input or the output socket stream,
  • when you close the object, which is directly or indirectly wrapping the input or the output socket stream, e.g. BufferedReader or Scanner.

Upvotes: 0

xaxxon
xaxxon

Reputation: 19771

            OutputStream out = daytime.getOutputStream();
            PrintStream pout = new PrintStream(out);

            pout.print(pesan);

            out.flush();
            out.close();

https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#getOutputStream()

Closing the returned OutputStream will close the associated socket.

Upvotes: 3

Related Questions