0

Objective - I want to send the entered text in the java (PC) project to the android app which displays this text.The PC is connected to wifi hotspot created by the android mobile.

The PC/client java project code:

public class EcsDemo {

    public static void main(String[] args) {

        System.out.println("Enter SSID to connect :");
        Scanner in = new Scanner(System.in);
        String ssid = in.nextLine();
        System.out.println("You entered ssid " + ssid);
        System.out.println("Connecting to ssid ..");
        DosCommand.runCmd(DosCommand.connectToProfile(ssid));
        // netsh wlan connect name=
        System.out.println("initializing tcp client ..");

            try {
                TCPClient.startTCpClient();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

public class TCPClient {
  public static void startTCpClient() throws UnknownHostException, IOException{
      String FromServer;
      String ToServer;

      Socket clientSocket = new Socket("localhost", 5000);

      BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
              System.in));

      PrintWriter outToServer = new PrintWriter(
              clientSocket.getOutputStream(), true);

      BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
              clientSocket.getInputStream()));

      while (true) {

          FromServer = inFromServer.readLine();

          if (FromServer.equals("q") || FromServer.equals("Q")) {
              clientSocket.close();
              break;
          } else {
              System.out.println("RECIEVED:" + FromServer);
              System.out.println("SEND(Type Q or q to Quit):");

              ToServer = inFromUser.readLine();

              if (ToServer.equals("Q") || ToServer.equals("q")) {
                  outToServer.println(ToServer);
                  clientSocket.close();
                  break;
              } else {
                  outToServer.println(ToServer);
              }
          }
      }
  }
  }

Android app/Server code:

public class MainActivity extends Activity {

    private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Log.i(TAG, "starting server");

        new ServerAsyncTask().execute();
    }
}

public class ServerAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            TCPServer.startTCPServer();// initTCPserver();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}


public static void startTCPServer() throws IOException{
    String fromclient;
    String toclient;

    ServerSocket Server = new ServerSocket(5000);

    System.out.println("TCPServer Waiting for client on port 5000");
    Log.i("startTCPServer","TCPServer Waiting for client on port 5000");

    while (true) {
        Socket connected = Server.accept();
        System.out.println(" THE CLIENT" + " " + connected.getInetAddress()
                + ":" + connected.getPort() + " IS CONNECTED ");
        Log.i("startTCPServer"," THE CLIENT" + " " + connected.getInetAddress()
                + ":" + connected.getPort() + " IS CONNECTED ");

        BufferedReader inFromUser = new BufferedReader(
                new InputStreamReader(System.in));

        BufferedReader inFromClient = new BufferedReader(
                new InputStreamReader(connected.getInputStream()));

        PrintWriter outToClient = new PrintWriter(
                connected.getOutputStream(), true);

        while (true) {

//            System.out.println("SEND(Type Q or q to Quit):");
//            toclient = inFromUser.readLine();
//
//            if (toclient.equals("q") || toclient.equals("Q")) {
//                outToClient.println(toclient);
//                connected.close();
//                break;
//            } else {
//                outToClient.println(toclient);
//            }


            fromclient = inFromClient.readLine();

            if (fromclient.equals("q") || fromclient.equals("Q")) {
                connected.close();
                break;
            } else {
                System.out.println("RECIEVED:" + fromclient);
            }

        }

    }
}
}

After running the android app and then when I run the java project I get the following exception:

java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.expressecs.javademo.TCPClient.startTCpClient(TCPClient.java:15)
    at com.expressecs.javademo.EcsDemo.main(EcsDemo.java:41)

I have referred to the following links:

java.net.ConnectException: Connection refused

Thanks!

Community
  • 1
  • 1
Rachita Nanda
  • 4,171
  • 8
  • 39
  • 63
  • I'm new to network programming , I thought it can be done using either TCP/UDP – Rachita Nanda May 16 '15 at 18:43
  • @Codester Why can't one use TCP over wireless network? How does the medium matter for the transport layer protocol? – Prabhu May 16 '15 at 18:48
  • @Prabhu Please correct me if I am wrong. – GeekDroid May 16 '15 at 18:53
  • @RachitaNanda I am extremely sorry, Please refer to [this](http://stackoverflow.com/questions/8443245/wifi-connection-via-android) , it might help you a lot – GeekDroid May 16 '15 at 19:00
  • @Codester, there is nothing like you *must* use UDP if using wireless. TCP /UDP are your transport protocols. The network b/w client and server do not matter. – Prabhu May 16 '15 at 19:01
  • @Prabhu Thanks for correcting, I apologized for the wrong answer and added a useful link. Hmm What do you think can be the error here ? – GeekDroid May 16 '15 at 19:02
  • 2
    @RachitaNanda Is the server bind to port 5000 successful? 'Connection Refused' means there isn't any application listening on the said port to `accept` connections. – Prabhu May 16 '15 at 19:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77981/discussion-between-codester-and-prabhu). – GeekDroid May 16 '15 at 19:05

1 Answers1

1

There is nothing listening at the IP:port you are trying to connect to.

Your server didn't start, or is listening to a different port, or is bound to 127.0.0.1 instead of 0.0.0.0 or a public IP address.

user207421
  • 289,834
  • 37
  • 266
  • 440