Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-Socket服务器刷新_Java_Sockets_Client Server_Lan - Fatal编程技术网

Java-Socket服务器刷新

Java-Socket服务器刷新,java,sockets,client-server,lan,Java,Sockets,Client Server,Lan,我正在创建一个局域网游戏,我希望我的服务器和客户端能够就玩家的坐标等问题进行反复通信。我有一个非常简单的服务器和客户端设置,下面是代码: public class SimpleClient { public SimpleClient(){ String host = "192.168.1.121"; int port; if(host.length() == 0){ host = "192.168.1.121"

我正在创建一个局域网游戏,我希望我的服务器和客户端能够就玩家的坐标等问题进行反复通信。我有一个非常简单的服务器和客户端设置,下面是代码:

public class SimpleClient {

    public SimpleClient(){

        String host = "192.168.1.121";
        int port;

        if(host.length() == 0){
            host = "192.168.1.121";
            port = 9999;
        } else {
            host = "localhost";
            String portStr = "9999";
            try {
                port = Integer.parseInt(portStr);
            } catch(Exception e){
                port = 9999;
            }
        }

        try {
            System.out.println("Client is tying to connect to the server host: " + host + " " + port);
            Socket skt = new Socket(host,port);

            BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            PrintStream myOutput = new PrintStream(skt.getOutputStream());

            myOutput.print("Hello Server!\n");

            String buf = myInput.readLine();
            if(buf != null) {
                System.out.println("Client recived [" + buf + "] from the sever");
            }

        } catch(IOException e){
            e.printStackTrace();
        }


    }
这是服务器:

public void SimpleServer(){

        try {

            ServerSocket myServerSocket = new ServerSocket(9999);

            System.out.println("Server is waiting for an incoming connection on host=" + InetAddress.getLocalHost().getHostAddress()
                                + " port=" + myServerSocket.getLocalPort());
            Socket skt = myServerSocket.accept();

            BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            PrintStream myOutput = new PrintStream(skt.getOutputStream());

            String buf = myInput.readLine();

            myOutput.print("Hello Client!\n");
            myOutput.print("Hello Client!\n");
            myOutput.print("Hello Client!\n");


            if(buf != null){
                System.out.println("Ther Server read: [" + buf + "]");
                myOutput.print("got it!");
            }


        }catch(Exception e){

        }

    }

我再次尝试让服务器和客户端一次又一次地互相发送消息。我已经尝试了一个while循环,它冻结了游戏。谢谢。

您有什么问题?我如何多次向客户端或服务器发送消息?这取决于您所说的消息是什么意思<代码>套接字使用TCP,这是一种基于流的协议。如果您正在写入
SocketOutputStream
,您正在向客户机/服务器发送一些内容。我对这方面非常陌生,因此我对套接字的理解非常有限。但是我想做的是能够向服务器的客户机发送一对变量,并让它们更新每个循环