追追追太阳 2016-01-12 11:40 采纳率: 0%
浏览 1534

使用socket通信,android客户端,java写的服务器,传输数据时总出现异常

客户端:
public void run() {
// TODO Auto-generated method stub
try {

        s = new Socket(ip, port);

        DataOutputStream os = new DataOutputStream(s.getOutputStream());
        os.writeUTF("可以准备接收了吗?");


        ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
        out.writeObject(u);
        out.flush();
        out.close();

        System.out.println("用户名是:"+u.getID());


    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

服务器:

 try{
            ServerSocket ss = new ServerSocket(9999);
            System.out.println("服务器正在监听!");
            Socket s = ss.accept();
            System.out.println("建立连接!");

            DataInputStream is = new DataInputStream(s.getInputStream());
            String str = is.readUTF();
            System.out.println(str);

            ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
            User u = (User)ois.readObject();

            ois.close();

            System.out.println("用户ID :"+u.getID());


        }catch(Exception e){
            e.printStackTrace();
        }

异常:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at java.io.DataInputStream.readUTF(Unknown Source)
at com.server.EasyServer.main(EasyServer.java:24)
(在os.writeUTF一行)

  • 写回答

4条回答

  • MCTKing 2016-01-12 11:49
    关注

    输入流的问题!看一下文档吧!

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable