0

why i get java.net.ConnectException: /192.168.1.5:6789 - Connection refused?

i am using php and android java code. before asking this question i checked questions like java.net.ConnectException: Connection refused. while there were useful but they could not solve my problem.

the scenario is

1- i am using XAMPP

2- windows firewall is off

3- when i run php script i can see port 6789 is set to listening by checking netstat -a

4- java "android" code :

    try
        {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);//"192.168.1.5" the PC which XAMPP is runing on
            socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);//6789
        }
        catch (UnknownHostException e1)
        {
            e1.printStackTrace();
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }

5- php code

 <?php
set_time_limit( 0 );
$address = '127.0.0.1';
$port = 6789;
$sock = socket_create( AF_INET, SOCK_STREAM, 0 ); // 0 for  SQL_TCP
socket_bind( $sock, 0, $port ) or die( 'Could not bind to address' );  
socket_listen( $sock );
  while (true) {
    $client = socket_accept( $sock );
    $input = socket_read( $client, 1024000 );
    socket_write( $client, $response );
    socket_close( $client );
 }

socket_close( $sock );

6- i run php script and it keeps loading that mean it is listening and is in while block

7- i start the android app and i get this notification

java.net.ConnectException: /192.168.1.5:6789 - Connection refused

and finally i am sure that PC(server) IP address is correct and in manifests internet permission is set

thank you in advance!

Community
  • 1
  • 1
Ehsan Jeihani
  • 1,238
  • 2
  • 14
  • 23

1 Answers1

1

I don't think that socket_create( AF_INET, SOCK_STREAM, 0 ); is correct. The third number is the protocol number, and the documentation says that you should be using the SOL_TCP constant for that.

The value 0 is the protocol number for IP. The protocol number for TCP is 6. Both of these are according to "/etc/protocols" on my Linux machine ... which is the definitive source according to the documentation / comments for socket_create and getprotobynumber.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • thank you for your reply. the problem was actually placing java code for button click event. when i click on it tow times continuously the connection is refused . i should have place that on onCreate function. – Ehsan Jeihani Feb 14 '16 at 13:53
  • and about "I don't think that socket_create( AF_INET, SOCK_STREAM, 0 );is correct". 0 means exactly SOL_TCP – Ehsan Jeihani Feb 21 '16 at 22:48
  • @EhsanJeihani - On what platform? – Stephen C Feb 22 '16 at 12:08
  • did not you see those keywords – Ehsan Jeihani Feb 23 '16 at 21:36
  • 1) Did you print out the value to be sure of that? I can't check for myself because I don't have PHP on Windows to play with. And my reading of the PHP doco's and comments is that SOL_TCP is not 0 ... on some platforms. 2) I did. The tags don't answer my question. I suspect the answer was looking for is that you are running the PHP code on "some version of windows" ... – Stephen C Feb 23 '16 at 22:48
  • i wanted to socket over internet which is not possible by this method . i found the XMPP method and Smack library to do that. thank you @Stephen – Ehsan Jeihani Feb 23 '16 at 22:52
  • 1
    3) I suspect that you have actually implemented your PHP to be platform dependent. That's fine ... provided that you don't try to run the server PHP code on Linux or (probably) MacOS, where I suspect that SOL_TCP is not zero. – Stephen C Feb 23 '16 at 22:54
  • Maybe the reason that you found that *"socket over internet is not possible by this method"* is that you used zero instead of SOL_TCP! (Depending on what its value actually is ... ) – Stephen C Feb 23 '16 at 23:00
  • no it does not work. because all the device are behind the modem and have a public shared ip – Ehsan Jeihani Feb 23 '16 at 23:10