0

我正在尝试使用 UDP 套接字在 Java 中创建一个非常简单的聊天应用程序。有一台服务器和多个客户端。

现在我的代码是:

服务器 :

package chat;

import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Set;

class User{
    InetAddress addr;
    int port;

    User(InetAddress a,int p){
        addr = a;
        port = p;
    }
}



public class Server {
    private static final int PORT = 27012;
    private static DatagramSocket sckt;
    private static DatagramPacket in,out;
    private static byte[] buffer;
    private static HashMap<String,User> users;
    public static void main(String[] args) {
        try{
            System.out.println("Opening port...");
            sckt = new DatagramSocket(PORT);
            users = new HashMap<String,User>();

        }
        catch(SocketException e){
            System.out.println("Port connection failed!");
            System.exit(1);
        }

        handleClient();
    }
        private static void sendMsg(InetAddress addr, int port, String msg){
            try{
            out = new DatagramPacket(msg.getBytes(),msg.length(),addr,port);
            sckt.send(out);
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    private static void handleClient(){
        try{
            String msgIn,msgOut="",senderNick;
            do{
            buffer = new byte[256];
            in = new DatagramPacket(buffer,buffer.length);
            sckt.receive(in);
            InetAddress clientAddress = in.getAddress();
            int clientPort = in.getPort();
            msgIn = new String(in.getData(),0,in.getLength());
            //print msgIn
            //System.out.println(msgIn);
                        senderNick = msgIn.substring(0,msgIn.indexOf(" "));
                        msgIn = msgIn.substring(msgIn.indexOf(" ")+1);
            if(msgIn.equals("/connect")){
                //String nick = msgIn.substring(msgIn.indexOf(" ") + 1);
                System.out.println(senderNick);
                if(users.containsKey(senderNick)){
                    msgOut = "Nick already in use!";
                }
                else{
                    users.put(senderNick, new User(clientAddress,clientPort));
                    msgOut = "Connected!";
                }
                                sendMsg(clientAddress,clientPort,msgOut);
            }
                        else if(msgIn.equals("/list")){
                            Set userNames;
                            userNames = users.keySet();
                            msgOut = "Users : \n";
                            msgOut += userNames.toString();
                            sendMsg(clientAddress,clientPort,msgOut);
                        }
                        else if(msgIn.startsWith("/msg")){
                            String tmp = msgIn.substring(msgIn.indexOf(" ")+1);
                            String receiverName = tmp.substring(0,tmp.indexOf(" "));
                            String message = tmp.substring(tmp.indexOf(" ")+1);

                            if(!users.containsKey(receiverName)){
                                msgOut = "User " + receiverName + " not found!";
                                sendMsg(clientAddress,clientPort,msgOut);
                            }
                            else{
                                User receiver = users.get(receiverName);
                                msgOut = "Message from "+  senderNick  +" : "+message;
                                sendMsg(clientAddress,clientPort,"Message Sent!");
                                sendMsg(receiver.addr,receiver.port,msgOut);
                            }

                        }
                        else if(msgIn.startsWith("/nick")){
                            String newNick = msgIn.substring(msgIn.indexOf(" ")+1);
                            if(users.containsKey(newNick)){
                                msgOut = "Nick already in use!";
                            }
                            else{
                                users.put(newNick,users.get(senderNick));
                                users.remove(senderNick);
                                msgOut = "Nick changed!";
                            }
                            sendMsg(clientAddress,clientPort,msgOut);
                        }
                        else if(msgIn.equals("/disconnect")){
                            users.remove(senderNick);
                        }

            //out = new DatagramPacket(msgOut.getBytes(),msgOut.length(),clientAddress,clientPort);
            //sckt.send(out);
            }while(true);
        }
        catch(IOException e){
            e.printStackTrace();
        }
        finally{
            System.out.println("Closing connection.");
            sckt.close();
        }
    }

}

客户 :

package chat;

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {

    private static InetAddress host;
    private static final int PORT = 27012;
    private static DatagramSocket sckt;
    private static DatagramPacket in, out;
    private static byte[] buffer;
    private static String nick;

    static class MessageListener implements Runnable {
        //DatagramPacket in;
        //byte[] buffer;
        String reply;
        public void run() {
            do{
            try{
            buffer = new byte[256];
            in = new DatagramPacket(buffer,buffer.length);
            sckt.receive(in);
            reply = new String(in.getData(),0,in.getLength());
            System.out.println("SERVER> "+reply);
            }
            catch(IOException e){
                e.printStackTrace();
            }
            }while(true);
        }
    }

    public static void main(String[] args) {
        try {
            host = InetAddress.getLocalHost();

        } catch (UnknownHostException e) {
            System.out.println("Host not found!");
            System.exit(1);
        }
        connect();

    }

    private static void sendMsg(String msg) {
        try {
            out = new DatagramPacket(msg.getBytes(), msg.length(), host, PORT);
            sckt.send(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void connect() {
        Scanner sc = new Scanner(System.in);

        try {
            buffer = new byte[256];
            sckt = new DatagramSocket();
            String reply = "";
            do {
                System.out.println("Name: ");
                nick = sc.nextLine();
                sendMsg(nick + " /connect");
                in = new DatagramPacket(buffer, buffer.length);
                sckt.receive(in);
                reply = new String(in.getData(), 0, in.getLength());
                System.out.println("SERVER> " + reply);
            } while (!reply.equals("Connected!"));
            accessServer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void accessServer() {
        try {
            sckt = new DatagramSocket();
            Scanner sc = new Scanner(System.in);
            String msg = "", reply = "";
            Thread myT = new Thread(new MessageListener());
            myT.start();
            do {
                System.out.print("Enter Message: ");
                msg = sc.nextLine();
                if (!msg.equals("/quit")) {
                    buffer = new byte[256];

                    sendMsg(nick + " " + msg);


                } else {
                    sendMsg(nick + " /disconnect");
                }
            } while (!msg.equals("/quit"));

            sc.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("Connection closing...");
            sckt.close();
        }

    }
}

我希望能够向单个用户或所有用户发送消息,但问题是我无法在客户端上接收消息,因为就目前而言,客户端仅在向客户端发送消息后才会收到消息服务器。所以我的想法是使用一个线程来持续监听来自服务器的消息,但它不起作用。最简单的方法是什么?

4

1 回答 1

1

您正在尝试立即使用收到的数据报,而不是等待您创建的线程接收数据报并填充您的in变量。您可以直接在线程代码中处理接收到的数据报,也可以在接收到数据报时使用 wait() / notify() 方案向主线程发出信号。

于 2013-05-13T21:36:24.590 回答