简介
RT
code
package com.kuang;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Created by lee on 2021/4/1.
*/
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket socket =serverSocket.accept();
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
while((len=is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
baos.close();
is.close();
socket.close();
serverSocket.close();
}
}
package com.kuang;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* Created by lee on 2021/4/1.
*/
public class TcpClient {
public static void main(String[] args) throws IOException {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(serverIP, port);
OutputStream os = socket.getOutputStream();
os.write("adfdsafdsafda".getBytes());
os.close();
socket.close();
}
}