通信的时候,就是互发信息的时候,客户端可以直接和服务器端互发信息么?我见好多都是客户端通过服务器端和另一个客户端互发信息。

解决方案 »

  1.   

    客户端和客户端通过服务器通信
    以下是客户端
    import java.net.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;public class Client extends Frame { DataOutputStream dos;
    DataInputStream dis;
    Socket s = null;
    TextField tf = new TextField();
    TextArea ta = new TextArea();
    private boolean ifConnect = false;
    Thread recvThread = new Thread(new RecvThread());

    public static void main(String[] args) {
    new Client().runFrame();
    }

    public void runFrame() {
    this.setLocation(400,300);
    this.setSize(400,300);
    add(ta,BorderLayout.NORTH);
    add(tf,BorderLayout.SOUTH);
    this.pack();
    this.addWindowListener(new WindowsMonitor());
    tf.addActionListener(new TextFieldLietener());
    setVisible(true);
    connect();

    recvThread.start();
    }

    private void connect() {
    try {
    s = new Socket("127.0.0.1",8888);
    dos = new DataOutputStream(s.getOutputStream());
    dis = new DataInputStream(s.getInputStream());
    ifConnect = true;
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private class WindowsMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) {

    try {
    ifConnect = false;
    dos.close();
    dis.close();
    s.close();
    } catch(IOException e1) {
    e1.printStackTrace();
    }
    /*try {
    ifConnect = false;
    recvThread.join();
    dos.close();
    dis.close();
    s.close();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    } catch (IOException e1) {
    e1.printStackTrace();
    } finally {
    try {
    dos.close();
    dis.close();
    s.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }*/
    System.exit(0);
    }

    } private class TextFieldLietener implements ActionListener { public void actionPerformed(ActionEvent arg0) {
    String str = tf.getText();
    tf.setText("");

    try {
    dos.writeUTF(str);
    dos.flush();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    } private class RecvThread implements Runnable { public void run() {
    while(ifConnect) {
    try{
    String str = dis.readUTF();
    // System.out.println(str);
    ta.setText(ta.getText() + str + '\n');
    } catch(SocketException e) {
    System.out.println("退出了!");
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
    以下是服务器
    import java.io.*;
    import java.net.*;
    import java.util.*;public class Server {

    ServerSocket ss = null;
    boolean started = false;

    List<ClientListen> clients = new ArrayList<ClientListen>();

    public static void main(String[] args) {
    new Server().starting();
    }

    public void starting() {
    try {
    ss = new ServerSocket(8888);
    started = true;
    } catch(BindException e ) {
    System.out.println("端口使用中...");
    System.exit(0);
    } catch (IOException e) {
    e.printStackTrace();
    }

    try {
    while(started) {
    Socket s = ss.accept();
    //System.out.println("a client connected!");
    ClientListen c = new ClientListen(s);
    new Thread(c).start();
    clients.add(c);
    // dis.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }finally {
    try {
    ss.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    class ClientListen implements Runnable {
    private Socket s;
    private DataInputStream dis = null;
    private DataOutputStream dos = null;
    private boolean ifstart = false;

    public ClientListen(Socket s ) {
    this.s = s;
    try {
    dis = new DataInputStream(s.getInputStream());
    dos = new DataOutputStream(s.getOutputStream());
    ifstart = true;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void send(String str) {
    try {
    dos.writeUTF(str);
    } catch (IOException e) {
    clients.remove(this);
    System.out.println("对方退出了!");
    //e.printStackTrace();
    }
    }

    public void run() {
    try{
    while(ifstart) {
    String str = dis.readUTF(); 
    //System.out.println(str);
    for(int i = 0;i < clients.size();i++) {
    ClientListen c = clients.get(i);
    c.send(str);
    }
    }
    } catch(SocketException e) {
    clients.remove(this);
    System.out.println("一个客户端已经退出!");
    }
    catch (EOFException e) {
    System.out.println("客户端退出!");
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if(dis != null) dis.close();
    if(dos != null) dos.close();
    if(s != null) s.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
    }
    }
    }
      

  2.   

    好像服务器的输入内容必须是socket流提供的吧?那这样就不能通过键盘输入了吧?这样服务器端好像就不能输入了吧?