如题.

解决方案 »

  1.   

    //server.java
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Server extends JFrame {
    private JTextField enter;
    private JTextArea display;
    ObjectOutputStream output;
    ObjectInputStream input;
    public Server()
    {
    super("Server");
    Container c =getContentPane();
    enter=new JTextField();
    enter.setEnabled(false);
    enter.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    sendData(e.getActionCommand());
    }
    });
    c.add(enter,BorderLayout.NORTH);
    display=new JTextArea();
    c.add(new JScrollPane(display),BorderLayout.CENTER);
    setSize(300,150);
    show();
    }
    public void runServer(){
    ServerSocket server;
    Socket connection;
    int counter=1;
    try{
    //第一步:create a ServerSocket.
    server=new ServerSocket(5000,100);
    while(true){
    //第二步:wait for a connection.
    display.setText("等待连接\n");
    connection=server.accept();
    display.append("connection"+counter+" received from: "+connection.getInetAddress().getHostName());
    //step 3:获得input和output 流
    output=new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    input=new ObjectInputStream(connection.getInputStream());
    display.append("\n Got I/O stream \n");
    //step 4:连接过程.process connection.
    String message="SERVER>>> Connection successful";
    output.writeObject(message);
    output.flush();
    enter.setEnabled(true);
    do{
    try{
    message=(String) input.readObject();
    display.append("\n"+message);
    display.setCaretPosition(display.getText().length());
    catch(ClassNotFoundException cnfex) {
    display.append("\n Unknown object type received");
    }
    }while(!message.equals("CLIENT>>> TERMINATE"));
    //step 5:close connection.
    display.append("\n USER terminated connection");
    enter.setEnabled(false);
    output.close();
    input.close();
    connection.close();
    ++counter;

    }
    }
    catch(EOFException eof){
    System.out.println("Client terminated connection");

    }
    catch(IOException io){
    io.printStackTrace();
    }
    }
    private void sendData(String s){
    try{
    output.writeObject("SERVER>>>"+s);
    output.flush();
    display.append("\nSERVER>>>"+S);
    }
    catch(IOException cnfex){
    display.append("\nError writing object");
    }
    }
    public static void main(String args[]){
    Server app=new Server();
    app.addWindowListener({
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    app.runServer();
    }
    }//client.java
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Client extends JFrame{
    private JTextField enter;
    private JTextArea display;
    ObjectOutputStream output;
    ObjectInputStream input;
    public Client(){
    super("Client");
    Container c =getContentPane();
    enter=new JTextField();
    enter.setEnabled(false);
    enter.addActionListener(new ActionListener(){
    public void axtionPerformed(ActionEvent e){
    sendData(e.getActionCommand());
    }
    });
    c.add(enter,BorderLayout.NORTH);
    display=new JTextArea();
    c.add(new JScrollPane(display),BorderLayout.CENTER);
    setsize(300,150);
    show();
    }
    public void runClient(){
    Socket client;
    try{
    //step1:create a socket connection.
    display.setText("Attempting connection\n");
    client=new Socket(InetAddress.getByName("127.0.0.1"),5000);
    display.append("connected to :"+client.getInetAddress().getHostName());
    //step 2:get the input and output streams.
    output=new ObjectOutputStream(client.getOutputStream());
    output.flush();
    input=new ObjectInputStream(client.getInputStream());
    display.append("\n GOT I/O streams\n");
    //step 3:process connection.
    enter.setEnabled(true);
    do{
    try{
    message=(String) input.readObject();
    display.append("\n"+message);
    display.setCaretPosition(display.getText().length());
    }catch( ClassNotFoundException cnfex ){
    display.append("\nUnknown object type received");
    }
    }while(!message.equals("SERVER>>>TERMINATE"));
    display.append("Closing connection.\n");
    input.close();
    output.close();
    client.close();
    }
    catch(EOFException eof){
    System.out.println("Server terminated connection");
    }
    catch(IOException e){
    e.printStackTrace();
    }
    }
    private void sendData(String s){
    try{
    message=s;
    output.writeObject("CLIENT>>>"+s);
    output.flush();
    display.append("\nCLIENT>>>"+s);
    }catch(IOException cnfex){
    display.append("\nError writing object");
    }
    }
    public static void main(String args[]){
    Client app=new Client();
    app.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    app.runClient();
    }
    }
    这是我想做一个局域网内的两点间聊天程序,假如我想加一个传输文件,我改怎么样做呢?