package TcpTest01;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class send01 {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("192.168.3.125", 10089);
OutputStream ops = socket.getOutputStream();
ops.write("hello".getBytes(StandardCharsets.UTF_8));
InputStream is = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
String s = new String(bytes, 0, len);
System.out.println(s);
socket.close();
}
}
package TcpTest01;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class get01 {
public static void main(String[] args) throws IOException {
ServerSocket SS = new ServerSocket(10089);
Socket accept = SS.accept();
InputStream is = accept.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
String s = new String(bytes, 0, len);
System.out.println(s);
OutputStream os = accept.getOutputStream();
os.write("数据已收到".getBytes(StandardCharsets.UTF_8));
SS.close();
}
}