共享内存 java_java - Java客户端-服务器编程:客户端之间的共享内存 - 堆栈内存溢出...
发布日期:2021-06-24 11:23:59 浏览次数:3 分类:技术文章

本文共 2078 字,大约阅读时间需要 6 分钟。

我遵循客户端-服务器模型开发了一个简单的Java 2D多人游戏。 关于套接字编程的功能很简单:

客户端向服务器发送其角色数据(x位置,y位置,健康点等)

服务器将更新的字符数据注册在一个表中,该表存储所有不同客户端的字符数据

服务器将表发送回客户端,以便他们可以呈现其他字符

我遇到了一个怪异的问题,希望能帮助您理解,以便我可以继续进行该项目。 这是我现在正在运行的代码。

import java.io.IOException;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Set;

import java.util.Vector;

import java.util.HashSet;

import java.util.Scanner;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

public class Server

{

private static Vector IDs = new Vector<>();

public static void main(String[] args) throws Exception

{

ExecutorService pool = Executors.newFixedThreadPool(32);

try (ServerSocket listener = new ServerSocket(40000))

{

System.out.println("The server is up.");

while (true)

{

pool.execute(new Player(listener.accept()));

}

}

}

private static class Player implements Runnable

{

private Socket socket;

private Scanner input;

private PrintWriter output;

private int ID;

public Player(Socket socket)

{

this.socket = socket;

this.ID = IDs.size() + 1;

IDs.add(ID);

}

public void run()

{

try

{

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

output = new PrintWriter(socket.getOutputStream(), true);

while(input.hasNextLine())

{

String result = "";

for(int i = 0; i < IDs.size(); i++)

{

result += (IDs.get(i) + " ");

}

output.println(result);

}

}

catch(Exception e)

{

System.out.println(e);

}

}

}

}

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

import java.util.Scanner;

public class Client {

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

try (Socket socket = new Socket("127.0.0.1", 40000)) {

Scanner scanner = new Scanner(System.in);

Scanner in = new Scanner(socket.getInputStream());

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

while (scanner.hasNextLine()) {

out.println(scanner.nextLine());

System.out.println(in.nextLine());

}

in.close();

scanner.close();

}

}

}

我想发生的事情是:每次客户端向服务器发送任何消息时,服务器都会以ID列表进行响应(每个连接的客户端都会获得一个新ID)。

实际情况:

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS85N0tOVC5wbmc=

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9QMk5LTi5wbmc=

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9FRjdHOC5wbmc=

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9UWTc4NS5wbmc=

连接的第二个客户端能够看到已经有第一个客户端。 但是第一个客户端看不到第二个客户端连接。

谢谢您的帮助。

转载地址:https://blog.csdn.net/weixin_32473663/article/details/114483103 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java布局管理器空布局_Java图形化界面设计——布局管理器之null布局(空布局)...
下一篇:spring java配置_Spring Java配置要点

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月08日 07时16分00秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【 ML 】Newton – Raphson Iteration Procedure of TOA - Based Positioning Simulation 2019-04-28
【 ML 】 Gauss – Newton Iteration Procedure of TOA - Based Positioning Simulation 2019-04-28
【 ML 】Steepest Descent Iteration Procedure of TOA - Based Positioning Simulation 2019-04-28
【 LLS 】Linear Approaches of TOA - Based Positioning 2019-04-28
【 MATLAB 】 LLS algorithm Simulation of TOA - Based Positioning 2019-04-28
【 MATLAB 】 WLLS algorithm Simulation of TOA - Based Positioning 2019-04-28
【 MATLAB】 Two-step WLS algorithm Simulation of TOA - Based Positioning 2019-04-28
【 MATLAB】Subspace algorithm Simulation of TOA - Based Positioning 2019-04-28
【 Notes 】WLLS Algorithm of TOA - Based Positioning (include the two - step WLS estimator) 2019-04-28
【 Notes 】Best linear unbiased estimator(BLUE) approach for time-of-arrival based localisation 2019-04-28
Heron's formula 2019-04-28
Law of cosines or cosine formula 2019-04-28
【 Notes 】MOBILE LOCALIZATON METHOD BASED ON MULTIDIMENSIONAL SIMILARITY ANALYSIS 2019-04-28
【 仿真 】基于多维相似性分析的移动定位方法仿真 2019-04-28
【 SIMULATION 】RMSE Comparison of Linear Approaches for TOA - Based Positioning 2019-04-28
【 笔记 】定位算法性能分析 2019-04-28
【 Notes 】 Measurement Model and Principles for TDOA-based Location 2019-04-28
【 Notes 】ALGORITHMS FOR TDOA-based SOURCE LOCALIZATION 2019-04-28
【 MATLAB 】Contour plot of matrix(矩阵的等高线图) 2019-04-28
Parabola(抛物线) 2019-04-28