生成一段java socket代码
时间: 2023-02-22 19:54:11 浏览: 89
Socket代码示例
// 创建一个Socket对象
Socket socket = new Socket("localhost", 8080);
// 获取Socket的输入输出流
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
// 读取服务器端数据
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String info = reader.readLine();
System.out.println("服务器发送过来的消息:" + info);
// 向服务器端发送数据
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
writer.write("你好啊!");
writer.flush();
// 关闭Socket
socket.close();
阅读全文