简体   繁体   English

为什么在Java TCP中发送数据时套接字总是关闭

[英]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'. 我尝试删除secket 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);
    }
}

}
            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() https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#getOutputStream()

Closing the returned OutputStream will close the associated socket. 关闭返回的OutputStream将关闭关联的套接字。

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, eg BufferedReader or Scanner . 关闭对象时,该对象将直接或间接包装输入或输出套接字流,例如BufferedReaderScanner

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM