TCP客户端/服务器程序,DataInputStream/DataOutputStream问题

Kis*_*han 2 java sockets tcp tcp-ip

我正在尝试编写一个简单的TCP客户端服务器连接.服务器为每个新客户端连接生成一个线程,每个线程与客户端进行通信.我正在使用DataInputStream和DataOutputStream类,在dis.readUTF()上,服务器线程停止运行.我尝试使用BufferedReader和PrintStream/Printwriter,仍然是同样的问题.请查找System.out.println("现在不在这里"),它前面的那行阻止执行.

/*
TCP client
*/

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPClient {

    public TCPClient() {
        // TODO Auto-generated constructor stub

    }

    public static void main (String args[]) throws UnknownHostException, IOException {

        Socket socket = new Socket("localhost", 9701);

        DataInputStream input = new DataInputStream(socket.getInputStream());

        DataOutputStream output = new DataOutputStream(socket.getOutputStream());

        //char[] buffer = new char[100];

        boolean stop = false;

        while (!stop) {

            System.out.println("here");
            output.writeBytes("hello server");

            String response = "-WTF-";
            System.out.println("here");

            response = input.readUTF();
            System.out.println("not here now");

            if (response == "kill") {
                stop = true;
            } else {
                System.out.println("5");
                output.writeBytes("talk to me");                
                System.out.println("received" + response);
            }
        }
        socket.close();
    }
}


/* TCP server */

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class TCPServer extends Thread {

    final static int TCP_SERVER_PORT = 9701;
    private Socket socket;

    public TCPServer(Socket sock) {
        // TODO Auto-generated constructor stub
        socket = sock;

    }

    public void run()  {

        System.out.println(this.socket.getPort() + " working or sleeping for 5 seconds");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        DataInputStream clientinp;
        DataOutputStream clientout;

        try {
            clientinp = new DataInputStream(socket.getInputStream());
            clientout = new DataOutputStream(socket.getOutputStream());
            System.out.println("here");

            while (true) {
                System.out.println("here now");
                String sentence = clientinp.readUTF();   
                System.out.println("not here now");
                System.out.println(sentence);
                clientout.writeBytes(sentence);

            }

        }
        catch (IOException e) {

            System.out.println(e.getStackTrace());
        }
        finally {

            try {
                this.socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        /*
         * other logic
         */     
    }

    public static void main(String args[]) throws IOException {

        ServerSocket serversocket;

        serversocket = new ServerSocket(TCP_SERVER_PORT);

        while (true) {
            Socket clientsocket = serversocket.accept();

            new TCPServer(clientsocket).start();

        }       
    }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*n C 8

您正在使用writeBytes在客户端中编写字符串并readUTF在服务器中读取相同的字符串.

如果您查看这两种方法的javadoc,您将看到您正在以一种格式书写,然后在另一种格式中阅读.具体来说,readUTF期望输入以2字节字符计数开始,然后是字符的"修改的UTF-8"编码.但是writeBytes每个字符只写1个字节.通常,readUTF将尝试读取比writeBytes写入更多的字节...并且套接字流将冻结.

你应该使用writeUTF而不是writeBytes......