473,543 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

server side socket program hangs

hi,
i am writing a socket program in python,both client side and server
side.I've written the client side which is working perfectly
fine(checked it against server program written in C).but as for my
server program written in python it simply hangs.it does not show any
error also.I've tried sample programs available .I don understand what
the reason is as i am quite new to it.
here is teh server side program:

///////////////////////
from socket import *
import socket
import sys

HOST = '' #any address
PORT = htons(9999) #same port address as client

try:
s = socket.socket(s ocket.AF_INET,s ocket.SOCK_STRE AM)
except socket.error:
print 'socket not created'
try:
s.bind((HOST,PO RT))
except socket.error:
print 'error in bind'
try:
s.listen(5)
except socket.error:
print 'error in listen'
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()

can someone tell me what the problem is?
Jul 18 '05 #1
11 8489
<an**********@s ify.com> wrote in message
news:57******** *************** ***@posting.goo gle.com...
hi,
i am writing a socket program in python,both client side and server
side.I've written the client side which is working perfectly
fine(checked it against server program written in C).but as for my
server program written in python it simply hangs.it does not show any
error also.I've tried sample programs available .I don understand what
the reason is as i am quite new to it.
here is teh server side program:

///////////////////////
from socket import *
import socket
import sys

HOST = '' #any address
PORT = htons(9999) #same port address as client
<snip code>
can someone tell me what the problem is?


Your applying a function designed to simplyfy transfering IP addresses to a
literal value you want to use for your port. The following should show the
effect thats now happening.

PythonWin 2.3.1 (#47, Sep 23 2003, 23:47:32) [MSC v.1200 32 bit (Intel)] on
win32.
Portions Copyright 1994-2001 Mark Hammond (mh******@skipp inet.com.au) - see
'Help/About PythonWin' for further copyright information.
import socket
socket.htons(99 99) 3879


--
Anthony McDonald

This email address is protected by SpamBayes
www.spambayes.org
Jul 18 '05 #2
hi,
was stuck with another work.now bac to python program.
i tried out using
PORT = socket.htons(99 99)

it still doesn't work.here is the code.it still hangs.can some one
tell me what could be te problem?

#from socket import *
import socket
import sys

HOST = '' #any address
PORT = socket.htons(92 22) #same port address as client

class Sock:
def __init__ (self,parent):
try:
self.s = socket(AF_INET, SOCK_STREAM)
except socket.error:
print 'socket not created'
try:
self.s.bind((HO ST,PORT))
except self.error:
print 'error in bind'
try:
self.s.listen(5 )
except self.error:
print 'error in listen'
conn, addr = self.s.accept()
# print 'Connected by',`addr`
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()

thanx,
AKR.

"Anthony McDonald" <tonym1972/at/club-internet/in/fr> wrote in message news:<3f******* *************** *@news.club-internet.fr>...
<an**********@s ify.com> wrote in message
news:57******** *************** ***@posting.goo gle.com...
hi,
i am writing a socket program in python,both client side and server
side.I've written the client side which is working perfectly
fine(checked it against server program written in C).but as for my
server program written in python it simply hangs.it does not show any
error also.I've tried sample programs available .I don understand what
the reason is as i am quite new to it.
here is teh server side program:

///////////////////////
from socket import *
import socket
import sys

HOST = '' #any address
PORT = htons(9999) #same port address as client

<snip code>

can someone tell me what the problem is?


Your applying a function designed to simplyfy transfering IP addresses to a
literal value you want to use for your port. The following should show the
effect thats now happening.

PythonWin 2.3.1 (#47, Sep 23 2003, 23:47:32) [MSC v.1200 32 bit (Intel)] on
win32.
Portions Copyright 1994-2001 Mark Hammond (mh******@skipp inet.com.au) - see
'Help/About PythonWin' for further copyright information.
import socket
socket.htons(99 99) 3879

Jul 18 '05 #3
<an**********@s ify.com> wrote in message
news:57******** *************** ***@posting.goo gle.com...
hi,
was stuck with another work.now bac to python program.
i tried out using
PORT = socket.htons(99 99)

it still doesn't work.here is the code.it still hangs.can some one
tell me what could be te problem?

#from socket import *
import socket
import sys

HOST = '' #any address
PORT = socket.htons(92 22) #same port address as client

class Sock:
def __init__ (self,parent):
try:
self.s = socket(AF_INET, SOCK_STREAM)
except socket.error:
print 'socket not created'
try:
self.s.bind((HO ST,PORT))
except self.error:
print 'error in bind'
try:
self.s.listen(5 )
except self.error:
print 'error in listen'
conn, addr = self.s.accept()
# print 'Connected by',`addr`
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()

thanx,
AKR.

Sorry for my brief answer last time, but I had hoped the code fragment I
posted would explain what was happening with your code.

Your code is almost a verbatum copy of the Python example, except you've
chosen to massage the port value using the HTONS function. So for most
architectures the port value 9999 or 9222 would be changed to somewhere in
the 3xxx port range. Any client trying to connect on those ports will fail.
Hence you noted success on a C server which isn't using that function, but
failure on your Python server which is. The solution is to just assign the
port value you want unchanged.

--
Anthony McDonald

This email address is protected by SpamBayes
www.spambayes.org
Jul 18 '05 #4
hi,
My one problem is solved,python server no more hangs,but my main
purpose still remains unsolved,my client is not able to establish
connection.my client is a C program running on a windows machine.On
running the python program,in the python shell i get this way
(cursor)

i assumed my server is waiting for a connection,but my client did not
connect,it failed.
I tried debugging the python program using the step by step debug
control.it moves till def __init__(Self,p arent):
and stops(seems to be waiting).It does not move to the next line.
Can you tell me what is happening? and what is the problem with my
server program?Sorry for the trouble,
thanx
AKR.



"Anthony McDonald" <tonym1972/at/club-internet/in/fr> wrote in message news:<3f******* *************** *@news.club-internet.fr>... <an**********@s ify.com> wrote in message
news:57******** *************** ***@posting.goo gle.com...
hi,
was stuck with another work.now bac to python program.
i tried out using
PORT = socket.htons(99 99)

it still doesn't work.here is the code.it still hangs.can some one
tell me what could be te problem?

#from socket import *
import socket
import sys

HOST = '' #any address
PORT = socket.htons(92 22) #same port address as client

class Sock:
def __init__ (self,parent):
try:
self.s = socket(AF_INET, SOCK_STREAM)
except socket.error:
print 'socket not created'
try:
self.s.bind((HO ST,PORT))
except self.error:
print 'error in bind'
try:
self.s.listen(5 )
except self.error:
print 'error in listen'
conn, addr = self.s.accept()
# print 'Connected by',`addr`
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()

thanx,
AKR.

Sorry for my brief answer last time, but I had hoped the code fragment I
posted would explain what was happening with your code.

Your code is almost a verbatum copy of the Python example, except you've
chosen to massage the port value using the HTONS function. So for most
architectures the port value 9999 or 9222 would be changed to somewhere in
the 3xxx port range. Any client trying to connect on those ports will fail.
Hence you noted success on a C server which isn't using that function, but
failure on your Python server which is. The solution is to just assign the
port value you want unchanged.

Jul 18 '05 #5
hi,
i've posted my doubt on my server program written in python.i am not
able to communicate with any client.My server program does not even
create a socket.can someone tell me what the problem is?
thanx,
AKR
an**********@si fy.com wrote in message news:<57******* *************** ****@posting.go ogle.com>...
hi,
My one problem is solved,python server no more hangs,but my main
purpose still remains unsolved,my client is not able to establish
connection.my client is a C program running on a windows machine.On
running the python program,in the python shell i get this way

(cursor)

i assumed my server is waiting for a connection,but my client did not
connect,it failed.
I tried debugging the python program using the step by step debug
control.it moves till def __init__(Self,p arent):
and stops(seems to be waiting).It does not move to the next line.
Can you tell me what is happening? and what is the problem with my
server program?Sorry for the trouble,
thanx
AKR.



"Anthony McDonald" <tonym1972/at/club-internet/in/fr> wrote in message news:<3f******* *************** *@news.club-internet.fr>...
<an**********@s ify.com> wrote in message
news:57******** *************** ***@posting.goo gle.com...
hi,
was stuck with another work.now bac to python program.
i tried out using
PORT = socket.htons(99 99)

it still doesn't work.here is the code.it still hangs.can some one
tell me what could be te problem?

#from socket import *
import socket
import sys

HOST = '' #any address
PORT = socket.htons(92 22) #same port address as client

class Sock:
def __init__ (self,parent):
try:
self.s = socket(AF_INET, SOCK_STREAM)
except socket.error:
print 'socket not created'
try:
self.s.bind((HO ST,PORT))
except self.error:
print 'error in bind'
try:
self.s.listen(5 )
except self.error:
print 'error in listen'
conn, addr = self.s.accept()
# print 'Connected by',`addr`
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()

thanx,
AKR.

Sorry for my brief answer last time, but I had hoped the code fragment I
posted would explain what was happening with your code.

Your code is almost a verbatum copy of the Python example, except you've
chosen to massage the port value using the HTONS function. So for most
architectures the port value 9999 or 9222 would be changed to somewhere in
the 3xxx port range. Any client trying to connect on those ports will fail.
Hence you noted success on a C server which isn't using that function, but
failure on your Python server which is. The solution is to just assign the
port value you want unchanged.

Jul 18 '05 #6
hi,
my initial program was to take host value as ''(INADDR_ANY)
only.during my trial and error only i started using
gethostname.how ever as of now i am running my client (C program) also
from the same machine only.so it should not make any difference.
I've reverted bac to host ''.my program still hangs.i 'v e tried
changing the port address,but doesn't work.pls help
thanx,
AKR.
Jørgen Cederberg <jo************ *@hotmail.com> wrote in message news:<9s******* ********@news.g et2net.dk>...
an**********@si fy.com wrote:
hi,
i guess no one is ready to help.neways,my problem is getting
solved.I 've slightly modified program (one of the samples found in
the net),now program creates socket,gives bind ,listen success.however
when i debug at accept it goes to the socket.py,at the accept()
function definition.and one more step ahead and it hangs.i dono how to
debug here(inside socket.py).

here is how the code looks like


Hi ...

the program actually works - but there is a quirk, look below for details.

////////////////
from socket import *
import sys

try:
s= socket(AF_INET, SOCK_STREAM)
print 'socket created'
except error:
print 'socket not created'
host = ''
port = 9323
try:
s.bind((gethost name(),port))


You are binding the socket to the hostname of your computer, instead of
''. Thus it will only accept connections specific to the hostname and
therefor fail if you try to connect to localhost or 127.0.0.1

print 'bind success'
except error:
print 'bind'
try:
s.listen(1)
print 'listen success'
except error:
print 'listen'

conn,addr = s.accept()
print 'client is at ', addr
data = conn.recv(1024)
conn.send(data)
conn.close()
////////////////////////
but i have a feeling that teher's some problem with the port or
some problem in the program that it hangs.can anyone help?or atleast
give me few links where i can find a solution,thanx.
AKR.

Hope this helps
Jorgen

Jul 18 '05 #7
an**********@si fy.com wrote:
hi,
my initial program was to take host value as ''(INADDR_ANY)
only.during my trial and error only i started using
gethostname.how ever as of now i am running my client (C program) also
from the same machine only.so it should not make any difference.
I've reverted bac to host ''.my program still hangs.i 'v e tried
changing the port address,but doesn't work.pls help
thanx,
AKR.
Hi

this is pretty odd. As mentioned in the previous mail, the program works
with my client program (written in Python by the way).

You keep saying that the programs 'hang', what do mean by that? I've
tried the program and all I can say is that it works. Are you sure that
your client works correctly?

Regards
Jorgen



Jørgen Cederberg <jo************ *@hotmail.com> wrote in message news:<9s******* ********@news.g et2net.dk>...
an**********@si fy.com wrote:
> hi,
> i guess no one is ready to help.neways,my problem is getting
> solved.I 've slightly modified program (one of the samples found in
> the net),now program creates socket,gives bind ,listen success.however
> when i debug at accept it goes to the socket.py,at the accept()
> function definition.and one more step ahead and it hangs.i dono how to
> debug here(inside socket.py).
>
> here is how the code looks like


Hi ...

the program actually works - but there is a quirk, look below for details.
>
> ////////////////
> from socket import *
> import sys
>
> try:
> s= socket(AF_INET, SOCK_STREAM)
> print 'socket created'
> except error:
> print 'socket not created'
> host = ''
> port = 9323
> try:
> s.bind((gethost name(),port))


You are binding the socket to the hostname of your computer, instead of
''. Thus it will only accept connections specific to the hostname and
therefor fail if you try to connect to localhost or 127.0.0.1

> print 'bind success'
> except error:
> print 'bind'
> try:
> s.listen(1)
> print 'listen success'
> except error:
> print 'listen'
>
> conn,addr = s.accept()
> print 'client is at ', addr
> data = conn.recv(1024)
> conn.send(data)
> conn.close()
> ////////////////////////
> but i have a feeling that teher's some problem with the port or
> some problem in the program that it hangs.can anyone help?or atleast
> give me few links where i can find a solution,thanx.
> AKR.



Hope this helps
Jorgen


Jul 18 '05 #8
hi,
Well i dono wat more to say.My client side is working properly ,the
error i get in the client side is connection refused.my client
responds to other server programs.
In my earlier postings i'd mentioned that while debugging i am not
able to understand what is happening at the def accept()(functi on call
in socket.py).
I use debugger control to debug,my program stays still at
accept().First i thought its waiting for a connection,but then my
client failed,morover my server is not ending.(it is not because of
the infinite while loop,i 've checked without the while loop).
When i try to close this program alone all the python applications
close.i guess itz hanging only.I don think it is system dependent.
Neways i tried from other machines ,the program still hangs.I am sorry
,but this is wat all i can say.
Do tell if there are any kind of problem related.
thanx,
AKR.
Jørgen Cederberg <jo************ *@hotmail.com> wrote in message news:<dK******* ********@news.g et2net.dk>...
an**********@si fy.com wrote:
hi,
my initial program was to take host value as ''(INADDR ANY)
only.during my trial and error only i started using
gethostname.how ever as of now i am running my client (C program) also
from the same machine only.so it should not make any difference.
I've reverted bac to host ''.my program still hangs.i 'v e tried
changing the port address,but doesn't work.pls help
thanx,
AKR.


Hi

this is pretty odd. As mentioned in the previous mail, the program works

with my client program (written in Python by the way).

You keep saying that the programs 'hang', what do mean by that? I've
tried the program and all I can say is that it works. Are you sure that
your client works correctly?

Regards
Jorgen



J rgen Cederberg <jo************ *@hotmail.com> wrote in message news:

<9s************ ***@news.get2ne t.dk>...
an**********@si fy.com wrote:
> hi,
> i guess no one is ready to help.neways,my problem is getting
> solved.I 've slightly modified program (one of the samples found in
> the net),now program creates socket,gives bind ,listen success.howev er > when i debug at accept it goes to the socket.py,at the accept()
> function definition.and one more step ahead and it hangs.i dono how to > debug here(inside socket.py).
>
> here is how the code looks like

Hi ...

the program actually works - but there is a quirk, look below for deta ils.
>
> ////////////////
> from socket import *
> import sys
>
> try:
> s= socket(AF INET,SOCK STREAM)
> print 'socket created'
> except error:
> print 'socket not created'
> host = ''
> port = 9323
> try:
> s.bind((gethost name(),port))

You are binding the socket to the hostname of your computer, instead o f ''. Thus it will only accept connections specific to the hostname and therefor fail if you try to connect to localhost or 127.0.0.1
> print 'bind success'
> except error:
> print 'bind'
> try:
> s.listen(1)
> print 'listen success'
> except error:
> print 'listen'
>
> conn,addr = s.accept()
> print 'client is at ', addr
> data = conn.recv(1024)
> conn.send(data)
> conn.close()
> ////////////////////////
> but i have a feeling that teher's some problem with the port or
> some problem in the program that it hangs.can anyone help?or atleast give me few links where i can find a solution,thanx.
> AKR.
Hope this helps
Jorgen

Jul 18 '05 #9
hi,
do u think there's a version problem?i am using Python 2.2 (windows
2000).is it related in any way?
thanx,AKR.

Jørgen Cederberg <jo************ *@hotmail.com> wrote in message news:<dK******* ********@news.g et2net.dk>...
an**********@si fy.com wrote:
hi,
my initial program was to take host value as ''(INADDR ANY)
only.during my trial and error only i started using
gethostname.how ever as of now i am running my client (C program) also
from the same machine only.so it should not make any difference.
I've reverted bac to host ''.my program still hangs.i 'v e tried
changing the port address,but doesn't work.pls help
thanx,
AKR.


Hi

this is pretty odd. As mentioned in the previous mail, the program works

with my client program (written in Python by the way).

You keep saying that the programs 'hang', what do mean by that? I've
tried the program and all I can say is that it works. Are you sure that
your client works correctly?

Regards
Jorgen



J rgen Cederberg <jo************ *@hotmail.com> wrote in message news:

<9s************ ***@news.get2ne t.dk>...
an**********@si fy.com wrote:
> hi,
> i guess no one is ready to help.neways,my problem is getting
> solved.I 've slightly modified program (one of the samples found in
> the net),now program creates socket,gives bind ,listen success.howev er > when i debug at accept it goes to the socket.py,at the accept()
> function definition.and one more step ahead and it hangs.i dono how to > debug here(inside socket.py).
>
> here is how the code looks like

Hi ...

the program actually works - but there is a quirk, look below for deta ils.
>
> ////////////////
> from socket import *
> import sys
>
> try:
> s= socket(AF INET,SOCK STREAM)
> print 'socket created'
> except error:
> print 'socket not created'
> host = ''
> port = 9323
> try:
> s.bind((gethost name(),port))

You are binding the socket to the hostname of your computer, instead o f ''. Thus it will only accept connections specific to the hostname and therefor fail if you try to connect to localhost or 127.0.0.1
> print 'bind success'
> except error:
> print 'bind'
> try:
> s.listen(1)
> print 'listen success'
> except error:
> print 'listen'
>
> conn,addr = s.accept()
> print 'client is at ', addr
> data = conn.recv(1024)
> conn.send(data)
> conn.close()
> ////////////////////////
> but i have a feeling that teher's some problem with the port or
> some problem in the program that it hangs.can anyone help?or atleast give me few links where i can find a solution,thanx.
> AKR.
Hope this helps
Jorgen

Jul 18 '05 #10

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

Similar topics

0
3624
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on Linux, connecting to MS SQL Server, unless it was already pre-installed by your Linux installation, is to build your own multithreaded TCP socket...
10
8205
by: RadioFreq | last post by:
I am writing an applet to display Julian Day and Time on a webpage. I have all the code written except for a class to open a socket and extract a String with the time in it. I am getting the time from (NIST), a government site. The problem that I am running into is that it seems that I can extract the time from the server as application,...
4
9417
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I create a socket, connect to the server...then I pass the socket to a class which just reads from the socket (in a thread). class Reader(Thread): def run(self): while 1:
4
6562
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times. There is no consistency when it fails. Could anyone here shed some light on how to debug/resolve the issue. I guess IBM looked at the issue and were...
10
5288
by: feel52 | last post by:
Below you'll find the code i'm working on. It's in a button click routine and hangs after 3 or 4 sometimes 5 loops done, probably in sock.receive(....). Some code was found here( on google i mean) but the amazing( at least for me) is that if i run the program step by step or i cancel the loop and run it by pressing the button again and...
1
1496
by: Mike Dole | last post by:
I'm sorry to bother you with this question but it was either this or giving up and trying to go for a simpler solution (which I will if this is not gonna work out..) I'm afraid this is way out of my league but then again why not give it a shot... I've used the Pocket PC Signature Application Sample from:...
4
6709
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
0
1386
by: Blog the Haggis | last post by:
Hi all, I've written a program which distributes binary data to a number of clients via TCP. The program runs perfectly unless one of the client programs hangs and then it freezes while waiting for the socket.send(Data) method to complete. I've tried all of these socket options to overcome this event: 'Set send timeout to 2 seconds:
4
3384
by: Babloo | last post by:
Hi everyone, i wanted to implement a client- server connection and transfer a file over the network. i was able to implement a normal set up where in i was able to send chat messages and small chunks of data. I want to implement the file transfer with out using the FTP library available in python . So can anyone suggest a piece of code...
0
7399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7729
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...
1
7332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7677
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
5880
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...
0
3385
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
3386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
626
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.