473,385 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sockets, gethostname() changing

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()

May 25 '07 #1
10 4268
On May 24, 8:04 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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

May 25 '07 #2
Thanks for the response.

On May 24, 9:24 pm, half.ital...@gmail.com wrote:
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

May 25 '07 #3
7stud wrote:
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 ----------------

May 25 '07 #4
On May 24, 8:50 pm, 7stud <bbxx789_0...@yahoo.comwrote:
Thanks for the response.

On May 24, 9:24 pm, half.ital...@gmail.com wrote:
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
That would bug me to high hell. A router in the middle would probably
stop that.
May 25 '07 #5
En Fri, 25 May 2007 00:04:04 -0300, 7stud <bb**********@yahoo.com>
escribió:
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.

--
Gabriel Genellina

May 25 '07 #6
<ha**********@gmail.comwrote:
...
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.
Looks like a DHCP configuration issue to me; one might try the remedies
suggested at
<http://www.macosxhints.com/article.php?story=20001224021638403>.
Alex
May 25 '07 #7
>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?


May 25 '07 #8
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 ----------------

May 25 '07 #9
On May 24, 8:04 pm, 7stud <bbxx789_0...@yahoo.comwrote:
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

May 25 '07 #10
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.


May 27 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: pythonhda | last post by:
Hi All, I writing a program to emulate a URL (essentially a very simple raw data proxy). The client should always think that it is only talking to the proxy and the proxy acts like the url it's...
0
by: Eric Brunel | last post by:
Hi all, I just compiled Python 2.3.2 on Linux Mandrake 8.0, upgrading from Python 2.1. I have one problem that I can't figure out: when I wanted to get "the" IP address of the current host with...
1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
1
by: jm | last post by:
Easy probably, please read on. I know some of you have commented already about some of my socket question. I appreciate that. I have a Form1: static void Main() { Application.Run(new...
0
by: Gregory Hassett | last post by:
I am writing a loop which will listen on a given port for incoming UDP packets. If the UDP sender (client), terminates abnormally, then my call to socket.BeginReceiveFrom throws a SocketException....
0
by: Gregory Hassett | last post by:
Hello, I want to periodically send a TCP packet to a peer, always from the same source port. That is, each packet will come from my local ip address, port 8801, and go to the peer's ip address,...
9
by: Gita George | last post by:
I'm trying to write a program (a pop3 mail checker) and I'm having a problem. I'm using the socket control and I've noticed that I do not have any events !? How do I use the socket to receive...
2
by: bil.shah | last post by:
Hi, I am listening to a port for data but I am not able to recieve whole data, I only get truncated data. Client sends me data that exceeds 40K and the data I recieve in my callback function is...
0
by: J008 | last post by:
Just looking for some insight as to why the callback "BeginReceiveFromCallback" is not being called in my "Receive" Subroutine below (when I call BeginReceiveFrom). I am trying to read data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.