【问题标题】:Java client/server socketsJava 客户端/服务器套接字
【发布时间】:2012-06-05 22:15:31
【问题描述】:

我开始使用 java 套接字并且有奇怪的 [缺少] 输出。这是我的套接字方法的来源:

客户端源码:

public void loginToServer(String host, String usnm ) {
    try {
        Socket testClient = new Socket(host,1042);
        System.out.println ("Connected to host at " + host);
        logString = ("CONNECTED: " + host);
        outGoing = new PrintWriter(testClient.getOutputStream(), true);
        outGoing.print("Hello from " + testClient.getLocalSocketAddress());
        InputStream inFromServer = testClient.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        System.out.println("Server says " + in.readLine());
        testClient.close();
    }
    catch (Exception e) {
        System.err.println ("Error connecting to host at " + host + ":1042.\n Reason: " + e);
        logString = ("CONNECT FAILED: " + host + ":1042: " + e);
    }
    printLog(logString);
    // send server usnm and os.name [System.getProperty(os.name)] ?
}

以及服务器代码:

public void runServer() {
        try{
            server = new ServerSocket(1042); 
        }
        catch (IOException e) {
            printLog("LISTEN FAIL on 1042: " + e);
            System.err.println("Could not listen on port 1042.");
            System.exit(-1);
        }
        try{
            client = server.accept();
        }
        catch (IOException e) {
            printLog("ACCEPT FAIL on 1042: " + e);
            System.err.println("Accept failed: 1042");
            System.exit(-1);
        }
        try{
            inComing = new BufferedReader(new InputStreamReader(client.getInputStream()));
            outGoing = new PrintWriter(client.getOutputStream(), true);
        }
        catch (IOException e) {
            printLog("READ FAIL on 1042: " + e);
            System.err.println("Read failed");
            System.exit(-1);
        }
        while(true){
            try{
                clientData = inComing.readLine();
                //processingUnit(clientData, client);
                outGoing.print("Thank you for connecting to " + server.getLocalSocketAddress() + "\nGoodbye!");
            }
            catch (IOException e) {
            printLog("READ FAIL on 1042: " + e);
                System.out.println("Read failed");
                System.exit(-1);
            }
        }
    }

而客户端给出的输出仅仅是Connected to host at localhost

发生了什么事?

【问题讨论】:

    标签: java sockets client serversocket


    【解决方案1】:

    您正在阅读线路,但您没有发送线路。将print() 更改为println()readLine() 将永远阻塞等待换行符。它将在流结束时返回 null,即当对等方关闭连接时,但您也没有检查它,因此您无限循环。

    【讨论】:

      【解决方案2】:

      您正在编写文本并读取二进制文件。由于您的输出和输入不匹配,因此在这种情况下很可能会挂起。

      我建议您使用带有 writeUTF/readUTF 的二进制文件或带有 println/readLine 的文本。

      BTW:readUTF 读取两个字节以确定要读取的数据长度。由于前两个字节是 ASCII 文本,您可能要等待大约 16,000 个字符才能返回。

      【讨论】:

      • 谢谢你,我永远猜不到这一点。每种类型适用于哪些情况?
      • 文本非常适合发送人类可读的单词和数字。二进制有利于高效准确地发送数据。
      • 这实际上似乎不是问题所在。纠正这个问题后,似乎服务器没有在 while 循环中执行代码?它正在进入循环,但没有尝试或捕捉......
      • 客户端和服务器都在等待对方发送一些文本。我会删除 DataInputStream,因为它比帮助更容易混淆。
      • 为什么客户端在等待文本之前不向服务器发送文本?
      猜你喜欢
      • 2017-02-25
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多