java吧 关注:1,235,016贴子:12,701,438
  • 7回复贴,共1

大神们求帮助

只看楼主收藏回复

跪求帮助
这个代码问题怎么解决!
****欢迎光临****
1.登陆
2.退出系统
1
请输入账号
001
请输入密码
001
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2269)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2282)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2753)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:777)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
at Supermarket.Sockets.main(Sockets.java:37)


1楼2016-05-04 22:42回复
    这是源代码
    服务器
    package Supermarket;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class Server extends Thread{
    public static void main(String[] args) {
    ServerSocket socket = null;
    Socket s =null;
    InputStream is = null;
    ObjectInputStream ois =null;
    OutputStream os = null;
    ObjectOutputStream oos = null;
    Goods goods = new Goods();
    try {
    //创建客户端
    //接受
    while(true){
    socket = new ServerSocket(8000);
    s=socket.accept();
    is=s.getInputStream();
    ois= new ObjectInputStream(is);
    //获取客户端的请求
    Datas datas=(Datas)ois.readObject();
    String role=goods.denglu(datas.getUserName(), datas.getPassWord());
    datas.setRole(role);
    oos= new ObjectOutputStream(s.getOutputStream());
    oos.writeObject(datas);
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    }
    }
    }


    2楼2016-05-04 22:43
    回复
      这是客户端
      package Supermarket;
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.io.OutputStream;
      import java.net.Socket;
      import java.net.UnknownHostException;
      import java.util.Scanner;
      public class Sockets {
      public static void main(String[] args) {
      System.out.println("****欢迎光临****");
      System.out.println("1.登陆");
      System.out.println("2.退出系统");
      Scanner input = new Scanner (System.in);
      String p = input.next();
      User user = new User();
      if(p.equals("1")){
      System.out.println("请输入账号");
      String name = input.next();
      System.out.println("请输入密码");
      String pwd = input.next();
      user.setUserName(name);
      user.setUserPwd(pwd);
      Socket socket =null;
      OutputStream os =null;
      ObjectOutputStream oos = null;
      ObjectInputStream ois = null;
      try {
      //创建客户端
      socket= new Socket("localhost",8000);
      //像服务器发送
      os= socket.getOutputStream();
      oos = new ObjectOutputStream(os);
      oos.writeObject(user);
      ois = new ObjectInputStream(socket.getInputStream());
      Datas datas=(Datas)ois.readObject();
      if(datas.getRole()==null){
      System.out.println("帐户名或密码错误");
      }else{
      System.out.println("登陆成功");
      }
      } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }finally{
      try {
      if(ois!=null)
      ois.close();
      if(oos!=null)
      oos.close();
      if(socket!=null)
      socket.close();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }
      }else{
      System.out.println("退出成功");
      }
      }
      }


      3楼2016-05-04 22:43
      回复
        相关对象
        package Supermarket;
        import java.io.Serializable;
        public class User implements Serializable{
        private String userName;
        private String userPwd;
        private String role;
        public User() {
        super();
        // TODO Auto-generated constructor stub
        }
        public User(String userName, String userPwd, String role) {
        super();
        this.userName = userName;
        this.userPwd = userPwd;
        this.role = role;
        }
        public String getUserName() {
        return userName;
        }
        public void setUserName(String userName) {
        this.userName = userName;
        }
        public String getUserPwd() {
        return userPwd;
        }
        public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
        }
        public String getRole() {
        return role;
        }
        public void setRole(String role) {
        this.role = role;
        }
        }


        4楼2016-05-04 22:44
        回复
          public class Datas implements Serializable {
          private String flag;// 传递的标识
          private String role;//用户用色
          private String userName;//用户名
          private String passWord;//密码
          private Datasdatas;//商品对象
          private List<Manage>manList; //商品对象
          public String getFlag() {
          return flag;
          }
          public void setFlag(String flag) {
          this.flag = flag;
          }
          public String getRole() {
          return role;
          }
          public void setRole(String role) {
          this.role = role;
          }
          public String getUserName() {
          return userName;
          }
          public void setUserName(String userName) {
          this.userName = userName;
          }
          public String getPassWord() {
          return passWord;
          }
          public void setPassWord(String passWord) {
          this.passWord = passWord;
          }
          public Datas getDatas() {
          return datas;
          }
          public void setDatas(Datas datas) {
          this.datas = datas;
          }
          public List<Manage> getManList() {
          return manList;
          }
          public void setManList(List<Manage> manList) {
          this.manList = manList;
          }
          }


          5楼2016-05-04 22:44
          回复
            package Supermarket;
            import java.util.HashMap;
            import java.util.Map;
            public class Goods {
            public static Map<String,Manage>manageMap;
            public static Map<String,User>userMap;
            public static void make(){
            manageMap= new HashMap<String,Manage>();
            Manage manage1 = new Manage("1001","吉百家芝麻油",9.50,"125ml",20);
            Manage manage2 = new Manage("1002","雀巢咖啡",1.50,"13g",200);
            Manage manage3 = new Manage("1003","脉动水蜜桃",7.00,"1.5l",50);
            Manage manage4 = new Manage("1004","旺仔QQ糖",2.50,"70g",70);
            Manage manage5 = new Manage("1005","桃李醇熟切片",6.50,"400g",10);
            Manage manage6 = new Manage("1006","白玉黄豆芽",2.40,"350g",20);
            Manage manage7 = new Manage("1007","白玉内酯豆腐",1.00,"400g",15);
            Manage manage8 = new Manage("1008","吉百家芝麻油",9.50,"125ml",20);
            manageMap.put(manage1.getCode(), manage1);
            manageMap.put(manage2.getCode(), manage2);
            manageMap.put(manage3.getCode(), manage3);
            manageMap.put(manage4.getCode(), manage4);
            manageMap.put(manage5.getCode(), manage5);
            manageMap.put(manage6.getCode(), manage6);
            manageMap.put(manage7.getCode(), manage7);
            manageMap.put(manage8.getCode(), manage8);
            userMap=new HashMap<String,User>();
            userMap.put("001", new User("001","00001","01"));
            userMap.put("002", new User("001","00002","02"));
            }
            //判断账号和密码
            public String denglu(String name,String pwd){
            if(userMap.containsKey(name)){
            User user= userMap.get(name);
            if(user.getUserPwd().equals(pwd)){
            return user.getRole();
            }
            }
            return null;
            }
            }


            6楼2016-05-04 22:45
            回复
              谢谢


              8楼2016-05-04 22:45
              回复
                直接tie代码很少有人看


                IP属地:北京来自Android客户端9楼2016-05-04 22:51
                回复