我正在用Java制作游戏。我被困了,因为我不知道如何处理阻塞套接字,因为我想拥有服务器gui。我在客户端也有问题,因为它阻碍了游戏的核心。服务器socket.accept()
是问题1,而客户端我不知道。我到处搜索,但我没有找到答案。我只发现了像#34; Make thread来处理socket"。我试过这个。
客户端
package com.sec.network;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class Clinetmulti {
public static boolean running = false;
public static boolean delayed = false;
public static void main(String args[]) throws Exception {
String[] namesList = new String[4];
namesList[0] = "Unknown";
namesList[1] = "Tom";
namesList[2] = "Ronald";
namesList[3] = "Lamkas";
String inmess = "NoMess";
String outmess = "NoMess";
boolean connected = false;
int random1 = ThreadLocalRandom.current().nextInt(1, 3 + 1);
String playername = namesList[random1];
Socket sk=new Socket("hidden" , 5000);
BufferedReader sin=new BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream sout=new PrintStream(sk.getOutputStream());
String s;
String oldinmess = "NoMess";
running = true;
System.out.println("Client started...");
while (true) {
if(delayed == true) {
//Read
if(sin.ready()) {
if(!sin.readLine().equals(oldinmess)) {
inmess = sin.readLine();
System.out.print("Server : " + inmess + "\n");
oldinmess = inmess;
}
}
//Check Messages
if(connected == false) {
if(inmess.equals(playername + "> " + "Connected")) {
connected = true;
}
}
//Check States
if(connected == false) {
outmess = "Request connect";
}
if(inmess.equals("Server stop")) {
outmess = "Stopping...";
break;
}
//Post
if(!outmess.equals("NoMess")) {
sout.println(playername + "> " + outmess);
sout.flush();
System.out.println("Client: " + playername + "> " + outmess);
}
//ClearMessageProccess
inmess = "NoMess";
outmess = "NoMess";
}
}
sk.close();
sin.close();
sout.close();
}
}
SERVER
package com.sss.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sss.main.Launcher;
public class Servermulti implements Runnable {
int port;
Thread th;
boolean running = false;
ServerSocket server=null;
Socket client=null;
ExecutorService pool = null;
int clientcount=0;
static String inMess = "NoMess";
static String outMess = "NoMess";
static boolean stop = false;
static int lchname;
static String player;
static int players = 0;
static String[] playerList = new String[100];
public static void main(String args[]) {
Servermulti sm = new Servermulti(5000);
}
public synchronized void start() {
th = new Thread(this);
if(!running) {
running = true;
}
System.out.println(th.getName());
try {
startServer();
} catch (IOException e) {
e.printStackTrace();
}
}
public Servermulti(int port){
System.out.println("Server started...");
this.port=port;
pool = Executors.newFixedThreadPool(5);
start();
}
public void startServer() throws IOException {
server = new ServerSocket(5000);
System.out.println("Server Booted");
System.out.println("Any client can stop the server by sending -1");
while(true) {
client = server.accept();
clientcount++;
ServerThread runnable= new ServerThread(client,clientcount,this);
pool.execute(runnable);
}
}
private static class ServerThread implements Runnable {
Servermulti server=null;
Socket client = null;
BufferedReader cin;
PrintStream cout;
int id;
String s;
ServerThread(Socket client, int count ,Servermulti server ) throws IOException {
this.client=client;
this.server=server;
this.id=count;
System.out.println("Connection " + id + " established with client " + client);
cin=new BufferedReader(new InputStreamReader(client.getInputStream()));
cout=new PrintStream(client.getOutputStream());
}
@Override
public void run() {
int x=1;
try{
while(true){
//Read
if(cin.ready()) {
inMess = cin.readLine();
System.out.println(inMess);
}
//Check
if(!inMess.equals("NoMess")) {
lchname = inMess.indexOf(">");
player = inMess.substring(0, lchname);
if(inMess.equals(player + "> " + "Request connect")) {
outMess = "Connected";
playerList[players + 1] = player;
players++;
}
}
//Post
if(!outMess.equals("NoMess")) {
cout.println(player + "> " + outMess);
cout.flush();
System.out.println("Server: " + player + "> " + outMess);
}
//StopCheck
if(stop == true) {
break;
}
//Clean Proccess
outMess = "NoMess";
inMess = "NoMess";
}
cin.close();
client.close();
cout.close();
if(x==0) {
System.out.println( "Server cleaning up.");
System.exit(0);
}
}
catch(IOException ex){
System.out.println("Error : "+ex);
}
}
}
@Override
public void run() {
}
}
答案 0 :(得分:0)
您需要实现多线程,1个线程接受来自客户端的连接,1个线程为连接到服务器的每个客户端启动。
以下是如何操作的示例:
public class Server {
private ServerSocket server;
public Server(int port) {
server = new ServerSocket(port);
}
public void listenForConnections() {
while (true) {
// Accept clients
Client client = new Client(server.accept());
// Create and start new thread when client connects
new Thread(client).start();
}
}
}
public class Client implements Runnable {
private Socket socket;
public Client(Socket socket) {
this.socket = socket;
}
public void run() {
while (true) {
// Do something
}
}
}