Java客户端/服务器套接字

Java客户端/服务器套接字,java,sockets,client,serversocket,Java,Sockets,Client,Serversocket,我从java套接字开始,有奇怪的[缺少]输出。下面是我的套接字方法的来源: 客户端源代码: public void loginToServer(String host, String usnm ) { try { Socket testClient = new Socket(host,1042); System.out.println ("Connected to host at " + host); logString = ("CONNE

我从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);
            }
        }
    }
客户端提供的输出仅仅是连接到本地主机上的主机


发生了什么事?

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

我建议您将二进制与writeUTF/readUTF一起使用,或将text与println/readLine一起使用


顺便说一句:readUTF读取两个字节以确定要读取的数据长度。由于前两个字节是ASCII文本,您可能需要等待大约16000个字符才能返回。

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

感谢捆绑,我永远不会猜到这一点。每种类型适合什么情况?文本有助于发送人类可读的单词和数字。二进制文件有助于高效准确地发送数据,但实际上这似乎不是问题所在。纠正了这一点后,服务器似乎没有执行while循环中的代码?它正在进入循环,但没有尝试或捕获…客户端和服务器都在等待对方发送一些文本。我会删除DataInputStream,因为它更容易混淆而不是帮助。为什么客户端在等待文本之前不向服务器发送文本?