qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          Java socket 服務(wù)和客戶端傳值

          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();
          }
          }
          }
            服務(wù)端
          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 閱讀(444) 評論(0)  編輯  收藏 所屬分類: 測試學(xué)習(xí)專欄

          <2014年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 吉林市| 那曲县| 永吉县| 竹溪县| 屏山县| 米脂县| 仪陇县| 留坝县| 衡阳市| 林周县| 屏山县| 宁强县| 华阴市| 黄大仙区| 永泰县| 黎平县| 元江| 石楼县| 通辽市| 隆化县| 屏东县| 曲麻莱县| 罗定市| 新密市| 忻城县| 府谷县| 延安市| 横峰县| 通化市| 瓮安县| 榆社县| 资兴市| 张家口市| 达日县| 扶余县| 壤塘县| 礼泉县| 吴桥县| 上饶市| 北流市| 富宁县|