java.net.SocketException:连接重置错误

时间:2014-06-04 12:29:08

标签: java sockets

我在Java中使用socket。 客户发送姓名和电话号码。 然后切断获取客户端数据并加密它。 然而问题就产生了。

  

java.net.SocketException:连接重置     在java.net.SocketInputStream.read(SocketInputStream.java:168)     在java.io.DataInputStream.readFully(DataInputStream.java:178)     在java.io.DataInputStream.readUTF(DataInputStream.java:592)     在java.io.DataInputStream.readUTF(DataInputStream.java:547)     在chat.QrcodeServer.dataEnc(QrcodeServer.java:45)     在chat.QrcodeServer.main(QrcodeServer.java:25)

服务器:

 import java.io.*;
 import java.net.*;

 public class QrcodeServer{
    public static void main (String[] args){
        ServerSocket ss = null;
        Socket client = null;
        int port = 10000;

        try {
                ss = new ServerSocket(port);
                System.out.println("Service is started in "+ port);
        }catch (IOException ee){
            ee.printStackTrace();
        }

        try{
            while (true){
                client = ss.accept();
                System.out.println("Client Info:" + client);
                dataEnc(client);

            }
        }catch(IOException ee){
            ee.printStackTrace();
        }
    }

    public static void dataEnc(Socket client){
        // Get an input file handle from the socket and read the input
         InputStream cin=null;
         String data=null;
         String password=null;
        try {

            //get client Data Stream
            cin = client.getInputStream();
            DataInputStream dis = new DataInputStream(cin);

            //get data
            data = new String (dis.readUTF());
            password = new String(dis.readUTF());

            System.out.println(data);
            System.out.println(password);

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

        //To make password hash
         String passwordTemp = Crypto.getPassword(password);
         try {

             //encrypt data through passwordTemp
            byte[] cipher = Crypto.encrypt(data,passwordTemp);

            //printout the cipher data
            System.out.print("cipher: ");
            for(int i=0; i<cipher.length;i++){
                System.out.print(Integer.toHexString(0xff&cipher[i])+" ");
            }
            System.out.println();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

客户端:

import java.io.*;
import java.net.*;
public class Client {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Socket server = null;
        String ip = "ip addr";
        int port = 10000;

        try{
            server = new Socket(ip, port);
            System.out.println("server: "+ip + "and port is "+ port );
            sendInfo(server);
        }catch(IOException e){
            e.printStackTrace();
        }


    }

    public static void sendInfo (Socket soc){
        // Get a communication stream associated with the socket
         OutputStream cout;
        try {
            cout = soc.getOutputStream();
            DataOutputStream dos = new DataOutputStream (cout);

            String name="kevin";
             String phone="123456780";
             String password="password";
             String data = name+phone;

             // Send a data
             dos.writeUTF(data);
             dos.writeUTF(password);


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



    }

}

它适用于localhost和本地内部网络。 但是当客户端从互联网连接时,上面的问题会产生。 可能是什么问题?

0 个答案:

没有答案