socket.error: [Errno 99] Cannot assign requested address

0 Tony Stark · July 9, 2015
I just started learning about networking and created a simple TCP client server network. Initially I used 127.0.0.1 as host and 5000 as port and the programs I wrote in python were working perfectly. However I went into android and tried to connect to the server however the android emulator I'm using has a localhost with ip = 127.0.0.1 which meant that I had to change the host's ip address in my server. However it doesn't matter what ip I change it to I get the error in the title. I've tried 10.0.0.1 and several others. Can someone point me in the right direction please

Post a Reply

Replies

Oldest  Newest  Rating
0 Rehman . · July 9, 2015
could you please paste your code here?
0 Tony Stark · July 9, 2015

import socket

def Main():
#host and port
host = '127.0.0.1'
#port
port = 5000
#create socket
s = socket.socket()
#bind socket to a port & host
s.bind((host,port))
#start listening for connections
#1 connection at a time
s.listen(2)
# accept function that returns a connection
#which is a new socket
c, addr = s.accept()
print("Connection from", str(addr))
#talk to client
while True:
#receive bytes from connection
#max buffer = 1024 bytes
data = c.recv(1024)
#if client ends connection so no data, break
if not data:
breakĀ 
#print to sever data from user
print("from connected user: " + str(data))
# we can do whatever we want with the data now
#exaple turn it to upper case
data = str(data).upper()
print("sending: " + str(data))
#send the data
c.send(data)
#after connection is closed
c.close()
Main()


Android Code


URL url = new URL("http://127.0.0.1:5000");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

//read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = in.toString();


The host and port 127.0.0.1,5000 only works when I run the server and client in python on my laptop. However the same ip address is the localhost for the emulator I'm using for android so it doesn't work, I have to change it. However It doesn't matter what new ip and port I change the server to, I get the error above when I run the server code. I tried 10.0.3.2' and port to 6677 but nope, same error
  • 1

Networking

103,810 followers
About

Chat about routers, switches and firewalls, and more!

Links
Moderators
Bucky Roberts Administrator