为我提供了一个套接字类,并使用这个套接字类创建了一个GUI Messenger应用程序。应该提示用户输入要与之通信的收件人的IP地址和端口。我是套接字编程的新手,并且已经苦苦挣扎了一段时间,我不知道该如何解决。我已经创建了GUI窗口,但是我需要其他方面的帮助。
下面的代码是给定套接字类的
Socket.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
public class Socket {
public static enum SocketType {
Broadcast, NoBroadcast
};
private InetAddress address;
private int portNumber;
private DatagramSocket mySocket;
private Thread receiveThread;
private boolean receiveThreadIsRunning = true;
private ConcurrentLinkedQueue<DatagramPacket> messageQueue = new ConcurrentLinkedQueue<DatagramPacket>();
public Socket(int portNumber, SocketType socketType) {
this.portNumber = portNumber;
try {
this.address = InetAddress.getLocalHost();
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
System.exit(-1);
}
try {
switch (socketType) {
case Broadcast:
this.mySocket = new DatagramSocket(portNumber);
break;
case NoBroadcast:
this.mySocket = new DatagramSocket(portNumber, address);
break;
}
} catch (SocketException se) {
se.printStackTrace();
System.exit(-1);
}
receiveThread = new Thread(new Runnable() {
public void run() {
receiveThread();
}
});
receiveThread.setName("Receive Thread For Port = " + this.portNumber);
receiveThread.start();
}
public void receiveThread() {
try {
mySocket.setSoTimeout(50);
} catch (SocketException se) {
se.printStackTrace();
System.exit(-1);
}
do {
DatagramPacket inPacket = null;
byte[] inBuffer = new byte[1024];
for (int i = 0; i < inBuffer.length; i++) {
inBuffer[i] = ' ';
}
inPacket = new DatagramPacket(inBuffer, inBuffer.length);
try {
mySocket.receive(inPacket);
messageQueue.add(inPacket);
} catch (SocketTimeoutException ste) {
// nothing to do here
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(-1);
}
} while (receiveThreadIsRunning);
System.out.println("ReceiveThread is exiting");
}
private void stopReceiveThread() {
this.receiveThreadIsRunning = false;
}
public void send(String message, InetAddress destinationAddress, int destinationPort) {
byte[] outBuffer;
outBuffer = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(outBuffer, outBuffer.length, destinationAddress,
destinationPort);
try {
mySocket.send(sendPacket);
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(-1);
}
}
public DatagramPacket receive() {
return messageQueue.poll();
}
public InetAddress getAddress() {
return address;
}
public int getPortNumber() {
return portNumber;
}
public void close() {
stopReceiveThread();
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
System.exit(-1);
}
mySocket.close();
}
}
下面的代码是GUI窗口,询问IP地址和端口号
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.net.InetAddress;
public class netGUI extends JFrame implements ActionListener {
private JButton chat;
private JLabel IP, PortNumber;
private JPanel netGUIMain;
private JFrame netGM;
//private JFrame messageBox;
private JTextField ipNumber;
private JTextField portNum;
private JButton clearText;
int port;
InetAddress add;
public static void main(String[] args) {
new netGUI();
}
public netGUI() {
netGM = new JFrame("Connect Frame");
netGM.setVisible(true);
netGM.setSize(500, 200);
netGM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
netGUIMain = new JPanel();
netGUIMain.setBackground(Color.black);
IP = new JLabel("IP Address: ");
IP.setForeground(Color.CYAN);
netGUIMain.add(IP);
ipNumber = new JTextField(12);
netGUIMain.add(ipNumber);
PortNumber = new JLabel("Port Number: ");
PortNumber.setForeground(Color.CYAN);
netGUIMain.add(PortNumber);
portNum = new JTextField(7);
netGUIMain.add(portNum);
clearText = new JButton("CLEAR");
netGUIMain.add(clearText);
chat = new JButton("CHAT");
netGUIMain.add(chat);
netGM.add(netGUIMain);
// messageBox = new JFrame("MESSANGER");
chat.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Socket server = new Socket(port, null);
newWindow window = new newWindow();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
下面的代码是Messenger的GUI窗口(邮件将通过指定的端口号和IP地址发送给收件人)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.net.InetAddress;
public class newWindow extends JFrame implements ActionListener{
public newWindow() {
JPanel newWPanel = new JPanel();
newWPanel.setBackground(Color.BLACK);
JFrame newWFrame = new JFrame("Messanger");
newWFrame.setSize(600,600);
newWFrame.setVisible(true);
newWFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea seeMsgs = new JTextArea(25, 30);
newWPanel.add(seeMsgs);
JTextField userText = new JTextField(40);
newWPanel.add(userText);
JButton sendM = new JButton("Send");
newWPanel.add(sendM);
newWFrame.add(newWPanel);
}
/*
userText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage(e.getActionCommand());
userText.setText(" "); //makes the text
field blank again after you send a message
}
});
}
private void sendMessage(String actionCommand) {
// TODO Auto-generated method stub
}
*/
public void actionPerformed(ActionEvent e) {
}
}
基本上,当用户输入他们试图向另一个窗口发送消息的人的IP地址和端口号时,应该在该端口号和IP地址上打开该窗口,该窗口将成为消息传递窗口。这就是沟通的地方。