客户端模拟
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 9000);
OutputStream out = socket.getOutputStream();
out.write("上传文件".getBytes());
InputStream in = socket.getInputStream();
byte[] bytes = new byte[1024];
int readCount = in.read(bytes);
System.out.println(new String(bytes, 0, readCount));
socket.close();
}
}
服务器端模拟
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9000);
Socket client = serverSocket.accept();
InputStream in = client.getInputStream();
byte[] bytes = new byte[1024];
int readCount = in.read(bytes);
System.out.println(new String(bytes, 0, readCount));
OutputStream out = client.getOutputStream();
out.write("发送成功".getBytes());
serverSocket.close();
client.close();
}
}