Java client/server sockets

喜你入骨 提交于 2021-02-10 05:49:09

问题


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

Client source code:

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)] ?
}

And the server code:

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.

What's going on?


回答1:


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.




回答2:


You are writing text and reading binary. Since your output and input don't match its highly likely to hang in this case.

I suggest you either use binary with writeUTF/readUTF or text with println/readLine.

BTW: readUTF reads two bytes to determine the length of data to read. Since the first two bytes are ASCII text, you are likely to wait for around 16,000 characters before returning.



来源:https://stackoverflow.com/questions/10837279/java-client-server-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!