简体   繁体   中英

Multiple ServerSocket Connections Java

i have these 2 codes but i cant get them to work correctly, one is in a Bukkit Plugin and the other one is the ServerSocket part.

The ClientThread DOES connect to the Server but it dosent echo data as i need it to, i have also been told that it dosent support multiple clients.

This is the 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;
    }

}

And this is the Server:

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);
    }
}
}

What i need it to do is Send data to the server and echo it to the Bukkit Servers. Thanks in Advance.

It seems to me that your first while loop in your server code does not end. As far as multiple clients you will need a concurrent server. Here is how to make in java http://www.how2java.com/2012/05/how-to-create-concurrent-server-using.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM