日期:2014-05-20  浏览次数:20829 次

socket 客户端发送字节流到服务器
求个最简单的代码 就是客户端发送数字22 服务器接收并回复给客户端22 一定要字节流传输
谢谢各位了

------解决方案--------------------
server端:
Java code

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {
    private int port;
    public Server(int port) {
        this.port = port;
    }
    public void Listen() throws IOException{
        ServerSocket serverSocket = new ServerSocket(this.port);
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        byte[] arry = new byte[1024];
        int length = 0;
        while((length=inputStream.read(arry))!=-1){
            outputStream.write(arry, 0, length);
            outputStream.flush();
        }
    }
    public static void main(String[] args) throws IOException{
        Server server = new Server(9999);
        server.Listen();
    }
}