简体   繁体   English

java-udp编程 - 从服务器到客户端的消息

[英]java-udp programming -sending message from server to the client

I made connection between server and client properly and i send message from client to the server,but how can i send messages from server to client.I mean how can i make server act like a client too.i tried to copy the client methods to the another class that server can invoke.but i couldnt then i tried to create a new package to use the client code in server class.any advices? 我正确地在服务器和客户端之间建立连接,我从客户端向服务器发送消息,但是如何从服务器向客户端发送消息。我的意思是如何让服务器像客户端一样行动。我试图将客户端方法复制到服务器可以调用的另一个类。但我不能然后我尝试创建一个新的包来使用服务器类中的客户端代码。任何建议?

ps:Sorry about my english. ps:抱歉我的英文。

public class Entrance_Server extends JFrame{

JButton buton = new JButton("Create");
JButton buton2 = new JButton("Join");
JPanel butonpanel = new JPanel();
DatagramSocket sockServer = null;
DatagramSocket sockClient = null;
int port = 7777;
String s;
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));



public Entrance_Server() {

    setLayout(new GridLayout(2,1));

    add(buton);
    add(buton2);

    buton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            Choosing c = new Choosing();
            c.start();

            System.out.println("Server socket created. Waiting for incoming data...");



        }
    });

    buton2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Choosing c = new Choosing();
            c.start();



        }
    });

}


public static void main(String[] args){

    Entrance_Server e = new Entrance_Server();
    e.setSize(500,350);
    e.setTitle("Welcome");
    e.setLocationRelativeTo(null);
    e.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    e.setVisible(true);

    e.connect();

}

public void connect (){

        try{
                sockServer = new DatagramSocket(7777);
                byte[] buffer = new byte[65536];
                DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);

                while(true)
        {
            sockServer.receive(incoming);
            byte[] data = incoming.getData();
            String s = new String(data, 0, incoming.getLength());

            //echo the details of incoming data - client ip : client port - client message
            System.out.println(incoming.getAddress().getHostAddress() + " : " + incoming.getPort() + " - " + s);

            s = "OK : " + s;
            DatagramPacket dp = new DatagramPacket(s.getBytes() , s.getBytes().length , incoming.getAddress() , incoming.getPort());
            sockServer.send(dp);

            Entrance_Client_in_Server ec = new Entrance_Client_in_Server();
            ec.connectc();


        }
            }catch(IOException i){
                System.err.println("IOException " + i);
            }



}



}

On your client u need to wait on the server response by using socket.Receive() 在您的客户端上,您需要使用socket.Receive()等待服务器响应

You can identify a client after he has send a packet to the server like you are doing. 您可以像他们一样将数据包发送到服务器后识别客户端。 You can then indentify the client like this: InetAddress address = packet.getAddress(); int port = packet.getPort(); 然后,您可以像这样识别客户端: InetAddress address = packet.getAddress(); int port = packet.getPort(); InetAddress address = packet.getAddress(); int port = packet.getPort();

And use it to send a packet back to the client, which will read the response using the socket.Receive(); 并使用它将数据包发送回客户端,客户端将使用socket.Receive()读取响应;

For further information about Client/Server connection using UDP DatagramSockets check Client-Server Datagram sockets 有关使用UDP DatagramSockets的客户端/服务器连接的详细信息,请检查客户端 - 服务器数据报套接字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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