网路编程 客户端

//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();

posted @ 2019-06-03 13:24  勇者斗恶龙唐嫣英雄传  阅读(113)  评论(0编辑  收藏  举报