这里模仿了一个网络聊天的工具,服务器(server)放在我的电脑上(我的电脑通过路由器,adsl上网,)一个客户端放在我电脑上,另一个客户端是我的一个同学,他是外网,他要连我的服务器端的Socket(ip,port),总是连不上。//服务器代码
package chatpacket;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.io.*;
public class Server {
ServerSocket ss;
List<Theother> arr;
    public static void main(String [] args){
     new Server().launch();
    }
    
   
    
    private void launch(){
     try {
    
ss=new ServerSocket(8888);
arr=new ArrayList<Theother>();
while(true){
Socket s=ss.accept();
Theother t=new Theother(s);
new Thread(t).start();
arr.add(t);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
    }
    private class Theother implements Runnable{
        Socket s;
        DataInputStream dis;
        DataOutputStream dos;
@Override
public void run() {
// TODO Auto-generated method stub
boolean bl=true;
    try {
while(bl){

String str=dis.readUTF();
System.out.println(str);
for(int i=0;i<arr.size();i++){
Theother t=arr.get(i);
t.send(str);
}

}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("a client leave");
} finally{


try {
if(dos!=null)  dos.close();
if(dis!=null)  dis.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
     private void send(String s){
     try {
dos.writeUTF(s);
dos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
     }
Theother(Socket s){
this.s=s;
try {
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
    }
}
//外网的客户端
package chatpacket;
import java.net.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;public class ChatClient1 extends Frame { /**
 * @param args
 */
private TextArea t0;
private TextArea t1;
private Button b0;
    private Socket s,s0;
    private DataOutputStream dos;
    private DataInputStream dis;
public static void main(String[] args) {
// TODO Auto-generated method stub
new ChatClient1().lanchFrame();
} ChatClient1() {
super("客户端");
} public void lanchFrame() {
t0 = new TextArea();
t1 = new TextArea();

t0.setSize(200, 300);
t1.setSize(200, 100);
t0.setEditable(false);
Panel p0 = new Panel(new BorderLayout());
p0.add(t0);
p0.add(t1, BorderLayout.SOUTH);
this.add(p0); Panel p1 = new Panel();
b0 = new Button("发送");
b0.addActionListener(new Btlistener());
p1.add(b0);
this.add(p1, BorderLayout.SOUTH);
this.setSize(500, 550);
this.setLocation(400, 100);
this.setResizable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
disconnect();
System.exit(0);
}
});
connect();
rec();

}
private void connect(){
try{
  s=new Socket("125.40.224.242",8888);
  dos=new DataOutputStream(s.getOutputStream());
}catch(IOException e){
e.printStackTrace();
JOptionPane.showConfirmDialog(this, "连接失败","提示",JOptionPane.DEFAULT_OPTION);
}
}
private void rec(){
try {
dis=new DataInputStream(s.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean bl=true;
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");

while(bl){
try {
String str=dis.readUTF();
t0.append(sdf.format(new Date())+" "+str.replace("\n", "\n        ")+"\n");
} catch (IOException e) {
bl=false;
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
}

private class Btlistener implements ActionListener {
public void actionPerformed(ActionEvent e){
String str0=t1.getText();
//String str=str0.replace("\n", "\n        ");
t1.setText("");
//SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
//t0.append(sdf.format(new Date())+"    client :\n"+" "+str+"\n");
try {

dos.writeUTF("客户端: \n"+str0.trim());
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
private void disconnect(){
try{
dis.close();
dos.close();
    s.close();
   
}catch(IOException e){
e.printStackTrace();
}    
}

}