我的服务器程序无法将数据发送到我编写的客户端程序

时间:2019-07-10 11:30:13

标签: java sockets

因此服务器程序由以下代码组成:

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

public class TimeServer {

    public static void main(String[] args) {

        try {

            //Create sockets
            ServerSocket ss = new ServerSocket(60000);
            Socket rs = ss.accept();
            //create streams
            BufferedInputStream bs = new BufferedInputStream(rs.getInputStream());
            InputStreamReader isr = new InputStreamReader(bs);
            BufferedOutputStream bos = new BufferedOutputStream(rs.getOutputStream());
            PrintWriter pw = new PrintWriter(bos);
            //set timeout
            rs.setSoTimeout(20000);
            int c = 0;
            StringBuilder sb = new StringBuilder();
            //while loop reads in a character until a period (includes period)  
            while (((char) c != '.')) {
                c = isr.read();
                //append each char to a string builder
                sb.append((char) c);
            }
            //convert stringbuilder to string
            String str = sb.substring(0);
            //If string equals "time." returns time else error message
            if (str.compareTo("time.") == 0) {
                Date now = new Date();
                pw.print("time is: " + now.toString());
                pw.flush();
            }
            else {
                pw.print("Invalid syntax: connection closed");
                pw.flush();
            }
            //close socket
            rs.close();
            //close serversocket
            ss.close();
        } catch(IOException i) {
            System.out.println(i.getMessage());
        }
    }
}

客户端的代码为:

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

public class TimeClient {

    public static void main(String[] args) {

        try {
            //create socket
            Socket sock = new Socket("localhost", 60000);
            //create streams
            BufferedInputStream bis = new BufferedInputStream(sock.getInputStream());
            InputStreamReader isr = new InputStreamReader(bis);
            BufferedOutputStream bos = new BufferedOutputStream(sock.getOutputStream());
            PrintWriter pw = new PrintWriter(bos);
            //set timeout
            sock.setSoTimeout(20000);
            //write argument to stream, argument should be "time." to recieve time
            pw.write(args[0]);
            pw.flush();
            int c = 0;
            StringBuilder sb = new StringBuilder();
            //while loop reads each character into stringbuilder
            while ((c != -1)) {
                c = isr.read();
                sb.append((char) c);
            }
            //stringbuilder converted to string and printed
            String str = sb.substring(0);
            System.out.println(str);
            //socket closed
            sock.close();
        } catch(IOException i) {
            System.out.println(i.getMessage());
        }
    }
}

问题是,如果我在单独的cmd.exe中运行每个程序,尽管使用localhost作为IP地址,它们也无法通信。我似乎找不到导致此问题的代码中的逻辑错误,想知道是否有人可以提供帮助?

1 个答案:

答案 0 :(得分:1)

问题是您使用的是BufferedOutputStream,并且在PrintWriter上写入后立即关闭了套接字。您编写的内容保留在缓冲区中,并且在将任何内容发送到客户端之前,套接字已关闭。

您需要先关闭flush才能强制发送缓冲区的内容:

...
//close socket
pw.flush();
rs.close();
...

TimeClient包含一个小错误:您循环接收直到得到正确的-1,但是您将那个-1附加到了错误的StringBuilder上。应该是:

//while loop reads each character into stringbuilder
while(true){
c = isr.read();
if (c == -1) { break; }
sb.append((char) c);
}

但这绝对不能阻止文本显示...