2014-01-27 4 views
0

У меня есть эти 2 кода, но я не могу заставить их работать правильно, один из них находится в плагине Bukkit, а другой - в части 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 серверов. Спасибо заранее.

ответ

Смежные вопросы