如:
A给B发送个消息,然后B回复A个消息。接着A给B传送个文件(A可以输入传送哪个文件),B接收到后回复给A一个收到的消息;然后A又给B传送个文件(A可以输入传送哪个文件),B接收到后又回复给A一个收到的消息.... 我自己写的老出错 。。 希望大家能帮帮我 。 谢谢 谢谢

解决方案 »

  1.   

    给你个简单例子,希望对你有帮助客户机这里:
    package server;
    import java.net.*;
    import java.io.*;public class JabberClient {
      public static void main(String[] args) 
          throws IOException {
        // Passing null to getByName() produces the
        // special "Local Loopback" IP address, for
        // testing on one machine w/o a network:
        InetAddress addr = 
          InetAddress.getByName(null);
        // Alternatively, you can use 
        // the address or name:
        // InetAddress addr = 
        //    InetAddress.getByName("127.0.0.1");
        // InetAddress addr = 
        //    InetAddress.getByName("localhost");
        
        Socket socket = 
          new Socket(addr, JabberServer.PORT);
        // Guard everything in a try-finally to make
        // sure that the socket is closed:
        try {
          System.out.println("socket = " + socket);
          BufferedReader in =
            new BufferedReader(
              new InputStreamReader(
                socket.getInputStream()));
          // Output is automatically flushed
          // by PrintWriter:
          PrintWriter out =
            new PrintWriter(
              new BufferedWriter(
                new OutputStreamWriter(
                  socket.getOutputStream())),true);
          for(int i = 0; i < 1000; i ++) {
            out.println("howdy " + i);
            String str = in.readLine();
            System.out.println(str);
          }
          out.println("END");
        } finally {
          System.out.println("closing...");
          socket.close();
        }
      }
    }服务器:package server;import java.io.*;
    import java.net.*;public class JabberServer {  
      // Choose a port outside of the range 1-1024:
      public static final int PORT = 8080;
      public static void main(String[] args) 
          throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        System.out.println("Started: " + s);
        try {
          // Blocks until a connection occurs:
          Socket socket = s.accept();
          try {
            System.out.println(
              "Connection accepted: "+ socket);
            BufferedReader in = 
              new BufferedReader(
                new InputStreamReader(
                  socket.getInputStream()));
            // Output is automatically flushed
            // by PrintWriter:
            PrintWriter out = 
              new PrintWriter(
                new BufferedWriter(
                  new OutputStreamWriter(
                    socket.getOutputStream())),true);
            while (true) {  
              String str = in.readLine();
              if (str.equals("END")) break;
              System.out.println("Echoing: " + str);
              out.println(str);
            }
          // Always close the two sockets...
          } finally {
            System.out.println("closing...");
            socket.close();
          }
        } finally {
          s.close();
        }
      } 

      

  2.   

    dzhitler7() 
    楼上的这个 我已经能实现了 。我加了个 文件传送的, 每传送完就要关闭连接。 我//掉关闭连接的句子后 程序就死那了
      

  3.   

    给你一个我以前写的例子,只能传文件。
    import java.io.*;
    import java.net.*;
    class ClientTest{
    public ClientTest(String fileName){

    try{
    Socket socket=new Socket("192.168.4.20",ServerTest.Port);
    System.out.println("Connection to Server succeed");

    File file=new File("E:\\"+fileName);
    file.createNewFile();

    BufferedInputStream in=
    new BufferedInputStream(
    new DataInputStream(socket.getInputStream())
    );

    BufferedOutputStream out=
    new BufferedOutputStream(
    new FileOutputStream(file)
    );
    int c;
    while((c=in.read())!=-1)
    out.write(c);
    out.flush();


    }
    catch(UnknownHostException e){
    System.err.println(e);
    }
    catch(IOException e){
    System.err.println(e);
    }
    }
    public static void main(String [] args){
    ClientTest app=new ClientTest("Bad.mp3");
    }
    }
    /////////////////////////////
    import java.io.*;
    import java.net.*;
    class ServerTest{
    public static final int Port=8080;

    public ServerTest(String fileName){
    try{
    ServerSocket server=new ServerSocket(Port);

    try{
    Socket connection=server.accept();
    System.out.println("Connection to client succeed");

    File file=new File(fileName);
    System.out.println(file.getName());

    BufferedInputStream in=
    new BufferedInputStream(
    new FileInputStream(file)
    );

    BufferedOutputStream out=
    new BufferedOutputStream(
    new DataOutputStream(connection.getOutputStream())
    );

    int c;
    while((c=in.read())!=-1)
    out.write(c);

    try{
    connection.close();
    }
    catch(IOException e){
    }
    }
    catch(IOException e){
    System.err.println(e);
    }
    }
    catch(IOException e){
    System.err.println(e);
    }
    }
    public  static void main(String [] args){
    ServerTest app=new ServerTest("D:\\Bad.mp3");
    }
    }
      

  4.   

    zouxinfox(Read the source,Use the force)  
    谢谢你的回复
    这步我已经会了,但是不能和发送消息结合 多次传送文件
      

  5.   

    zhmt((雪狼-独行))  
    呵呵 我开了两个线程就OK了  还是谢谢你啊
      

  6.   

    zhmt((雪狼-独行)) 
    我也要这个程序,可以发给我一下不?
      

  7.   

    zzhzzh204553(真的好想你),你给我个邮箱地址,我明天发给你,