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!

8
  • I'm new to network programming , I thought it can be done using either TCP/UDP Commented May 16, 2015 at 18:43
  • @Codester Why can't one use TCP over wireless network? How does the medium matter for the transport layer protocol? Commented May 16, 2015 at 18:48
  • @Prabhu Please correct me if I am wrong. Commented May 16, 2015 at 18:53
  • @RachitaNanda I am extremely sorry, Please refer to this , it might help you a lot Commented May 16, 2015 at 19:00
  • 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. Commented May 16, 2015 at 19:04

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was using local host , when I changed it to the ip for wifi. it worked. Thanks

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.