0

I am using socket in Java. Client send a name and phone number. then sever get client data and encrypt it. however problem accrue.

java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:168) at java.io.DataInputStream.readFully(DataInputStream.java:178) at java.io.DataInputStream.readUTF(DataInputStream.java:592) at java.io.DataInputStream.readUTF(DataInputStream.java:547) at chat.QrcodeServer.dataEnc(QrcodeServer.java:45) at chat.QrcodeServer.main(QrcodeServer.java:25)

Server:

 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();
        }

    }
}

Client:

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();
        }



    }

}

It works in localhost and local interior network. but problem above accrue when client connected from internet. What might be the problem?

9
  • what IP address did you set in client to connect to the server from the internet ? Commented Jun 4, 2014 at 12:32
  • Seems to break in Server.dataEnc. Did you get an informational message from line System.out.println("Client Info:" + client); Commented Jun 4, 2014 at 13:40
  • @Mifmif My server IP. I can't tell you specific address. Commented Jun 4, 2014 at 15:08
  • @SergeBallesta: Yes. I got the Client Info. Commented Jun 4, 2014 at 15:09
  • of course a don't ask for specific IP address , what I ask is : is the server IP address is the public IP that connect to the internet or the private IP that you use in you local net. if your server has just a private IP , then you have to Configure NAT and ports routing . if not then check your firewall configuration. in all cases your client should point to a public IP address Commented Jun 4, 2014 at 15:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.