当前位置:
首页 >
网路编程 客户端
发布时间:2025/7/14
39
豆豆
//localhost代表的是本机地址
Socket socket=new Socket("localhost",4365);
//向服务端输出信息
OutputStream os=socket.getOutputStream();
String request="我是客户端,服务器你好!";
//调用Write方法导入,用String类里的getBytes把字符串改为字符数组
os.write(request.getBytes());
socket.shutdownOutput();
//接收信息
InputStream is=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//读服务端
String sound=null;
while((sound=br.readLine())!=null){
System.out.println("我是客户端,我读取到了:"+sound);
}
//关就完事了
br.close();
is.close();
os.close();
socket.close();
转载于:https://www.cnblogs.com/T25YYZ/p/10967017.html
总结
- 上一篇: 第 2 章
- 下一篇: Mac怎么刷新DNS缓存