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

时间:2016-05-01 21:32:43

标签: 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();
    }
}
}

然后是客户端类

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();
    }
}



}

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

示例输出:

Server: 4 Server: 10 Server: 12 Server: 2 Server: 16 Server: 14 Server: 6 Server: 4 Server: 6 Server: 12 Server: 12 Server: 6 Server: 10 Server: 16 Server: 10 Server: 12

1 个答案:

答案 0 :(得分:1)

NewClient类中的

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

使用相同的套接字生成多个线程。因此,服务器看起来是随机的,因为有几个使用相同的通信信道。