我正在尝试构建一个聊天程序。我编写了代码,一切正常,当我使用我的电脑并使用 127.0.0.1 连接时,它仍然运行良好。我还成功地在我的笔记本电脑和我的计算机之间成功连接,它们在同一路由器上运行。(我使用内部 IP 地址来执行此操作,10.0.0.3)。
现在我正在尝试在我的路由器和其他路由器之间建立连接。为此,我正在尝试连接到外部 IP 地址。我通过路由器进行端口转发部分,还创建了一个静态 IP。当我运行代码时,我总是收到“连接被拒绝错误”。
这是代码:
MainServer.java:
import java.util.*;
import java.io.*;
import java.net.*;
public class MainServer {
private ArrayList<Socket> sockets;
public MainServer() {
ServerSocket server_socket;
try {
server_socket = new ServerSocket(5005);
sockets = new ArrayList<Socket>();
System.out.println("server is now running");
while(true) {
Socket socket = server_socket.accept();
sockets.add(socket);
try {
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.println("---you are connected to the server---\r\n");
writer.flush();
} catch(Exception e) {e.printStackTrace();}
System.out.println("server connected to " + socket.getInetAddress());
Reader reader = new Reader(socket);
Thread thread = new Thread(reader);
thread.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
MainServer server = new MainServer();
}
class Reader implements Runnable {
Socket socket;
public Reader(Socket socket) {
this.socket=socket;
}
public void run() {
while(true) {
try {
InputStreamReader stream_reader = new InputStreamReader(socket.getInputStream());
BufferedReader reader = new BufferedReader(stream_reader);
while(true) {
String str = reader.readLine();
if(str==null)
continue;
System.out.println("message from the client " + socket.getInetAddress() + ": " + str);
send_back_message(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void send_back_message(String str) {
try {
for(Socket send_to_socket: sockets) {
PrintWriter writer = new PrintWriter(send_to_socket.getOutputStream());
writer.println(send_to_socket.getInetAddress()+ ": " + str);
writer.flush();
}
} catch(Exception e) {e.printStackTrace();}
}
}
}
客户端.java:
public Client() {
frame = new JFrame();
JPanel panel = new JPanel();
chat = new JTextArea(20,40);
chat.setEditable(false);
JScrollPane scroll = new JScrollPane(chat);
text = new JTextField(32);
JButton send = new JButton("Send");
send.addActionListener(new SendButtonListener());
panel.add(scroll);
panel.add(text);
panel.add(send);
frame.getContentPane().add(panel);
frame.setSize(500,500);
frame.setVisible(true);
try {
socket = new Socket("77.126.189.65",5005);
} catch (Exception e) {
e.printStackTrace();
}
Thread thread = new Thread(new ClientReader());
thread.start();
}
public static void main(String[] args) {
Client client = new Client();
}
class SendButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.println(text.getText());
writer.flush();
text.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
class ClientReader implements Runnable {
public void run() {
try {
InputStreamReader stream_reader = new InputStreamReader(socket.getInputStream());
BufferedReader reader = new BufferedReader(stream_reader);
while(true) {
String str = reader.readLine();
if(str==null)
continue;
chat.setText(chat.getText() + str + "\r\n" );
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我也试过这个工具。当我运行 MainServer 文件并尝试该工具时,我得到了肯定的答案。我也对自己的 Eclipse 持肯定态度。当我的 MainServer 成功连接时,它会使用以下几行打印一条关于它的消息:
Socket socket = server_socket.accept();
System.out.println("server connected to " + socket.getInetAddress());
所以每次我点击上面工具中的“检查”按钮时,我都会在我的 Eclipse 控制台(system.out.println 部分)中收到一条消息:
服务器连接到 /69.163.149.200
因此我认为问题可能没有连接到 MainServer 或 portforwading/firewall/static IP。
我还认为可能会出现问题,因为我正在尝试从我自己的路由器设备连接到我自己的路由器。我将在接下来的半小时内将 MainServer 文件保持打开状态,因此如果有人可以在他的计算机上运行 Client.java,那将会很有帮助。