服务器端
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket=new ServerSocket(999);
System.out.println("开始监听");
Socket socket=serverSocket.accept(); // 铸锻
InputStream inputStream=socket.getInputStream();
byte[] buffer=new byte[100];
System.out.println("开始读数据");
int len =inputStream.read(buffer);
String string = new String(buffer,0,len);
System.out.println(string);
System.out.println("退出");
客户端
public class TcpClient {
public static void main(String[] args) throws IOException {
Socket socket=new Socket("10.25.41.36",999);
String str = "nihao 龙哥";
byte[] buffer = str.getBytes();
OutputStream os = socket.getOutputStream();
os.write(buffer,0,buffer.length);
System.out.println("退出");
}
}