import java.net.*;
import java.io.*;
import java.util.*;class Server {
boolean started = false;
ServerSocket ss = null;
List<ClientRun> list = new ArrayList<ClientRun>();
public static void main(String[] args) {
new Server().start();
} public void start()
{
try
{
ss = new ServerSocket(8888);
started = true;
}
catch (BindException be)
{
System.out.println("端口使用中!");
}
catch (IOException e)
{
System.out.println("服务器连接失败!");
}
try
{
while (started) {
Socket s = ss.accept();
ClientRun cr = new ClientRun(s);        //主线程只负责接收信息,每个客户端连接进来都会开始一个新线程
new Thread(cr).start();    //把连接进来的Socket传到线程中。
list.add(cr);
System.out.println("系统提示:"+s.getInetAddress()+"已经联入");
}
}
catch (IOException e)
{
System.out.println("Client closed!");
}
}
/***********************************多线程实现多客户端同时连接**************************************************/
class ClientRun implements Runnable
{
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
boolean bConnect = false;
public ClientRun(Socket s)                        
{
this.s = s;
try
{
dis = new DataInputStream(s.getInputStream());   
dos = new DataOutputStream(s.getOutputStream());
bConnect = true;
}
catch (IOException e)
{
e.printStackTrace();
}
} public void send(String str)
{
try
{
dos.writeUTF(str);
}
catch (IOException e)
{
e.printStackTrace();
}

}
public void run()
{
try
{
while (bConnect)
{
String str = dis.readUTF();
System.out.println(str);
for (int i=0 ;i<list.size() ;i++ )
{
ClientRun cr = list.get(i);
cr.send(str);
}
}
}
catch (Exception e)
{
System.out.println(s.getInetAddress()+"离开了!");
}
finally
{
try
{
if (dis != null) dis.close();
if (dos != null) dos.close();
if (s != null){
s.close();
s = null;
}
}
catch (IOException io)
{
io.printStackTrace();
}
}

}
}
}

解决方案 »

  1.   


    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    import java.util.*;class Client {
    public static void main(String[] args) {
    MyFrame fram = new MyFrame();
    }
    }class MyFrame extends Frame {
    Panel p1;
    Panel p2;
    Panel p3;
    TextArea show;
    TextField input;
    Label port;
    Label ip;
    //Label name;
    TextField tPort;
    TextField tIp;
    //TextField tName;
    Button submit;
    Button login;
    Socket s = null;
    DataOutputStream  dos = null;
    DataInputStream dis = null;
    private boolean bConnect = false; public MyFrame() {
    init();
    }
    public void init() {
    /***********************************show begin****************************************************/
    p1 = new Panel();
    show = new TextArea(15,80);
    show.setEditable(false);
    p1.add(show);
    add(p1,BorderLayout.CENTER);
    /***********************************show end****************************************************/ /***********************************Panel2 begin****************************************************/
    p2 = new Panel(new FlowLayout(FlowLayout.LEFT));
    port = new Label("PORT");
    tPort  = new TextField(7);
    ip = new Label("IP");
    tIp = new TextField(25);
    //name = new Label("NAME");
    //tName = new TextField(7);
    login = new Button("login");
    p2.add(port);
    p2.add(tPort);
    p2.add(ip);
    p2.add(tIp);
    //p2.add(name);
    //p2.add(tName);
    p2.add(login);
    login.addActionListener(new submitAction());
    add(p2,BorderLayout.NORTH);
    /***********************************Panel2 end****************************************************/

    /***********************************Panel3 begin****************************************************/
    p3 = new Panel(new FlowLayout(FlowLayout.LEFT));
    input = new TextField(70);
    submit = new Button("submit");
    p3.add(input);
    p3.add(submit);
    input.addActionListener(new inputAction());
    submit.addActionListener(new submitAction());
    add(p3,BorderLayout.SOUTH);
    /***********************************Panel3 end****************************************************/
    /********************************fram begin************************************************/
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    //disConnect();
    System.exit(0);
    }
    });
    this.setSize(300, 300);
    setLocation(100,100);
    pack();
    setVisible(true);
    /********************************fram end************************************************/
    }
    class submitAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    if ("submit".equals(e.getActionCommand())) {
    String str = input.getText().trim();
    //show.setText(show.getText() + str + '\n');
    input.setText("");
    faSong(str);

    }else if ("login".equals(e.getActionCommand())) {
    connect();
    }
    }
    }
    /****************************************回车输出事件*****************************************************/
    class inputAction implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    String str = input.getText().trim();
    //show.setText(show.getText() + str + '\n');
    input.setText("");
    faSong(str);
    }
    }
    /*******************************向服务端发送信息*******************************************************/
    public void faSong(String str)
    {
    try
    {
    dos.writeUTF(str);
    dos.flush();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }
    /*******************************关闭资源*******************************************************/
    /*public void disConnect()
    {
    try
    {
    dos.close();
    }
    catch (IOException e)
    {
    //e.printStackTrace();
    System.out.println("系统错误");
    }

    }*/
    /*******************************************接收服务器发送的信息******************************************************/
    private class RecvClient implements Runnable
    {
    public void run()
    {
    try
    {
    while (bConnect)
    {
    String str = dis.readUTF();
    show.setText(show.getText()+str+'\n');
    }
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }
    }
    /*******************************connection begin*******************************************************/
    public void connect() {
    try
    {
    s = new Socket(tIp.getText(),Integer.parseInt(tPort.getText()));
    dos = new DataOutputStream(s.getOutputStream());
    dis = new DataInputStream(s.getInputStream());
    bConnect = true;
    new Thread(new RecvClient()).start();
    System.out.println("我连进来啦!~哈哈~~");
    }
    catch (UnknownHostException uhe)
    {
    uhe.printStackTrace();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }
    }