views:

67

answers:

2

Hi, i already have a post which is quite similiar, but i am getting more and more frustrated because it seems nothing is wrong with my network setup. Other software can be seen from the outside (netcat listen servers etc.) but not my scripts.. How can this be?? Note: It works on LAN but not over the internet.

Server:

import socket

host = ''
port = 80001

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(1)
print 'Listening..'
conn, addr = s.accept()
print 'is up and running.'
print addr, 'connected.'
s.close()
print 'shut down.'

Client:

import socket
host = '80.xxx.xxx.xxx'
port = 80001

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.close()

Somebody please help me.

Any help is greatly appreciated.

Jake

+4  A: 

Edited again to add:

I think you may be missing some basics on socket communication. In order for sockets to work, you need to ensure that the sockets on both your client and server will meet. With your latest revision, your server is now bound to port 63001, but on the local loopback adapter: 127.0.0.1

Computers have multiple network adapters, at least 2: one is the local loopback, which allows you to make network connections to the same machine in a fast, performant manner (for testing, ipc etc), and a network adapter that lets you connect to an actual network. Many computers may have many more adapters (virtual adapters for vlans, wireless vs wired adapters etc), but they will have at least 2.

So in your server application, you need to instruct it to bind the socket to the proper network adapter.

host = ''
port = 63001


bind(host,port)

What this does in python is binds the socket to the loopback adapter (or 127.0.0.1/localhost).

In your client application you have:

host = '80.xxx.xxx.xxx'
port = 63001

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))

Now what your client attempts to do is to connect to a socket to port 63001 on 80.xxx.xxx.xxx (which is your wireless internet adapter).

Since your server is listening on your loopback adapter, and your client is trying to connect on your wireless adapter, it's failing, because the two ends don't meet.

So you have two solutions here:

  1. Change the client to connect to localhost by host = 127.0.0.1
  2. Change the server to bind to your internet adapter by changing host = 80.xxx.xxx.xxx

    Now the first solution, using localhost, will only work when your server and client are on the same machine. Localhost always points back to itself (hence loopback), no matter what machine you try. So if/when you decide to take your client/server to the internet, you will have to bind to a network adapter that is on the internet.

Edited to add:**

Okay with your latest revision it still won't work because 65535 is the largest post available.

Answer below was to the original revision of the question.

In your code posted, you're listening (bound) on port 63001, but your client application is trying to connect to port 80. Thats why your client can't talk to your server. Your client needs to connect using port 63001 not port 80.

Also, unless you're running an HTTP server (or your python server will handle HTTP requests), you really shouldn't bind to port 80.

In your client code change:

import socket
host = '80.xxx.xxx.xxx'
port = 63001

And in your Server Code:

import socket

host = ''
port = 63001

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostbyname(socket.gethostname()), port ))
Alan
Yeah True.. Ehm.. I just changed it to what you said, and now it says that the host didn't reply correctly or didn't reply after a certain amount of time.. I am confused..
Jake
With regards to the internet, how is the machine running your server script connected to the internet? Is is directly plugged into the cable/dsl modem?
Alan
It is connected wirelessly via a router which in turn is connected to a modem.
Jake
That's likely the culprit. Where is your client application being run from? And, do you have port-forwarding set up properly?
Alan
No.. Let me do it now by loggin in to my router.... Or is it something only the ISP can do?
Jake
Ok, so now i unplugged the router and am now connected directly to the internet by cable, but it still isn't working.. What am i doing wrong?
Jake
What is the port value?
Alan
The port number is currently 63001..
Jake
Where are you running your client application?
Alan
I am running both on the same computer. But i thought it would be possible to still do it since netcat for an example can make connections to its own listen server from the same host.
Jake
By running on the same machine, your server may be bound to the loopback adapter (127.0.0.1), while your client is trying to connect over your wireless (since it's bound to the ip).
Alan
+1  A: 

In your server script you have port = 80, but you don't ever use it. It looks like the server is listening on 63001. And the client is connecting to 80.

If you're going to use 80, make sure you don't have an http server trying to use the port at the same time as well.

jonescb
Ok i corrected it now, but when i run it, it is still not working.. I changed the port to 80001.
Jake
80001 is an invalid port. 65535 is the max port value.
Alan