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

来源:互联网 发布:程序员的思维锻炼 编辑:程序博客网 时间:2024/05/21 07:57
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/stockeck?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
原创粉丝点击