gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:58:45 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com