您的位置:首页 > 新闻 > 资讯 > 哈尔滨企业建站哪家靠谱_物联网方案_汕头seo代理_做网站优化的公司

哈尔滨企业建站哪家靠谱_物联网方案_汕头seo代理_做网站优化的公司

2024/12/23 22:56:33 来源:https://blog.csdn.net/qq_44858608/article/details/142318228  浏览:    关键词:哈尔滨企业建站哪家靠谱_物联网方案_汕头seo代理_做网站优化的公司
哈尔滨企业建站哪家靠谱_物联网方案_汕头seo代理_做网站优化的公司

TCP客户端代码

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;public class TcpClientDemo01 {public static void main(String[] args) {Socket socket = null;OutputStream os = null;InputStream is = null;BufferedReader reader = null;try {// 1. 要知道服务器的地址、端口号InetAddress serverIP = InetAddress.getByName("127.0.0.1");int port = 9999;// 2. 创建一个Socket连接socket = new Socket(serverIP, port);// 3. 创建输出流,用于发送消息给服务器os = socket.getOutputStream();PrintWriter pw = new PrintWriter(os, true);// 4. 创建输入流,用于接收服务器的消息is = socket.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));// 5. 客户端输入消息reader = new BufferedReader(new InputStreamReader(System.in));String clientMessage;String serverResponse;while (true) {// 读取客户端输入的消息System.out.print("客户端: ");clientMessage = reader.readLine();// 发送消息到服务器pw.println(clientMessage);// 接收服务器返回的消息serverResponse = br.readLine();System.out.println("服务器: " + serverResponse);// 判断是否退出if ("bye".equalsIgnoreCase(clientMessage)) {break;}}} catch (Exception e) {e.printStackTrace();} finally {// 关闭资源try {if (reader != null) reader.close();if (is != null) is.close();if (os != null) os.close();if (socket != null) socket.close();} catch (IOException e) {e.printStackTrace();}}}
}

TCP服务器代码

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;public class TcpServerDemo01 {public static void main(String[] args) {ServerSocket serverSocket = null;Socket socket = null;InputStream is = null;OutputStream os = null;BufferedReader br = null;try {// 1. 创建一个服务器端的Socket,绑定端口9999serverSocket = new ServerSocket(9999);// 2. 等待客户端连接System.out.println("服务器等待连接...");socket = serverSocket.accept();System.out.println("客户端已连接!");// 3. 获取客户端发送的消息is = socket.getInputStream();br = new BufferedReader(new InputStreamReader(is));// 4. 创建输出流,发送消息给客户端os = socket.getOutputStream();PrintWriter pw = new PrintWriter(os, true);String clientMessage;String serverMessage;while (true) {// 读取客户端发送的消息clientMessage = br.readLine();System.out.println("客户端: " + clientMessage);// 判断是否退出if ("bye".equalsIgnoreCase(clientMessage)) {pw.println("再见!");break;}// 服务器回复消息BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));System.out.print("服务器: ");serverMessage = reader.readLine();// 发送回复给客户端pw.println(serverMessage);}} catch (Exception e) {e.printStackTrace();} finally {// 关闭资源try {if (br != null) br.close();if (is != null) is.close();if (os != null) os.close();if (socket != null) socket.close();if (serverSocket != null) serverSocket.close();} catch (IOException e) {e.printStackTrace();}}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com