当前位置: 代码迷 >> Java相关 >> socket 客户端发送字节流到服务器,该怎么解决
  详细解决方案

socket 客户端发送字节流到服务器,该怎么解决

热度:9635   发布时间:2013-02-25 21:44:42.0
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();    }}
  相关解决方案