这是用压缩方式传输传输文件的例子
=========server
int ret=0;
byte[] tmpdata=new byte[4096];
byte[] data;
FileInputStream in=new FileInputStream("Test.txt");
Socket client=server.accept();
BufferedOutputStream out=new BufferedOutputStream(new GZIPOutputStream(client.getOutputStream()));
do{
ret=in.read(tmpdata);
System.out.println(ret);
if(ret==-1) break;
data=new byte[ret];
System.arraycopy(tmpdata, 0, data, 0, ret);
out.write(data);
out.flush();
}while(true);
in.close();
out.close();==========Client:
int ret;
byte[] tmpdata=new byte[4096];
byte[] data;
Socket c=new Socket("heifei", 5050);
InputStream in=c.getInputStream();
FileOutputStream f=new FileOutputStream("Test2.gz");
do{
ret=in.read(tmpdata);
if(ret==-1)break;
data=new byte[ret];
System.arraycopy(tmpdata, 0, data, 0, ret);
f.write(data);
f.flush();
}while(true);
f.close();
in.close();
c.close();

解决方案 »

  1.   

    我也遇见过同样的问题,那是因为你的客户端已经结束,所以服务器一直不停的接受null消息,你可以在服务器端加以判断,但接受到null是关闭socket就行了。
       不知道符合不符合你的要求///
      

  2.   

    Thinking in Java 2nd Edition 里的例子,不过不是手工输入,而是程序自己输入的而已://: c15:JabberServer.java
    // Very simple server that just
    // echoes whatever the client sends.
    import java.io.*;
    import java.net.*;
    public class JabberServer {
        // Choose a port outside of the range 1-1024:
        public static final int PORT = 9090;
        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();
            }
        }
    } ///:~
    //: c15:JabberClient.java
    // Very simple client that just sends
    // lines to the server and reads lines
    // that the server sends.
    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");
            System.out.println("addr = " + addr);
            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 < 10; i ++) {
                    out.println("howdy " + i);
                    String str = in.readLine();
                    System.out.println(str);
                }
                out.println("END");
            } finally {
                System.out.println("closing...");
                socket.close();
            }
        }
    } ///:~