系统设计socket服务在电子商务上的试用

public  class  Client {
     public  static  void  main(String[] args) {
         String s= null ;
         Socket mysocket;
         DataInputStream in= null ;
         DataOutputStream out= null ;
         try  {
             mysocket= new  Socket( "127.0.0.1" , 4331 );
             in= new  DataInputStream(mysocket.getInputStream());
             out= new  DataOutputStream(mysocket.getOutputStream());
             for ( int  k= 1 ;k< 100 ;k=k+ 2 ){
                 out.writeUTF( "" +k);
                 s=in.readUTF();
                 System.out.println( "客户收到" +s);
                 Thread.sleep( 500 );
             }
         catch  (Exception e) {
             System.out.println( "服务器已断开" +e);
         }
     }
}
 
public  class  Server {
     public  static  void  main(String[] args) {
         ServerSocket server= null ;
         Socket you= null ;
         String s= null ;
         DataOutputStream out= null ;
         DataInputStream in= null ;
         try  {
             server= new  ServerSocket( 4331 );
         catch  (Exception e) {
             System.out.println(e);
         }
         try  {
             System.out.println( "等待客户呼叫" );
             you=server.accept();
             out= new  DataOutputStream(you.getOutputStream());
             in= new  DataInputStream(you.getInputStream());
             while ( true ){
                 s=in.readUTF();
                 int  m=Integer.parseInt(s);
                 out.writeUTF( "你好,我是服务器" );
                 out.writeUTF( "你说的数乘2后是:" + 2 *m);
                 System.out.println( "服务器收到:" +s);
                 Thread.sleep( 500 );
             }
         catch  (Exception e) {
             System.out.println( "客户端已断开" +e);
         }
     }
}
---------------------------------------------------------------------------------
public class Server {
    public static void main(String[] args) {
        ServerSocket server=null;
        Socket you=null;
        String s=null;
        DataOutputStream out=null;
        DataInputStream in=null;
        try {
            server=new ServerSocket(4331);
        } catch (Exception e) {
            System.out.println(e);
        }
        try {
            System.out.println("等待客户呼叫");
            you=server.accept();
            out=new DataOutputStream(you.getOutputStream());
            in=new DataInputStream(you.getInputStream());
            while(true){
                s=in.readUTF();
                int m=Integer.parseInt(s);
                out.writeUTF("你好,我是服务器");
                out.writeUTF("你说的数乘2后是:"+2*m);
                System.out.println("服务器收到:"+s);
                Thread.sleep(500);
            }
        } catch (Exception e) {
            System.out.println("客户端已断开"+e);
        }
    }
}
 
唐韬  14:34:28
000031/stock eck?su=1,2,3&num=1,2,3
唐韬  14:56:34
000073{"ok":1,“canbuy”[{“su”:1,n:1},{“su”:2,n:0},{“su”:3,n:0}]}
唐韬  15:00:11
data = URLEncoder.encode(data,"UTF-8");
             String length = ((1000000+data.length())+"").substring(1);
唐韬  15:22:53
  public static String read(DataInputStream in,int start, int end) throws IOException{
     byte buffer[] = new byte[end-start];
     in.read(buffer, start, end);
     return new String(buffer);
    }
 int length2 = Integer.parseInt(read(in, 0,6));
             result=read(in,6,6+length2);
Nico  15:41:33
public static String read(DataInputStream in) throws IOException{
     byte length[] = new byte[6];
     in.read(length, 0, 6);
     int leg = Integer.parseInt(new String(length));
     System.out.println(leg);
     byte result[] = new byte[leg];
     in.read(result, 0, leg);
     System.out.println(new String(result));
     return new String(result);
    }
Nico  18:34:52
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Server {
static String data = "{\"ok\":1,\"canbuy\"[{\"su\":1,n:1},{\"su\":2,n:0},{\"su\":3,n:0}]}";
    public static void main(String[] args) throws UnsupportedEncodingException {
       send(data);
    }
    public static void send(String data) throws UnsupportedEncodingException{
      data = URLEncoder.encode(data,"UTF-8");
         String length = ((1000000+data.length())+"").substring(1);
         
      ServerSocket server=null;
         Socket you=null;
         String s=null;
         DataOutputStream out=null;
         DataInputStream in=null;
         try {
             server=new ServerSocket(4331);
         } catch (Exception e) {
             System.out.println(e);
         }
         try {
             System.out.println("等待客户呼叫");
             you=server.accept();
             out=new DataOutputStream(you.getOutputStream());
             in=new DataInputStream(you.getInputStream());
             while(true){
                 s=read(in);
                 System.out.println("-----------");
                 System.out.println(URLDecoder.decode(s,"UTF-8"));
                 out.write((length+data).getBytes());
                 System.out.println("服务器收到:"+s);
                 Thread.sleep(10);
             }
         } catch (Exception e) {
          e.printStackTrace();
             System.out.println("客户端已断开"+e);
         }
    }
    public static String read(DataInputStream in) throws IOException{
     byte length[] = new byte[6];
     in.read(length, 0, 6);
     int leg = Integer.parseInt(new String(length));
     System.out.println(leg);
     byte result[] = new byte[leg];
     in.read(result, 0, leg);
     System.out.println(new String(result));
     return new String(result);
    }
}
 public static void testCheck(String[] args) throws UnsupportedEncodingException {
        //String result = sendSocket("stock/check?ids=1,2,3&num=1,2,3",ByConfiguration.getInstance().getConfig("socket.ip"),Integer.parseInt(ByConfiguration.getInstance().getConfig("socket.port")));
        String result = sendSocket("stock/check?ids=1,2,3&num=1,2,3","192.168.99.60",2077);
        JSONObject json =  JSONObject.fromObject(result);
        if(json.getInt("ok")==1){
            JSONArray sus = json.getJSONArray("canbuy");
            for(int i=0;i<sus.size();i++){
                System.out.println(sus.getJSONObject(i).getLong("su"));
                System.out.println(sus.getJSONObject(i).getInt("n"));
            }
        }
        System.out.println(result);
    }
    public static void updateStock() throws InterruptedException{
        
        
    }
    public static String sendSocket(String data,String ip, int port){
         String result="";
         Socket mysocket = null;
         DataInputStream in=null;
         DataOutputStream out=null;
         try {
             mysocket=new Socket(ip,port);
             mysocket.setSoTimeout(30);
             in=new DataInputStream(mysocket.getInputStream());
             out=new DataOutputStream(mysocket.getOutputStream());
             String length = ((1000000+data.length())+"").substring(1);
             out.write((length+data).getBytes());
             result=read(in);
         } catch (Exception e) {
             e.printStackTrace();
             System.out.println("服务器已断开"+e);
         }finally{
             if(in!=null){
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
             }
             if(out!=null){
                 try {
                     out.close();
                 } catch (IOException e1) {
                     e1.printStackTrace();
                 }
              }
             if(mysocket!=null){
                try {
                    mysocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
             }
         }
         return result;
    }
    public static String read(DataInputStream in) throws IOException{
        byte length[] = new byte[6];
        in.read(length, 0, 6);
        int leg = Integer.parseInt(new String(length));
        byte result[] = new byte[leg];
        in.read(result, 0, leg);
        return new String(result);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值