473,574 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.gethostn ame(), 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.gethostn ame()
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.gethostn ame()
port = 1274

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

May 25 '07 #1
10 4300
On May 24, 8:04 pm, 7stud <bbxx789_0...@y ahoo.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.gethostn ame(), 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.gethostn ame()
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.gethostn ame()
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...@gm ail.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.gethostn ame()
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.d ial9.xxxxxxx.le vel9.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.gethostn ame(), 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.gethostn ame()
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.gethostn ame()
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.gethostn ame() 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.blogs pot.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...@y ahoo.comwrote:
Thanks for the response.

On May 24, 9:24 pm, half.ital...@gm ail.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.gethostn ame()
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.d ial9.xxxxxxx.le vel9.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**********@y ahoo.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.gethostn ame(), 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**********@g mail.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.d ial9.xxxxxxx.le vel9.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?sto ry=200012240216 38403>.
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.com p

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.p y", 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.p y", 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.com p

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.gethostn ame() #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.gethostn ame() #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.blogs pot.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...@y ahoo.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.gethostn ame(), 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.gethostn ame()
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.gethostn ame()
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

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

Similar topics

0
1490
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 emulating. My *immediate* problem is how do I tell the client that the web server has finished sending? I know that when you recieve 0 bytes from...
0
6012
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 Python 2.1, I did: Python 2.1.1 (#12, Apr 10 2002, 17:52:08) on linux2 Type "copyright", "credits" or "license" for more information. >>>...
1
3773
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 pending connections every 500ms. I also have a vb6 application that uses the WinSock control at the other end of the communication tunel. I have to...
1
1757
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 Form1()); clsListening.AsynchronousSocketListener.StartListening();
0
3360
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. I catch this exception, then re-create the socket, and re-bind it. The next time I call BeginReceiveFrom, I get that same exception again (" An...
0
3014
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, to HIS port 8801. This means that I need to bind my sender socket to (myaddress,8801), use it for the send, then shut it down and close it. Then...
9
2451
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 data? How do I simulate the DataArrival event from Winsock (VB 6.0)? How can I create and alternate event? This also is the case for the tcpClient...
2
2031
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 always 8K to 9K. I dont get the rest of the data. I think the data size is too big to come in one go and hence it comes in 2-3 sub-packages but my...
0
4263
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 asynchronously using UDP. I have implemented Send and Connect subroutines in a similar way, and they both seem to work. When I put a breakpoint in the...
0
8092
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8262
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8129
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6497
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5641
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5331
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3768
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3785
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1092
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.