借助GUI来发送和接收消息/数据报:无服务器

时间:2018-11-08 09:17:39

标签: sockets jframe message-queue datagram inetaddress

这是通过控制台执行此操作的代码,但是我的任务是使用Socket类中已经提供的方法来开发GUI。这不是客户端/服务器应用。我几乎不了解使用JFrame实现ActionListener来为该项目提供不错的GUI所掌握的知识。我必须通过GUI预先声明消息要发送的IP地址(对于传出消息和传入消息,套接字在64000处是唯一的),并显示在每个连接到该IP地址的不同聊天窗口(框架)上发送和接收的消息我或我所连接的。我知道我需要一个潜在的JTextField作为发送消息的IP地址,另一个JTextField用于发送消息,一个JTextArea用于显示传入和传出的消息,一个JButton发送消息以及我的标题窗口应显示我正在与之交谈的IP地址和所使用的套接字号。对于您中的许多人来说,它可能是如此简单,但是如果您分享一些知识,我将不胜感激,因为我只是在尝试学习并变得更好。

Socket.java


import java.net.*;
import java.util.*;
import java.util.concurrent.*;





    public class Socket {

        private InetAddress myAddress;
        private int port;
        private DatagramSocket socket = null;
        private ConcurrentLinkedQueue<DatagramPacket> messageQueue = new ConcurrentLinkedQueue<DatagramPacket>();


        public Socket(int port) {
            this.port = port;
            try {
                this.myAddress = InetAddress.getLocalHost();
                this.socket = new DatagramSocket(port, this.myAddress);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(-1);
            }

            Thread receiveThread = new Thread(
                    new Runnable() {
                        public void run() {
                            receiveThread();
                        }
                    });
            receiveThread.setName("Receive Thread For Port = " + this.port);
            receiveThread.start();
        }


        public void receiveThread() {

            do {
                byte[] inBuffer = new byte[1024];
                for ( int i = 0 ; i < inBuffer.length ; i++ ) {
                    inBuffer[i] = ' ';
                }

                DatagramPacket inPacket = new DatagramPacket(inBuffer, inBuffer.length);

                try {
                    // this is a blocking call
                    socket.receive(inPacket);
                } catch(Exception e) {
                    e.printStackTrace();
                    System.exit(-1);
                }

                String message = new String(inPacket.getData());
                System.out.println("ReceiveThread - Message on port = " + this.port +"\n"+ 
                        " message = " + message + "\n" +
                        " From IP = " + inPacket.getAddress() + " -"+
                        " From Port = " + inPacket.getPort()
                );
                messageQueue.add(inPacket);
            } while(true);
        }


        public DatagramPacket receive() {
            return messageQueue.poll();
        }


        public void send(String s, InetAddress destinationIP, int destinationPort) {

            byte[] outBuffer;

            try {
                outBuffer = s.getBytes();
                DatagramPacket outPacket = new DatagramPacket(outBuffer, s.length());
                outPacket.setAddress(destinationIP);
                outPacket.setPort(destinationPort);
                socket.send(outPacket);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(-1);
            }
        }
    }

// Driver.java

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.concurrent.TimeUnit;

public class Driver {

    public static void main(String[] args) {
        Socket socket = new Socket(64000);

        InetAddress myAddress = null;

        try {
            //myAddress = InetAddress.getByName("192.168.1.xxx"); //Router B
            myAddress = InetAddress.getLocalHost();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }

        socket.send("This is Me", myAddress, 64000);
        socket.send("I am IP 192.168.1.xxx", myAddress, 64000);


        try {
            System.out.println("Main is sleeping");
            TimeUnit.SECONDS.sleep(2);
            System.out.println("Main is awake");
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        } 

        DatagramPacket packet;

        do {
            packet = socket.receive();
            if (packet != null) {
                String message = new String(packet.getData());
                System.out.println("Message = " + message);
            }
        } while(packet != null);

        System.out.println("End of fun!");
    }

}

0 个答案:

没有答案