qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Java socket 服务和客户端传值

packagesockerk;
importjava.io.DataInputStream;
importjava.io.IOException;
importjava.net.Socket;
importjava.net.UnknownHostException;
publicclassSocketClient{
publicstaticvoidmain(String[]args){
Socketsocket=null;
try{
socket=newSocket("127.0.0.1",8888);
DataInputStreamdis=newDataInputStream(socket.getInputStream());
Stringstring="";
while(!"end".equals(string)){
string=dis.readUTF();
System.out.println(string);
}
socket.close();
}catch(UnknownHostExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
  服务端
packagesocket;
importjava.io.BufferedReader;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.ServerSocket;
importjava.net.Socket;
publicclassSocketSingleServer{
publicstaticvoidmain(String[]args)throwsIOException{
ServerSocketserverSocket=null;
Socketclient=null;
try{
serverSocket=newServerSocket(8888);
client=serverSocket.accept();
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));
DataOutputStreamdos=newDataOutputStream(client.getOutputStream());
while(!client.isClosed()){
Stringstr="";
if((str=br.readLine())!=null){
dos.writeUTF(str);
dos.flush();
}
}
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
client.close();
}
}
}
  客服端

posted on 2014-05-13 16:26 顺其自然EVO 阅读(438) 评论(0)  编辑  收藏 所属分类: 测试学习专栏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜