package com.taylor.tcp;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.Test;
public class TcpServer {
@Test
public void fun() throws IOException {
ServerSocket ss = new ServerSocket(10006);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...........connect");
InputStream in =s.getInputStream();
byte[] b = new byte[1024];
int leng = in.read(b);
System.out.println(new String(b,0,leng));
s.close(); //先关客户端,后关服务端
ss.close();
}
}
package com.taylor.tcp;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
public class TcpClient {
@Test
public void fun() throws UnknownHostException, IOException {
Socket s = new Socket("172.20.10.5", 10006);
OutputStream out = s.getOutputStream();
out.write("wo shi ni da ye".getBytes());
s.close();
}
}
网络编程-udp通信1
最新推荐文章于 2023-09-27 19:10:30 发布