当我在java TCP中发送数据时,为什么套接字始终关闭

时间:2016-10-06 05:26:12

标签: java sockets serversocket

服务器类: 该程序即将从客户端接收数据然后回复。服务器没问题......但问题是服务器将回复发送给客户端。客户端总是说'套接字已关闭'。我尝试删除secket close语句,但结果是相同的..,输出说“套接字关闭”。所以请帮我解决这个问题...

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);

    }
}

}

客户端类:

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);
    }
}

}

3 个答案:

答案 0 :(得分:3)

            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()

  

关闭返回的OutputStream将关闭相关的套接字。

答案 1 :(得分:0)

套接字在这种情况下关闭:

  • 关闭套接字时,
  • 关闭输入或输出套接字流时,
  • 当你关闭对象时,它直接或间接地包装输入或输出套接字流,例如var value = 4; var arr = [ {player_id: '11', team_id: '1'}, {player_id: '4', team_id: '2'}, {player_id: '10', team_id: '1'} ]; arr = arr.filter(function(item){ return item.player_id != value; }) console.log(arr)BufferedReader

答案 2 :(得分:-1)

服务器类

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

            pout.print(messageOut);

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

客户代码:

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

pout.print(pesan);

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