服务器未在套接字通信中接收

时间:2014-11-11 16:59:35

标签: java sockets

我正在尝试创建一个Java程序,其中服务器生成一个随机数,并在与客户端建立连接后,让它猜出数字。但是,他们似乎都无法收到对方的消息。
服务器端:

package numberguessserv;

import java.net.*;
import java.io.*;
import java.util.Random;
import java.util.Scanner;

public class NumberGuessServ {

    public static void main(String[] args) {
        Scanner inpkb = new Scanner(System.in);
        Random randomGenerator = new Random();
        int randomNum = randomGenerator.nextInt(10);
        String number = Integer.toString(randomNum);
        int port;
        boolean isGuessed = false;
        String msgReceived;
        Socket connect = new Socket();
        System.out.print("port: ");
        port = inpkb.nextInt();
        try {
            ServerSocket clSock = new ServerSocket(port);
            System.out.println("Waiting...");
            connect = clSock.accept();
            System.out.println("Connection established with"+connect.getInetAddress());
            InputStreamReader isr = new InputStreamReader(connect.getInputStream());
            BufferedReader isrBuff = new BufferedReader(isr);
            OutputStreamWriter osw = new OutputStreamWriter(connect.getOutputStream());
            BufferedWriter oswBuff = new BufferedWriter(osw);
            while (!isGuessed) {
                msgReceived = isrBuff.readLine();
                System.out.println("Number received: "+msgReceived);
                if (msgReceived.equals(number)) {
                    isGuessed = true;
                    oswBuff.write("Right!");
                    oswBuff.flush();
                }
                else {
                    oswBuff.write("Wrong!");
                    oswBuff.flush();
                }
                if (isGuessed)
                    System.out.println("Number was guessed right.");
                else
                    System.out.println("Number was guessed wrong.");
                }
            }
            catch (Exception ex) {
                System.out.println("An exception has occurred: "+ex);
            }
            finally {
                try {
                    connect.close();
                }
                catch (Exception ex) {
                    System.out.println("An exception has occurred: "+ex);
                }
           }
      }
 }

客户方:

package numberguessclient;

import java.io.*;
import java.util.Scanner;
import java.net.*;

public class NumberGuessClient {

    public static void main(String[] args) {
        Scanner inpkb = new Scanner(System.in);
        int port;
        String IP;
        boolean isGuessed = false;
        String number, msg;
        Socket serv = new Socket();
        System.out.print("IP: ");
        IP = inpkb.next();
        System.out.print("port: ");
        port = inpkb.nextInt();
        try {
            serv = new Socket(IP,port);
            System.out.println("Connetion established with"+serv.getInetAddress());
            InputStreamReader isr = new InputStreamReader(serv.getInputStream());
            BufferedReader isrBuff = new BufferedReader(isr);
            OutputStreamWriter osw = new OutputStreamWriter(serv.getOutputStream());
            BufferedWriter oswBuff = new BufferedWriter(osw);
            while (!isGuessed) {
                System.out.print("number: ");
                number = inpkb.next();
                oswBuff.write(number);
                oswBuff.flush();
                System.out.println("Number sent.");
                msg = isrBuff.readLine();
                System.out.println("The reply was received: "+msg);
                if (msg.equals("Right!")) {
                    isGuessed = true;
                    System.out.println(msg);
                }
                else {
                    System.out.println(msg+"\nTry again...");
                }
            }
        }
        catch (Exception ex) {
            System.out.print("An exception has occurred: "+ex);
        }
        finally {
            try {
                serv.close();
            }
            catch (Exception ex) {
                System.out.print("An exception has occurred: "+ex);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这是预料之中的。两端都是使用

读取的
isrBuff.readLine();

所以,他们都希望另一端发送一条线。但他们都没有发一条线。他们都做了

oswBuff.write(number);
oswBuff.flush();

发送一些字符,但不发送任何换行符。接收端无法知道已到达行尾,因此它会继续阻塞,直到收到EOL为止。

答案 1 :(得分:0)

readLine ()等待行尾。除非并且直到它获得一个新的行字符,否则它将在那里等待阅读。

所以,你需要给出一个新的线条特征' \ n'每次为输出流提供输入,以便readLine()读取换行符并且不等待。

目前在您的代码中,您需要在每次输入时通过执行以下操作来提供换行符:

oswBuff.write("any message");
oswBuff.write('\n');      //add this statement everywhere you are writing to the output stream
oswBuff.flush();

这适用于服务器和客户端。