androidpythonwebsocketraspberry-piaccess-point

Unable to connect to socket over wifi connection


I am not able to connect to socket on Raspberry Pi 3 B+. This is what I did

  1. Installed dnsmasq and hostapd and configured
  2. created an access point and assigned static ip to wlano as 192.168.4.1 without bridging to lan
  3. started a python script to listen on port 8888 (is successfully waiting for connections)
  4. created an Android app to connect to the access point and send message over socket to 192.168.4.1 on port 8888

When I tried to connect to the socket using the wlan0 static ip,192.168.4.1 I am getting unknown host exception. and the python script prints the socket ip as 127.0.1.1 how can I run the Python script to listen on wlan0 IP instead of 127.0.1.1 this is my Python script which I got from internet

import socket
import sys
from thread import *

HOST = ''   # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

#Start listening on socket
s.listen(10)
print 'Socket now listening'

#Function for handling connections. This will be used to create threads
def clientthread(conn):
    #Sending message to connected client
    conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string

    #infinite loop so that function do not terminate and thread do not end.
    while True:

        #Receiving from client
        data = conn.recv(1024)
        reply = 'OK...' + data
        if not data: 
            break

        conn.sendall(reply)

    #came out of loop
    conn.close()

#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])

    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))

s.close()

I haven't done any Python programming earlier. So I have to rely on the script and it is working fine.


Solution

  • Set

    HOST = "192.168.4.1"
    

    In the code and it should work.