标签:
try {
//1.建立服务端 对象
ServerSocket ss = new ServerSocket(8888);
//2.等待客户端链接
Socket s = ss.accept(); //阻塞式的方法
//建立服务端和客户端的输入和输出流
OutputStream out = s.getOutputStream();
String str = "你好fsdfasdfa";
//输出
out.write( str.getBytes());
s.close();
ss.close();
} catch (IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Socket s = new Socket("localhost" ,8888);
InputStream in = s.getInputStream();
//字符流接受
byte[] b = new byte[1024];
int len ;
String str = null;
StringBuffer sb = new StringBuffer();// stringbuffer 解决字符串连接的常量池 内存
while((len = in .read(b ))!=-1){
str = new String(b ,0,len );
sb.append( str);
}
System. out.println(sb .toString());
s.close();
} catch (UnknownHostException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
标签:
原文地址:http://www.cnblogs.com/ShengXi-1994/p/5577511.html