python socket server and java android socket - freezes app
<p>I'm making a python server running on my computer and java socket client running on my phone. the minute the java tries to connect, the app freezes. Not saying anything in the logcat. I have no idea why...</p>

<p>python 3.5.1:</p>

<pre><code>PORT = 8888

import socket

sock = socket.socket()
sock.bind(("0.0.0.0", PORT))
sock.listen(1)
client = None
while not client:
    try:
        client = sock.accept()
    except:
        pass
print(client[1])
</code></pre>

<p>java:</p>

<pre><code>static String get_leaderboard() {
        Socket sock;
        OutputStream out;
        InputStream in;
        try {
            System.out.println("now trying to connect");
            sock = new Socket("192.168.1.29", 8888);
            System.out.println("connected successfuly");
            out = sock.getOutputStream();
            in = sock.getInputStream();
            return Integer.toString(in.read());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Error in connection";
</code></pre>

<p>but actually, logcat never prints "connected successfuly". just "trying to connect".</p>

<p>This line is wrong:</p>

<pre><code>client = sock.connect()
</code></pre>

<p>Servers don't call <code>connect()</code>. Servers call <code>listen()</code> and <code>accept()</code>. Only clients call <code>connect()</code>.</p>

<p>Since your server calls <code>bind()</code> and <code>listen()</code>, but not <code>accept()</code>, any client will hang trying to connect to it.</p>

<p>Resources:</p>

<ul>
<li><a href="https://wiki.python.org/moin/TcpCommunication" rel="nofollow">https://wiki.python.org/moin/TcpCommunication</a></li>
<li><a href="https://docs.python.org/3/howto/sockets.html" rel="nofollow">https://docs.python.org/3/howto/sockets.html</a></li>
<li><a href="http://beej.us/guide/bgnet/" rel="nofollow">http://beej.us/guide/bgnet/</a></li>
</ul>

java android python sockets