import java.net.*;
import java.io.*;class Server{
public static void main(String[] args){
ServerSocket ss = null;
Socket s = null;
try{
ss = new ServerSocket(8888);
s = ss.accept();
System.out.println("连接到客户端……");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(s == null)
s.close();
if(ss == null)
ss.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}class Client{
public static void main(String[] args){
Socket s = null;
try{
s = new Socket("127.0.0.1",8888);
System.out.println("连接到服务端……");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(s == null)
s.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}在 new Socket() 时 IP 指定为 127.0.0.1 测试能连接上
但是使用 IP 地址就连不上了
写了了聊天程序不能联网 只能打击 让人蛋疼啊
求解……
注:本人是拨号上网,不是局域网……
import java.io.*;class Server{
public static void main(String[] args){
ServerSocket ss = null;
Socket s = null;
try{
ss = new ServerSocket(8888);
s = ss.accept();
System.out.println("连接到客户端……");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(s == null)
s.close();
if(ss == null)
ss.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}class Client{
public static void main(String[] args){
Socket s = null;
try{
s = new Socket("127.0.0.1",8888);
System.out.println("连接到服务端……");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(s == null)
s.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}在 new Socket() 时 IP 指定为 127.0.0.1 测试能连接上
但是使用 IP 地址就连不上了
写了了聊天程序不能联网 只能打击 让人蛋疼啊
求解……
注:本人是拨号上网,不是局域网……
你的电脑没有用类似tomacat的服务器,所以外部网络访问不了你的ip。
而你如果要想用ip连,那首先要人家认识你这个ip地址在哪里,包括你自己,
而你又没服务器,所以