我一直在努力学习java中的网络,并且已经开始使用简单的GUI制作一个简单的客户端/服务器程序。服务器只有一个JTextArea,客户端有一个JTextField,JTextArea和一个JButton。它的工作方式是客户端用户在JTextField中键入消息并单击JButton(发送)。这将调用sendData方法,该方法接收客户端键入的消息,将其放入DatagramPacket并将数据包发送到预定义端口上的服务器。启动时的服务器创建一个线程,该线程不断循环,允许它使用DatagramSocket.recieve(DatagramPacket)方法被动地侦听数据包。如果收到数据包,它会使用数据包中的数据更新gui的JTextArea。但是,好像数据包没有到达服务器。我在局域网上测试了它,它正在运行。但是,当在互联网上使用时,数据包似乎会丢失。我尝试过两个不同的人,他们已经转发了他们的端口,并确保他们被转发。客户端和服务器都包含3个简单类。任何帮助将不胜感激。很抱歉长描述和代码转储。注意:我正在使用UDP只是因为我想学习它而且我知道TCP可以更好地用于聊天等事情。
客户的客户类:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class Client {
DatagramSocket socket;
Panel panel;
public Client(Panel panel) {
this.panel = panel;
try {
socket = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
panel.textArea.setText("Socket could not be created.");
}
}
public void sendData(byte[] data, InetAddress ipAddress){
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 27015);
try {
socket.send(packet);
panel.textArea.setText("Client: Package being sent to server...");
} catch (IOException e) {
e.printStackTrace();
panel.textArea.setText("Package could not be sent");
}
}
}
客户的Panel类:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Panel extends JPanel implements ActionListener {
JButton send;
JTextField textField;
JTextField textArea;
Client client;
InetAddress ipAddress;
public Panel(){
this.setVisible(true);
send = new JButton("Send");
send.addActionListener(this);
send.setVisible(true);
textField = new JTextField();
textField.setVisible(true);
Dimension dim = new Dimension(300,20);
textField.setPreferredSize(dim);
textArea = new JTextField();
Dimension dim2 = new Dimension(300,100);
textArea.setPreferredSize(dim2);
this.add(textField);
this.add(send);
this.add(textArea, FlowLayout.LEFT);
client = new Client(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == send){
System.out.println("Send was pressed");
String message = textField.getText();
try {
ipAddress = InetAddress.getByName("localhost");
} catch (UnknownHostException e1) {
e1.printStackTrace();
textArea.setText("Hostname could not be resolved");
}
client.sendData(message.getBytes(), ipAddress);
textArea.setText("Client: " + message);
}
}
}
Server的ServerThread类:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.JPanel;
public class ServerThread extends Thread{
private DatagramSocket socket;
Panel panel;
public ServerThread(Panel panel) {
this.panel = panel;
try {
socket = new DatagramSocket(27015);
panel.setTextArea("The server has begun listening on port 3659...");
} catch (SocketException e) {
e.printStackTrace();
panel.setTextArea("Server cant open socket and listen on port 3659");
}
}
public void run(){
while(true){
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
socket.receive(packet); // waits for packet to arrive
panel.setTextArea("Server: A package was recieved from the Client...");
} catch (IOException e) {
e.printStackTrace();
panel.setTextArea("Socket cant recieve packets");
}
String message = new String(packet.getData());
panel.setTextArea("Client: " + message);
}
}
}
注意:对于Server和Client,主类只创建一个新的Jframe和面板类的新实例,并将其设置为其内容窗格。我没有包含Server的面板类,因为它不包含任何重要的代码。
再次感谢。
答案 0 :(得分:1)
听起来网络安全配置会阻止您接收UDP数据包。您需要配置网络,使其接受指定端口上的UDP数据包。此外,您还必须将防火墙配置为接受指定端口上的UDP数据包。