0
이 2 개의 코드가 있지만 제대로 작동하지 않습니다. 하나는 Bukkit Plugin이고 다른 하나는 ServerSocket 부분입니다.여러 개의 ServerSocket 연결 Java
ClientThread는 서버에 연결되지만 필요에 따라 데이터를 에코합니다. 또한 여러 클라이언트를 지원한다는 내용도 들었습니다.
이
은 ClientThread입니다 :package com.devro.thecosmoscore.Packets.core;
import com.devro.thecosmoscore.TheCosmosCore;
import com.devro.thecosmoscore.Utils.LoggingUtils;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* Programmed by: DevRo_ (Erik Rosemberg)
* Creation Date: 18, 11, 2013
* Programmed for the TheCosmosCore project.
*/
public class ClientThread extends Thread {
private static ClientThread thread;
public Socket socket = null;
public DataOutputStream out = null;
public DataInputStream in = null;
public boolean connected = false;
private String hostName;
private int port;
private static boolean stopping = false;
public ClientThread(String hostName, int port)
{
this.hostName = hostName;
this.port = port;
start();
setThread(this);
}
public static ClientThread getThread()
{
return thread;
}
public static void setThread(ClientThread thread) {
thread = thread;
}
public static void shutdown() {
stopping = true;
}
public void run()
{
LoggingUtils.log("Relay", "Attempting to connect to relay server. . . Please wait");
try
{
this.socket = new Socket(getHostName(), getPort());
this.out = new DataOutputStream(this.socket.getOutputStream());
this.in = new DataInputStream(this.socket.getInputStream());
} catch (UnknownHostException e) {
LoggingUtils.log("Relay", "Could not connect to relay: Unknown host.");
setThread(null);
return;
} catch (IOException e) {
LoggingUtils.log("Relay", "Could not connect to relay: IOException.");
e.printStackTrace();
setThread(null);
return;
}
try
{
this.out.writeUTF(TheCosmosCore.getInstance().getServer().getServerName());
this.out.flush();
}
catch (Exception e)
{
}
this.connected = true;
LoggingUtils.log("Relay", "Connected to the relay server.");
try {
while (loop(this.in, this.out));
} catch (Exception e) {
LoggingUtils.log("Relay", "Connection to relay server dropped.");
setThread(null);
this.connected = false;
return;
} finally {
this.connected = false;
try
{
if (this.out != null) {
this.out.close();
}
if (this.in != null) {
this.in.close();
}
this.socket.close();
LoggingUtils.log("Relay", "Disconnecting from relay.");
} catch (IOException e) {
LoggingUtils.log("Relay", "Could not disconnect from relay: IOException ");
}
}
setThread(null);
}
public boolean loop(DataInputStream in, DataOutputStream out)
throws Exception
{
if (stopping) {
return false;
}
short type = in.readShort();
if (PacketManager.getKnownPackets().get(Short.valueOf(type)) == null) {
return true;
}
IPacket p = (IPacket)((Class)PacketManager.getKnownPackets().get(Short.valueOf(type))).newInstance();
p.read(in);
p.onReceive();
return true;
}
public void write(IPacket packet) {
if (!isConnected()) {
return;
}
try
{
packet.write(this.out);
} catch (IOException e) {
LoggingUtils.log("Relay", "Error writing packet: " + packet.getClass() + ".");
setThread(null);
try
{
this.socket.close();
}
catch (IOException e1)
{
}
}
}
public String getHostName()
{
return this.hostName;
}
public int getPort() {
return this.port;
}
public Socket getSocket() {
return this.socket;
}
public boolean isConnected() {
return this.connected;
}
}
그리고 이것은 서버입니다 : 내가 할 필요 무엇
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;
public class Server {
static Set<Socket> listOfSockets = new HashSet<Socket>();
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java Server <port>");
System.exit(1);
}
int portNumber = Integer.parseInt(args[0]);
ServerSocket echoServer = null;
String line;
DataInputStream is;
PrintStream os;
Socket clientSocket = null;
try {
echoServer = new ServerSocket(portNumber);
while (true) {
listOfSockets.add(echoServer.accept());
}
}catch (IOException e) {
System.out.println(e);
}
System.out.println("Server has started on port number " + portNumber +". To stop it press <CTRL><C>.");
try {
clientSocket = echoServer.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
while (true) {
line = is.readLine();
os.println(line);
}
}catch (IOException e) {
System.out.println(e);
}
}
}
가 서버에 데이터를 전송하고 Bukkit 서버에 에코입니다. 미리 감사드립니다.
제발 도와 주실 래요? – ItsErikR