客户端向服务发送数据。服务器收到数据后给客户端反馈。
可是程序运行之后并没有返回数据给客户端,而且程序也没有停下来。
class TcoClient{
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\abs.apk"));
Socket socket = new Socket(InetAddress.getLocalHost(), 10000);
OutputStream os = socket.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) != -1) {
os.write(buf, 0, len);
}
InputStream is = socket.getInputStream();
int backLen = is.read(buf);
System.out.println(new String(buf, 0, backLen));
os.close();
}
}
class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(10000);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\cde.apk"));
byte[] buf = new byte[1024*1024];
int len;
while ((len = is.read(buf)) != -1) {
bos.write(buf, 0, len);
}
OutputStream os = socket.getOutputStream();
os.write("文件传输完成".getBytes());
}
叮当猫咪
相关分类