我有两个课程:chat_client和chat_server。 我必须打开服务器,然后其他人必须打开客户端。如果我们中的一个人在文本字段中键入内容并单击“发送”,则另一个将在文本区域中接收它。 如果我使用localhost或我的Ipv4地址并在本地运行它,一切都按预期工作。
s = new Socket("127.0.0.1", 1300);
我打开服务器并要求朋友打开客户端并且没有任何工作(我使用我的ipv4作为Socket中的第一个参数(主机,端口));
chat_client.java:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.*;
import java.io.*;
import java.applet.*;
import java.awt.*;
public class chat_client extends JFrame {
private static JPanel contentPane;
private static JTextField textField;
private static JTextArea textArea;
private static DataOutputStream dos;
private static DataInputStream dis;
private static Socket s;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
chat_client frame = new chat_client();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
try {
s = new Socket("127.0.0.1", 1300);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String msg = "";
while(true)
{
try {
msg = dis.readUTF();
String text = textArea.getText().trim() + "\n" + msg;
textArea.setText(text);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* Create the frame.
*/
public chat_client() {
setTitle("Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(82, 209, 123, 20);
contentPane.add(textField);
textField.setColumns(10);
JButton btnSend = new JButton("Send");
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String msg = textField.getText();
try {
dos.writeUTF(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textField.setText(null);
}
});
btnSend.setBounds(253, 208, 89, 23);
contentPane.add(btnSend);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(53, 27, 315, 145);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
}
chat_server.java:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.TextArea;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class chat_server extends JFrame {
private JPanel contentPane;
private JTextField textField;
private static ServerSocket ss;
private static Socket s;
private static DataInputStream dis;
private static DataOutputStream dos;
private static JTextArea textArea;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
chat_server frame = new chat_server();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
try {
ss = new ServerSocket(1300);
s = ss.accept();
JOptionPane.showMessageDialog(null, "Client logged!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String msg = "";
while(true)
{
try {
msg = dis.readUTF();
String text = textArea.getText().trim() + "\n" + msg.trim();
textArea.setText(text);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* Create the frame.
*/
public chat_server() {
setTitle("Server");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(53, 34, 332, 142);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
textField = new JTextField();
textField.setColumns(10);
textField.setBounds(94, 205, 123, 20);
contentPane.add(textField);
JButton button = new JButton("Send");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String msg = "";
msg = textField.getText().trim();
try {
dos.writeUTF(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
textField.setText("");
}
});
button.setBounds(270, 204, 89, 23);
contentPane.add(button);
}
}
如果您正在测试它,请先运行服务器,然后再运行客户端。如果有效,将显示消息框。
答案 0 :(得分:0)
请勿在客户端代码中对loopback ip(127.0.0.0)进行硬编码。您的朋友不在同一台计算机上,无法访问您的服务器