服务端:
public class Server02 { public static void main(String[] args) throws Exception{ //创建服务端连接 ServerSocket serverSocket = new ServerSocket(8999); //接收客户端请求 Socket accept = serverSocket.accept(); //输入流读取数据 InputStream inputStream = accept.getInputStream(); DataInputStream dataInputStream = new DataInputStream(inputStream); //输出流反馈数据 OutputStream outputStream = accept.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); //接收数据并传输 String s = dataInputStream.readUTF(); System.out.println("我是服务端,我接收到了:"+s); //发送反馈信息 String s1 = "欢迎您的到来,先生!"; dataOutputStream.writeUTF(s1); //释放资源 dataInputStream.close(); dataOutputStream.close(); inputStream.close(); outputStream.close(); serve