我正在尝试与我的朋友一起运行客户端 - 服务器程序。它基本聊天。 但即使我给了他我的IP号码它也没有设法连接开始。 我们使用system.out。 println和客户端服务器都失败了 waitnig 1分钟打印"没有设法连接"。 请建议。
公共类MyPanel扩展了JPanel {
private JButton btnSend;
private JTextArea txt;
ServerSocket serverSocket = null;
**Server SIDE:**
public MyPanel()
{
this.setLayout(new BorderLayout());
this.add(getBtn() , BorderLayout.SOUTH);
this.add(getText() , BorderLayout.CENTER);
try{
System.out.println("here");
serverSocket = new ServerSocket(6600);
System.out.println("Server's ready");
Socket clientSocket = serverSocket.accept();
System.out.println("after accept");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream() , true);
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String temp;
while( (temp = in.readLine())!= null)
out.println(temp);
}catch(IOException e){}
}
public JButton getBtn(){
btnSend = new JButton("send");
btnSend.addActionListener(new MyAction());
return btnSend;
}
public JTextArea getText(){
txt = new JTextArea();
return txt;
}
private class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
客户端SIDE
public class Messages extends JPanel{
private JTextArea txtArea;
private JButton sendBtn;
private Socket socket;
private InetAddress address;
private PrintWriter out;
private BufferedReader in;
public Messages(){
this.setLayout(new BorderLayout());
txtArea = new JTextArea();
add(txtArea, BorderLayout.CENTER);
sendBtn = new JButton("Send");
add(sendBtn, BorderLayout.SOUTH);
addClient();
}
public void addClient(){
try{
//address = InetAddress.getByName("");
//System.out.println(address);
socket = new Socket("**here we type my ip numbeer as string", 6600);
}catch(IOException e){
System.out.println("Didn't manage to connect");
}
try{
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(IOException e){
System.out.println("Buffer problem");
}
System.out.println("Connected");
}
}
答案 0 :(得分:0)
Ping服务器机器IP&检查它是否可达。如果ping工作,则检查服务器计算机中运行的防火墙是否有阻止通信的规则。