本帖最后由 刘明瑞 于 2012-1-18 13:21 编辑
最近视频看到java socket的编程,其中有一个问题不解,请各位指点一下 .
如下这段代码在本机上是可以正常运行的,但在客户端打包后在远端机器上运行,运行不起来,提示 fatal exception occurred!the programming will exit!
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public ServerSocket ss = null;
public Socket socket = null;
public boolean flag = true;
public static void main(String args[]) {
new Server().start();
}
public void start() {
try {
ss = new ServerSocket(9999);
} catch (IOException e) {
e.printStackTrace();
}
while(flag) {
try {
socket = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
client端:
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public Socket socket = null;
public static void main(String args[]) {
new Client().connects();
}
public void connects() {
try {
socket = new Socket(InetAddress.getByName("192.168.100.1"),9999,InetAddress.getLocalHost(),5320);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} |