Java 无法在客户端和服务器之间建立UDP连接

Java 无法在客户端和服务器之间建立UDP连接,java,sockets,tcp,udp,datagram,Java,Sockets,Tcp,Udp,Datagram,基本上,我正在编写一个简单的TCP-UDP多客户端GUI程序。我可以建立到服务器的TCP连接,但不能建立UDP连接。服务器正在侦听端口4000。我在GUI客户端上有一个组合框,其中包含元素TCP和UDP。如果用户选择UDP并单击Connect.它将通过调用UDPconnection方法尝试建立UDP连接 以下是我的客户端GUI代码: public void UDPconnection() { try { done = false; datagram

基本上,我正在编写一个简单的TCP-UDP多客户端GUI程序。我可以建立到服务器的
TCP
连接,但不能建立
UDP
连接。服务器正在侦听端口
4000。
我在GUI客户端上有一个
组合框,其中包含元素
TCP
UDP。
如果用户选择
UDP
并单击
Connect.
它将通过调用
UDPconnection
方法尝试建立
UDP
连接

以下是我的客户端GUI代码:

public void UDPconnection()
{
    try
    {
        done = false;
        datagramSocket = new DatagramSocket(serverPort);
    }
    catch (SocketException e)
    {
        done = true;
        System.out.println("Host not available");
    }
}
public class chatServer2 implements Runnable {

    private int clientCount = 0;
    private ChatServerThread clients[] = new ChatServerThread[50];
    private ServerSocket server = null;
    Thread thread = null;

    //same as version3
    public chatServer2(int port){
        try{
            server = new ServerSocket(port);//step1
            System.out.println("Started the server...waiting for a client");
            start(); //the chatserver's start method that goes ahead and creates a new thread
        }
        catch(IOException e){
            System.err.println("ERROR "+e.getMessage());

        }
    }

    public void start(){
        if(thread == null){
            thread = new Thread(this);
            thread.start();
        }
    }

    @Override
    public void run() {//same as version 3
        while(thread !=null){
            try{
                System.out.println("Waiting for a client...");
                //now we add a new Thread and accept a client
                addThread(server.accept());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void addThread(Socket socket){
        if(clientCount < clients.length){
            clients[clientCount] = new ChatServerThread(this, socket);
            try {
                clients[clientCount].open();//open the stream for the ChatServerThread client
                clients[clientCount].start();//start to run the ChatServerThread client
                clientCount++;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


    public synchronized void handle(int ID, String input)
    {
        System.out.println(input);

        if(input.equalsIgnoreCase("bye"))
        {
            remove(ID);//person said bye so remove them
        }
        else
        {
            System.out.println(input);
            for (int i = 0; i < clientCount; i++)
            {

                clients[i].send("User: " + ID + ": " + input);
            }
        }

    }



    public synchronized void remove(int ID){
        int position = findClient(ID);
        if(position >=0){
            ChatServerThread toRemove = clients[position];
            if(position <clientCount-1){
                for(int i= position+1; i <clientCount; i++){
                    clients[i-1] = clients[i];
                }
                clientCount--;
            }
            try {
                toRemove.close();//close the person's that said bye connection
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    private int findClient(int ID){
        for(int i=0; i<clientCount; i++){
            if(clients[i].getID() == ID){
                return i;
            }
        }
        return -1;//not in the array
    }

    public static void main(String [] args){
        chatServer2 l = new chatServer2(4000);
    }

}
这是我的聊天服务器代码:

public void UDPconnection()
{
    try
    {
        done = false;
        datagramSocket = new DatagramSocket(serverPort);
    }
    catch (SocketException e)
    {
        done = true;
        System.out.println("Host not available");
    }
}
public class chatServer2 implements Runnable {

    private int clientCount = 0;
    private ChatServerThread clients[] = new ChatServerThread[50];
    private ServerSocket server = null;
    Thread thread = null;

    //same as version3
    public chatServer2(int port){
        try{
            server = new ServerSocket(port);//step1
            System.out.println("Started the server...waiting for a client");
            start(); //the chatserver's start method that goes ahead and creates a new thread
        }
        catch(IOException e){
            System.err.println("ERROR "+e.getMessage());

        }
    }

    public void start(){
        if(thread == null){
            thread = new Thread(this);
            thread.start();
        }
    }

    @Override
    public void run() {//same as version 3
        while(thread !=null){
            try{
                System.out.println("Waiting for a client...");
                //now we add a new Thread and accept a client
                addThread(server.accept());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void addThread(Socket socket){
        if(clientCount < clients.length){
            clients[clientCount] = new ChatServerThread(this, socket);
            try {
                clients[clientCount].open();//open the stream for the ChatServerThread client
                clients[clientCount].start();//start to run the ChatServerThread client
                clientCount++;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


    public synchronized void handle(int ID, String input)
    {
        System.out.println(input);

        if(input.equalsIgnoreCase("bye"))
        {
            remove(ID);//person said bye so remove them
        }
        else
        {
            System.out.println(input);
            for (int i = 0; i < clientCount; i++)
            {

                clients[i].send("User: " + ID + ": " + input);
            }
        }

    }



    public synchronized void remove(int ID){
        int position = findClient(ID);
        if(position >=0){
            ChatServerThread toRemove = clients[position];
            if(position <clientCount-1){
                for(int i= position+1; i <clientCount; i++){
                    clients[i-1] = clients[i];
                }
                clientCount--;
            }
            try {
                toRemove.close();//close the person's that said bye connection
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    private int findClient(int ID){
        for(int i=0; i<clientCount; i++){
            if(clients[i].getID() == ID){
                return i;
            }
        }
        return -1;//not in the array
    }

    public static void main(String [] args){
        chatServer2 l = new chatServer2(4000);
    }

}
公共类chatServer2实现可运行{
private int clientCount=0;
私有ChatServerThread客户端[]=新ChatServerThread[50];
私有服务器套接字服务器=null;
线程=空;
//与版本3相同
公共聊天服务器2(int端口){
试一试{
服务器=新服务器套接字(端口);//步骤1
System.out.println(“启动服务器…等待客户端”);
start();//继续并创建新线程的chatserver的start方法
}
捕获(IOE异常){
System.err.println(“ERROR”+e.getMessage());
}
}
公开作废开始(){
如果(线程==null){
线程=新线程(此);
thread.start();
}
}
@凌驾
public void run(){//与版本3相同
while(线程!=null){
试一试{
System.out.println(“等待客户…”);
//现在我们添加一个新线程并接受一个客户端
addThread(server.accept());
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
公共void addThread(套接字){
if(clientCount=0){
ChatServerThread toRemove=客户端[位置];
如果(位置)
  • 首先,没有所谓的“UDP连接”

  • 在指定端口上创建
    DatagramSocket
    失败并不意味着“主机不可用”。请查看异常消息以了解其含义

  • UDP不与TCP进行互操作,特别是与Java
    ServerSockets
    不进行互操作。两端需要一个
    DatagramSocket
    DatagramChannel
    ,并且没有连接阶段:只需来回发送数据报