1.使用字节流
,客户端給服务器发一句hello serve
, 服务器接受到了, 然后在控制台输出
//服务器
@Test
public void server() throws IOException {
//1.监听端口 等待连接
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("等待连接。。。");
//2. 如果有客户端连接, 返回一个socket对象
Socket accept = serverSocket.accept();
System.out.println("socket=" + accept);
//3. 获取输入流对象
InputStream inputStream = accept.getInputStream();
byte[] b = new byte[1024];
int readLen = 0;
//4.获取数据
while ((readLen = inputStream.read(b)) != -1) {
System.out.println(new java.lang.String(b, 0, readLen));
}
//5. 关闭
inputStream.close();
accept.close();
serverSocket.close();
}
//客户端
@Test
public void client() throws IOException {
//1.连接服务器
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println(socket);
//2.获得socket关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//3.写数据到数据通道
outputStream.write("hello server".getBytes());
System.out.println("hello server".getBytes());
//4.关闭
outputStream.close();
socket.close();
}
2.使用字节流
,在一的条件下,服务器给客户端回一句,hello client
,注意:写入数据完成以后,设置结束标记
@Test
public void server() throws IOException {
//1.监听端口 等待连接
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("等待连接。。。");
//2. 如果有客户端连接, 返回一个socket对象
Socket accept = serverSocket.accept();
System.out.println("socket=" + accept);
//3. 获取输入流对象
InputStream inputStream = accept.getInputStream();
byte[] b = new byte[1024];
int readLen = 0;
//4.获取数据
while ((readLen = inputStream.read(b)) != -1) {
System.out.println(new java.lang.String(b, 0, readLen));
}
//5.获取socket输出流
OutputStream outputStream = accept.getOutputStream();
outputStream.write("hello client".getBytes());
//6.设置结束标记
accept.shutdownOutput();
//7. 关闭
outputStream.close();
inputStream.close();
accept.close();
serverSocket.close();
}
@Test
public void client() throws IOException {
//1.连接服务器
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println(socket);
//2.获得socket关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//3.写数据到数据通道
outputStream.write("hello server".getBytes());
//4.设置结束标记
socket.shutdownOutput();
//5.获取输入流
InputStream inputStream = socket.getInputStream();
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(buf)) != -1){
System.out.println(new String(buf, 0, readLen));
}
//6.关闭
outputStream.close();
socket.close();
}
3.用字符流
完成2题
字节流转换字符流
//3.写数据到数据通道
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello server---字符流");
bufferedWriter.newLine();//插入一个换行, 表示写入内容结束 对方也需要使用readLine()
bufferedWriter.flush(); //字符流必须手动刷新
//写入数据
InputStream inputStream = accept.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
System.out.println(bufferedReader.readLine());
完整代码
@Test
public void server() throws IOException {
//1.监听端口 等待连接
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("等待连接。。。");
//2. 如果有客户端连接, 返回一个socket对象
Socket accept = serverSocket.accept();
System.out.println("socket=" + accept);
//3.使用字符流获得数据
InputStream inputStream = accept.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
System.out.println(bufferedReader.readLine());
//5.获取socket输出流, 写入数据
OutputStream outputStream = accept.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello client---字符流");
bufferedWriter.newLine();//插入一个换行, 表示写入内容结束 对方也需要使用readLine()
bufferedWriter.flush(); //字符流必须手动刷新
//6.设置结束标记
accept.shutdownOutput();
//7. 关闭
outputStream.close();
inputStream.close();
accept.close();
serverSocket.close();
}
@Test
public void client() throws IOException {
//1.连接服务器
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println(socket);
//2.获得socket关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
//3.写数据到数据通道
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello server---字符流");
bufferedWriter.newLine();//插入一个换行, 表示写入内容结束 对方也需要使用readLine()
bufferedWriter.flush(); //字符流必须手动刷新
//4.设置结束标记
socket.shutdownOutput();
//5.获取输入流, 输出数据
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
System.out.println(bufferedReader.readLine());
//6.关闭
outputStream.close();
socket.close();
}
- 使用UDP,A给B发送一句话, B收到了以后,回复一句话
@Test
public void A() throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
System.out.println("正在等待B给我传数据。。。。");
socket.receive(packet);
int length = packet.getLength();
byte[] data = packet.getData();
String ans = new String(data, 0, length);
System.out.println(ans);
//接收到了 现在回他一句话
byte[] data2 = "走吧,咱去吃饭".getBytes();
DatagramPacket packet1 = new DatagramPacket(data2, data2.length, InetAddress.getByName("172.21.145.16"), 8889);
socket.send(packet1);
socket.close();
}
@Test
public void B() throws IOException {
DatagramSocket socket = new DatagramSocket(8889);
byte[] data = "走,吃饭了".getBytes();
DatagramPacket packet1 = new DatagramPacket(data, data.length, InetAddress.getByName("172.21.145.16"), 8888);
socket.send(packet1);
//发送完毕, 现在去接收消息
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
System.out.println("正在等待B给我传数据。。。。");
socket.receive(packet);
int length = packet.getLength();
byte[] data2 = packet.getData();
String ans = new String(data2, 0, length);
System.out.println(ans);
socket.close();
}