简体   繁体   中英

Android socket not receiving data from php server

I have a problem with android client receiving data from php server. Android can successfully writes data to php server and server accepts that data and then send back response to that client but android is not accepting. it does not move forward from Socket s = ss.accept()

Here is my android code to recieve data

public void run() {
    Boolean end = false;
    ServerSocket ss = serverSocket;

    /*try {
        ss = new ServerSocket(54546);
    } catch (IOException e1) {
        //TODO Auto-generated catch block
        e1.printStackTrace();
    }*/
    while(!end){
        //Server is waiting for client here, if needed
        try {
            Log.i("before accept", "yes");
            Socket s = ss.accept();

            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
            //PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
            String st = input.readLine();
            Log.d("Tcp Example", "From client: "+st);
            //output.println("Good bye and thanks for all the fish :)");
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Here is my php code

$host = "127.0.0.1";
$port = 54546;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
//$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n");
/*if (!socket_connect($socket, $host, $port)) {
    die('failed'.socket_strerror(socket_last_error($socket)));
}*/
if (!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) { 
    echo socket_strerror(socket_last_error($socket)); 
    exit; 
}
$result = socket_bind($socket, $host, $port) or die("Could not create socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
echo "\nbefore socket accept while loop\n";
$aaa = fopen("tesst.txt", "w");

while(true)
{
    echo "\nbefore socket accept\n";
    $spawn = socket_accept($socket) or die("Could not accept incoming connection\n");

    echo "\nThe server is ready\n";
    $input = socket_read($spawn, 1024) or die("Could not read input\n");
    echo "Input recieved from $spawn : ".$input;
    fwrite($aaa, $input);
    $output = $input."\n";

    $sent = socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
    echo "Output sent ".$sent;
    socket_close($spawn);
}   
fclose($aaa);
socket_close($socket);
echo "\nTerminating\n";

ss.accept() is not accepting connection from server.

We don't have to do ss.accept() for the client as well. The client should establish the connection using the connect() and the server should do accept(). Once the connection is established, the server should use the file descriptor returned from the accept() to send and receive data to/from the client. On the other side, the client does not need to do any accept, it should simply call recv() to retrieve received data.

Thus, if the Android code is the client, then it should do a connect() call to the PHP server -- the connect() call would take the IP address and the port number (54546). With a successful connect() call, the accept() on the PHP would return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM