sockets, gethostname() changing

7

7stud

Hi,

I'm experimenting with a basic socket program(from a book), and both
the client and server programs are on my computer. In both programs,
I call socket.gethostname(), but I discovered that when I am connected
to the internet, both the client and server hang and nothing happens.
I discovered that the hostname of my computer automatically changes to
that of my isp when I'm connected to the internet, and presumably the
server program on my computer cannot listen on my isp's address(host,
port). Is there a way to make the hostname of my computer static, so
that it doesn't change to my isp's hostname when I connect to the
internet. I'm using mac os 10.4.7. Why does my computer's hostname
dynamically change in the first place?

server program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
print host
port = 1274
s.bind((host, port))

s.listen(5)
while("Ctrl-C hasn't been entered"):
c, addr = s.accept() #blocks and waits for client connection
print "Got socket connection from", addr
c.send("Thank you for connecting. Now get lost.")
c.close()


client program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
port = 1274

s.connect((host, port))
print s.recv(1024)
s.close()
 
H

half.italian

Hi,

I'm experimenting with a basic socket program(from a book), and both
the client and server programs are on my computer. In both programs,
I call socket.gethostname(), but I discovered that when I am connected
to the internet, both the client and server hang and nothing happens.
I discovered that the hostname of my computer automatically changes to
that of my isp when I'm connected to the internet, and presumably the
server program on my computer cannot listen on my isp's address(host,
port). Is there a way to make the hostname of my computer static, so
that it doesn't change to my isp's hostname when I connect to the
internet. I'm using mac os 10.4.7. Why does my computer's hostname
dynamically change in the first place?

server program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
print host
port = 1274
s.bind((host, port))

s.listen(5)
while("Ctrl-C hasn't been entered"):
c, addr = s.accept() #blocks and waits for client connection
print "Got socket connection from", addr
c.send("Thank you for connecting. Now get lost.")
c.close()

client program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
port = 1274

s.connect((host, port))
print s.recv(1024)
s.close()

I can't imagine why your hostname would be changing, unless you
installed some of their proprietary software thats messing around with
things. What is the hostname set to in Sys Prefs->Sharing? Try
setting it there. What are the before and after connection names you
get?

~Sean
 
7

7stud

Thanks for the response.

I can't imagine why your hostname would be changing, unless you
installed some of their proprietary software thats messing around with
things.

When I first started using Terminal, I noticed that the prompt in
Terminal changed when I was connected to the internet.
What is the hostname set to in Sys Prefs->Sharing?

My Name's Computer
Try
setting it there. What are the before and after connection names you
get?

If I add the line:

host = socket.gethostname()
print host #<----------

and I'm not connected to the internet and I run the program, I get:

my-names-computer.local

When I'm connected to the internet, I get:

dialup-9.999.999.999.dial9.xxxxxxx.level9.net
 
S

Steve Holden

7stud said:
Hi,

I'm experimenting with a basic socket program(from a book), and both
the client and server programs are on my computer. In both programs,
I call socket.gethostname(), but I discovered that when I am connected
to the internet, both the client and server hang and nothing happens.
I discovered that the hostname of my computer automatically changes to
that of my isp when I'm connected to the internet, and presumably the
server program on my computer cannot listen on my isp's address(host,
port). Is there a way to make the hostname of my computer static, so
that it doesn't change to my isp's hostname when I connect to the
internet. I'm using mac os 10.4.7. Why does my computer's hostname
dynamically change in the first place?

server program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
print host
port = 1274
s.bind((host, port))

s.listen(5)
while("Ctrl-C hasn't been entered"):
c, addr = s.accept() #blocks and waits for client connection
print "Got socket connection from", addr
c.send("Thank you for connecting. Now get lost.")
c.close()


client program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
port = 1274

s.connect((host, port))
print s.recv(1024)
s.close()
For local testing it is *much* easier to have your client and server use
IP address 127.0.0.1 - this is the usual address on the "loopback"
network, which doesn't require any physical network hardware to operate.

Just as a matter of interest, what is socket.gethostname() returning? I
suspect it will depend on whether you have established an interface
specific domain suffix - I haven't, and I have no trouble with your code.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------
 
H

half.italian

Thanks for the response.



When I first started using Terminal, I noticed that the prompt in
Terminal changed when I was connected to the internet.


My Name's Computer


If I add the line:

host = socket.gethostname()
print host #<----------

and I'm not connected to the internet and I run the program, I get:

my-names-computer.local

When I'm connected to the internet, I get:

dialup-9.999.999.999.dial9.xxxxxxx.level9.net

That would bug me to high hell. A router in the middle would probably
stop that.
 
G

Gabriel Genellina

I'm experimenting with a basic socket program(from a book), and both
the client and server programs are on my computer. In both programs,
I call socket.gethostname(), but I discovered that when I am connected
to the internet, both the client and server hang and nothing happens.
I discovered that the hostname of my computer automatically changes to
that of my isp when I'm connected to the internet, and presumably the
server program on my computer cannot listen on my isp's address(host,
port). Is there a way to make the hostname of my computer static, so
that it doesn't change to my isp's hostname when I connect to the
internet. I'm using mac os 10.4.7. Why does my computer's hostname
dynamically change in the first place?

Don't use any hostname at all; use '' instead. That should bind to any
interfase on your computer.
 
7

7stud

For local testing it is *much* easier to have your client
and server use IP address 127.0.0.1

According to my book, an address is a tuple of the form (hostname,
port), so I didn't know what you meant by using 127.0.0.1 as the
address. I played around with it, and using the tuple ("127.0.0.1",
1234) for the address worked(the server and client were able to
communicate) and I got the expected output -- whether I was connected
to the internet or not. I experimented a little more and "localhost"
also worked for the hostname, and when I did that this line:

c, addr = s.accept()

produced an addr that contained "127.0.0.1" for the hostname.
Don't use any hostname at all; use '' instead. That should bind
to any interfase on your computer.

That worked too when I was connected to the internet, and addr once
again contained "127.0.0.1" for the hostname.

Looks like a DHCP configuration issue to me; one might try the
remedies suggested at

I previously read similar postings about changing the HOSTNAME in /etc/
hostconfig from AUTOMATIC to a chosen name, but when I looked at /etc/
hostconfig there was no entry for HOSTNAME. Ok, I'll give it a
whirl...

I added the line:

HOSTNAME=my.comp

to /etc/hostconfig, and after restarting my computer, that succeeded
in making the prompt in Terminal stay the same whether I was connected
to the internet or not. But I got these errors when I tried to run my
client/server programs:

not connected to internet:
---------------------------
Traceback (most recent call last):
File "./programs_python/socketsServer.py", line 9, in ?
s.bind((host, port))
File "<string>", line 1, in bind
socket.gaierror: (7, 'No address associated with nodename')

connected to the internet:
-------------------------
Traceback (most recent call last):
File "./programs_python/socketsServer.py", line 9, in ?
s.bind((host, port))
File "<string>", line 1, in bind
socket.error: (49, "Can't assign requested address")
-------------------

So I deleted the line:

HOSTNAME=my.comp

from the hostconfig file and restarted my computer. After that my
client/server programs would run as before. But now I get different
output from my server program:

Got socket connection from ('127.0.0.1', 49222)

The strange thing is: the hostname and port in the output are not what
I'm using in my server program:
---------
import socket

s = socket.socket()

print "made changes 2"

host = socket.gethostname() #I'm not connected to the internet when I
use this line
print host

port = 1291
s.bind((host, port))

s.listen(5)
while("Ctrl-C hasn't been entered"):
c, addr = s.accept() #blocks and waits for client connection
print "Got socket connection from", addr
c.send("Thank you for connecting. Now get lost.")
c.close()
----------

The full output of that program is:

made changes 2
my-names-computer.local
Got socket connection from ('127.0.0.1', 49222)

The hostname now appears to be permanently stuck as "127.0.0.1", and
the port is wrong. That output was so confusing to me, I wasn't even
sure whether the file I was editing was actually the file that was
executing, so I printed out "made changes #" at the top of the file to
make sure the file I was editing was the one that was actually
executing.

I can't remember exactly what the output was for addr before I started
messing around with the HOSTNAME in /etc/config, but I'm pretty sure
addr contained the same hostname as the line above it in the output,
and the port matched the port in the program.

Any ideas why the hostname and port in the last line of the output are
not the same as the ones used in the program anymore?
 
S

Steve Holden

7stud wrote:
[...]
The strange thing is: the hostname and port in the output are not what
I'm using in my server program:
---------
import socket

s = socket.socket()

print "made changes 2"

host = socket.gethostname() #I'm not connected to the internet when I
use this line
print host

port = 1291
s.bind((host, port))

s.listen(5)
while("Ctrl-C hasn't been entered"):
c, addr = s.accept() #blocks and waits for client connection
print "Got socket connection from", addr
c.send("Thank you for connecting. Now get lost.")
c.close()
----------

The full output of that program is:

made changes 2
my-names-computer.local
Got socket connection from ('127.0.0.1', 49222)

The hostname now appears to be permanently stuck as "127.0.0.1", and
the port is wrong. That output was so confusing to me, I wasn't even
sure whether the file I was editing was actually the file that was
executing, so I printed out "made changes #" at the top of the file to
make sure the file I was editing was the one that was actually
executing.

I can't remember exactly what the output was for addr before I started
messing around with the HOSTNAME in /etc/config, but I'm pretty sure
addr contained the same hostname as the line above it in the output,
and the port matched the port in the program.

Any ideas why the hostname and port in the last line of the output are
not the same as the ones used in the program anymore?
Because the client is using what's called an "ephemeral" port - it is
getting an arbitrary port number from the TCP layer, guaranteed to be
unused by any other socket, and using that to connect to your server
socket. Remember a connection has two ends - the details you are
printing out from your server are those of the client endpoint.

If you run several clients simultaneously you will find that each uses a
different port number. That's exactly what's needed to make sure that
the protocol stack can deliver the right information to the right client.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------
 
H

half.italian

Hi,

I'm experimenting with a basic socket program(from a book), and both
the client and server programs are on my computer. In both programs,
I call socket.gethostname(), but I discovered that when I am connected
to the internet, both the client and server hang and nothing happens.
I discovered that the hostname of my computer automatically changes to
that of my isp when I'm connected to the internet, and presumably the
server program on my computer cannot listen on my isp's address(host,
port). Is there a way to make the hostname of my computer static, so
that it doesn't change to my isp's hostname when I connect to the
internet. I'm using mac os 10.4.7. Why does my computer's hostname
dynamically change in the first place?

server program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
print host
port = 1274
s.bind((host, port))

s.listen(5)
while("Ctrl-C hasn't been entered"):
c, addr = s.accept() #blocks and waits for client connection
print "Got socket connection from", addr
c.send("Thank you for connecting. Now get lost.")
c.close()

client program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
port = 1274

s.connect((host, port))
print s.recv(1024)
s.close()

Try setting an environment variable for 'hostname' using this:
http://www.versiontracker.com/dyn/moreinfo/macosx/15073

Either way, its a good program to have.

~Sean
 
7

7stud

I figured something out that succeeded in making the hostname
constant, and it allows me to run the socket programs without error.
I'm posting what I did for future seekers. This is for mac os 10.4.7:

1) In System Preferences>Sharing, there is a name entered there:

Computer Name: John Smiths computer

But underneath that is this text:

Other computers on your local subnet can access your
computer at John-Smiths-computer.local

Copy the name John-Smiths-computer.local exactly as written. Notice
the added dashes and the extension. If you want, you can change the
name by entering something else in the text box to the right of
"Computer Name:". Then if you click outside the text box elsewhere to
move the focus away from the text box, the text underneath the text
box will update with the new name, and that is the exact name you need
to use for step 2.

2) In the file /etc/hostconfig, at the bottom I added the line:

HOSTNAME=John-Smiths-computer.local

The name entered there has to be the exact same name as the name in
step 1.

The hostconfig file is read only unless you are logged in using the
root account(which you are never supposed to do). In any case, you
can use the sudo command to momentarily become the superuser, which
will enable you to edit hostcofig. If you are logged in as the root
account, then you'll be able to edit hostconfig without having to use
the sudo command.

I used the cd command to change directories to /etc:

$ cd /etc

Then I used vi to edit the hostconfig file

$ sudo vi hostconfig

Hold your breath because you don't want to mess anything up when you
have root privileges, although I don't think you can get in much
trouble while editing a text file. Then save the file.


3) Restart your computer. I think there must be a way to reload the
hostconfig file with out having to restart, but I couldn't figure out
which command to use to accomplish that. After restarting, the
hostname part of my prompt in Terminal stayed constant whether I was
connected to the internet or not. And I could run the socket programs
in my original post using gethostname()--whether I was connected to
the internet or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top