我正在尝试将Client Class连接到另一台计算机中使用的服务器 与另一个IP ...我写了服务器的IP和Socket中的相同端口,但没有连接...当我在同一台计算机上运行服务器和客户端时,我成功连接到服务器但是当我在ip中写道127.0.0.1
抱歉我的蹩脚英语...服务器类:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import javax.swing.*;
public class ClientSide {
Socket client;
ObjectOutputStream out1;
//////////////////////
JFrame frame=new JFrame("Client");
JTextArea ta=new JTextArea(20,10);
JButton btn=new JButton("Enter");
JTextArea send=new JTextArea(10,5);
///////////////////////
/////Gui////
ClientSide(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(600,500);
JPanel p=new JPanel(new BorderLayout());
frame.add(p);
JScrollPane spta=new JScrollPane(ta);
p.add(btn,BorderLayout.EAST);
p.add(spta,BorderLayout.CENTER);
p.add(send,BorderLayout.SOUTH);
ta.setBackground(Color.cyan);
frame.revalidate();
////set Client//
try {
client=new Socket("127.0.0.1",8080);
ta.setText(" -Connection-");
////set Output/////
out1=new ObjectOutputStream( client.getOutputStream());
out1.flush();
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
out1.writeObject(send.getText());
out1.flush();
ta.setText(ta.getText()+"\nYou:"+send.getText());
} catch (IOException e1) {
e1.printStackTrace();
}
if (send.getText().equals("Bye"))
try {
client.close();
frame.dispose();
} catch (IOException e) {
e.printStackTrace();
}
}
});
//////set Input////
ObjectInputStream in = new ObjectInputStream(client.getInputStream());
Object inputLine; ;
while(true){
try {
out1.flush();
inputLine = in.readObject();
if(inputLine!=null){
ta.setText(ta.getText()+"\nClient:"+inputLine);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ClientSide();
}
}
客户端类:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;
public class ServerSide {
ObjectOutputStream out;
ServerSocket ss;
Socket so;
JFrame frame=new JFrame("Server");
///////
JTextArea ta=new JTextArea(20,10);
JTextArea send=new JTextArea(10,5);
JButton enter=new JButton("Enter");
////set Gui////
ServerSide(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame .setVisible(true);
frame. setSize(600,500);
JPanel p=new JPanel(new BorderLayout());
frame. add(p);
JScrollPane spta=new JScrollPane(ta);
p.add(enter,BorderLayout.EAST);
p.add(spta,BorderLayout.CENTER);
p.add(send,BorderLayout.SOUTH);
ta.setBackground(Color.cyan);
frame.revalidate();
/////Server///
try {
int port=8080;
System.out.println(port);
ss = new ServerSocket(8080);
so=ss.accept();
ta.setText(ta.getText()+"\n -Connection-");
/////set Output////
out = new ObjectOutputStream(so.getOutputStream());
out.flush();
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
Object ob=send.getText();
out.writeObject(ob);
SwingUtilities.invokeLater(new Runnable(){
public void run() {
ta.setText(ta.getText()+"\nYou:"+send.getText());
}
});
} catch (IOException e1) {
e1.printStackTrace();
}
if (send.getText().equals("Bye"))
try {
so.close();
ss.close();
frame.dispose();
} catch (IOException e) {
e.printStackTrace();
}
}
});
/////set Input///
ObjectInputStream in = new ObjectInputStream(so.getInputStream());
String ob ;
while(true){
try {
ob = (String) in.readObject();
if(ob!=null){
ta.setText(ta.getText()+"\nClient:"+ob);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ServerSide();
}
}
异常:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at ControlMouseProject.ClientSide.<init>(ClientSide.java:41)
at ControlMouseProject.ClientSide.main(ClientSide.java:104)
line 41:
client=new Socket("IP",8080);
答案 0 :(得分:0)
禁用服务器PC的个人防火墙。我不得不在我的防病毒软件中禁用我的服务器pc的个人防火墙来解决这个问题。希望它也适合你。