java - 保持数据与客户端和服务器同步?

标签 java networking synchronization

我正在创建一个小程序,在客户端和服务器之间发送数据(以练习网络编程)。它所做的只是让客户端将数据(一个整数)发送到服务器,然后服务器将其加一并将其发送回客户端,客户端又将加一并将其发送给服务器以重复循环直至 100 .

服务器类的代码:

package simpleslickgame;

import java.io.DataOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public class NewServer{

static ArrayList<Socket> sockets = new ArrayList<Socket>(20);
static ArrayList<String> msgHistory = new ArrayList<String>(10000);
public static void main(String args[]){

    int port = 60606;

    Socket socky;
    ServerSocket ss;

    try{
        System.out.println("Welcome to the data sender!");
        System.out.println("Listening on port " + port);

        ss = new ServerSocket(port);

        while (true){
            socky = ss.accept();

            sockets.add(socky);

            System.out.println(socky.toString());
            //pw = new PrintWriter(socky.getOutputStream(), true);
            System.out.println("Connection established!");
            Thread t = new Thread(
                    new DataThread(socky));
            t.start();

        }

    }catch(Exception e){
        e.printStackTrace();
    }
}
}
class DataThread extends NewServer implements Runnable{
private Socket s;
private int data;

public DataThread(Socket sock){
    this.s = sock;
}

public void run(){
    String client =  s.getInetAddress().toString();
    System.out.println("Connected to " + client);

    try{
        Scanner in  = new Scanner(s.getInputStream());

        PrintWriter pw = new PrintWriter(s.getOutputStream(), true);

        while(true){
        //pw.println("Testing...");
        this.data = in.nextInt();
        if(this.data > 99)
            break;
        System.out.println("Server: " + this.data);
        pw.println(this.data+1);
        }
        s.close();
        in.close();

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

然后是 Client 类

package simpleslickgame;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class NewClient {

public static void main(String[] args) {
    int port = 60606;
    System.out.println("Welcome to the message client!");
    Socket s = getSocket(port);
    String message;

    String user;
    try{
        System.out.println("Connected on port " + port);


        while(true){
            Thread t = new Thread(
                    new GameThread(s));
            t.start();
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}
private static Socket getSocket(int port){
    Socket s;
    String host;
    InetAddress ip;

    Scanner input = new Scanner(System.in);
    while(true){
        System.out.print("What server do you want to connect to?");
        host = input.nextLine();
        try{
            ip = InetAddress.getByName(host);
            s = new Socket(ip,port);
            return s;
        }catch(UnknownHostException e){
            System.out.println("The host is unknown.");
        }catch(IOException e){
            System.out.println("Network error.");
        }
    }
}

}
class GameThread extends NewClient implements Runnable{
private Socket s;
private int data;
public GameThread(Socket sock){
    this.s = sock;
}
public void run(){
    try{
        Scanner in = new Scanner(s.getInputStream());
        PrintWriter out = new PrintWriter(s.getOutputStream(), true);
        this.data = -1;
        while(true){

            out.println(this.data+1);
            this.data = in.nextInt();
            System.out.println("Client: " + this.data);
            if(this.data > 99){
                break;
            }
        }
        this.s.close();


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



}
然而,当我运行这两个时,我只是得到了大量的输出,这些输出似乎不符合代码,而只是看似随机的数量(显然它们不是,但我需要一些帮助来弄清楚为什么它不是增量的)。

示例输出:

服务器:4 服务器:10 服务器:12 服务器:2 服务器:16 服务器:14 服务器:6 服务器:4 服务器:6 服务器:12 服务器:12 服务器:6 服务器:10 服务器:16 服务器:10 服务器:12

最佳答案

在 NewClient 类中:

     while(true){
        Thread t = new Thread(
                new GameThread(s));
        t.start();
    }

使用同一个套接字生成多个线程。因此,服务器获得看似随机的整数,因为有多个服务器使用相同的通信 channel 。

关于java - 保持数据与客户端和服务器同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36972215/

相关文章:

java - 将多个选择 Jlist 作为字符串返回

apache - httpd 在内部运行但不在外部运行

c# - EnterWriteLock之后,不保留锁定

c - 无锁队列和指针问题

java - Maven jmeter插件: SAXParseException: Content is not allowed in prolog

java - 将系统时间更改为提前三个小时,然后恢复为默认值

java - 如何在运行时检查是否声明了 NamedNativeQuery

Python urllib2 URLError 异常?

C - inet_pton() 不生成网络字节顺序

java - 哪个线程通知唤醒?