简体   繁体   中英

Trouble with sending / receiving data

So I am attempting to send data to myself and receive the data then print it, now I have been testing for a while and I have noticed its not sending anything, in fact, maybe it is and I am not receiving it properly, I need assistance with this please.

This is what I am using to send data

        String host = "127.0.0.1";
        int port = Options.port;
        Socket socket = new Socket(host, port);
        OutputStream os = socket.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os);
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write(msg + "\n");
        bw.flush();

This is what I am using to receive, I always use this method and it never works so I would not be surprised if this was the root cause.

        ServerSocket serverSocket = new ServerSocket(Options.port);
        System.out.println("[Listening on port] " + Options.port);

        while(true){
            Socket socket = serverSocket.accept();
            socket = serverSocket.accept();
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String message = br.readLine();
                System.out.println(message);
        }

1) replace:

Socket socket = serverSocket.accept();
socket = serverSocket.accept();

with:

Socket socket = serverSocket.accept();

2) If this does not solve the problem, set ports as int values, without Options.port

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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