0
我有這2個代碼,但我不能讓他們正常工作,一個是在Bukkit插件,另一個是ServerSocket部分。多ServerSocket連接Java
ClientThread是否連接到服務器,但它dosent echo數據,因爲我需要它,我也被告知,它dosent支持多個客戶端。
這是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