本文介绍了Java客户端/服务器套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Java套接字开始,有奇怪的[缺乏]输出.这是我的套接字方法的来源:

I'm starting off with java sockets and have strange [lack of] output. Here's my source for the socket methods:

客户端源代码:

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

客户端提供的输出仅仅是连接到本地主机的主机.

And the output the client gave was merely Connected to host at localhost.

这是怎么回事?

推荐答案

您正在阅读线路,但未发送线路.将 print()更改为 println(). readLine()将永远阻止等待换行符.它会在流的末尾(即,当对等方关闭连接时)返回null,但是您也不检查该连接,因此您将无限期地循环.

You are reading lines but you aren't sending lines. Change print() to println(). readLine() will block forever waiting for the newline. It will return null at end of stream, i.e. when the peer closes the connection, but you aren't checking for that either, so you are looping indefinitely.

这篇关于Java客户端/服务器套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-19 02:15
查看更多