parent
6b67ad4397
commit
718b7f2d91
@ -0,0 +1,56 @@
|
|||||||
|
package com.msb.io.bio;
|
||||||
|
|
||||||
|
import jdk.internal.util.xml.impl.Input;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author bingor
|
||||||
|
* @Date 2023-05-05 22:27
|
||||||
|
* @Description: com.msb.io.bio
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public class BioClient {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Socket socket = null;
|
||||||
|
OutputStream outputStream = null;
|
||||||
|
InputStream inputStream = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
socket = new Socket("127.0.0.1", 8888);
|
||||||
|
outputStream = socket.getOutputStream();
|
||||||
|
outputStream.write("服务端,你好,我是小袁".getBytes());
|
||||||
|
|
||||||
|
byte [] bytes = new byte[1024];
|
||||||
|
inputStream = socket.getInputStream();
|
||||||
|
int len = inputStream.read(bytes);
|
||||||
|
String msg = new String(bytes, 0, len);
|
||||||
|
System.out.println("客户端接收到的消息:" + msg);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
try {
|
||||||
|
if(socket != null) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
if(outputStream != null) {
|
||||||
|
outputStream.close();
|
||||||
|
}
|
||||||
|
if(inputStream != null) {
|
||||||
|
inputStream.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.msb.io.bio;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author bingor
|
||||||
|
* @Date 2023-05-05 22:10
|
||||||
|
* @Description: com.msb.io.bio
|
||||||
|
* @Version: 1.0
|
||||||
|
*/
|
||||||
|
public class BioServer {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
ServerSocket serverSocket = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
serverSocket = new ServerSocket();
|
||||||
|
serverSocket.bind(new InetSocketAddress("127.0.0.1", 8888));
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
Socket socket = serverSocket.accept();
|
||||||
|
InputStream inputStream = socket.getInputStream();
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
int len = inputStream.read(bytes);
|
||||||
|
String msg = new String(bytes, 0, len);
|
||||||
|
System.out.println("服务端接收到客户端的信息:" + msg);
|
||||||
|
|
||||||
|
OutputStream outputStream = socket.getOutputStream();
|
||||||
|
outputStream.write("客户端,你好!".getBytes());
|
||||||
|
outputStream.flush();
|
||||||
|
|
||||||
|
outputStream.close();
|
||||||
|
inputStream.close();
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
if(serverSocket != null) {
|
||||||
|
try {
|
||||||
|
serverSocket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue