473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

confusion and problems with first socket script

hi all,
i am getting some problems with my first socket
script. can any one of you point me why this is
happening. the server script suppose to accept one
connection at a time and send countdown to client
connected to it from a function through a separate
thread. and echo all the connections opened and
closed. the client script is working fine. it echoes
the countdown it recieve from server.

### CODE for socketserver.py
import socket, thread
import time, sys

def handConnect(con n):
for count in xrange(1,11):
try:
conn.send(str(c onn.getpeername ()) + ":
Counting..."+st r(count)+"\n")
except:
print "Disconnect ed: ", conn.getpeernam e(),
sys.exc_info()[1]
break
time.sleep(2)
print "Closing Connection:", conn.getpeernam e()
conn.close()
def main():
host, port = socket.gethostn ame(), 4786
serversock = socket.socket(s ocket.AF_INET,
socket.SOCK_STR EAM)
serversock.bind ((host,port))
serversock.list en(1)

# According to documentation the argument to listen()
is the number of connections we can accept at one
time.

while True:
conn, addr = serversock.acce pt()
print "Client Connected:", addr
thread.start_ne w_thread(handCo nnect, (conn,))
if __name__ == "__main__":
main()
### Code for socketclient.py
import socket

host, port = socket.gethostn ame(), 4786
sock = socket.socket(s ocket.AF_INET,
socket.SOCK_STR EAM)
sock.connect((h ost, port))

while True:
data = sock.recv(60)
if not data: break
print "Recieved["+str(sock.getp eername())+"]:", data

print "Socket disconnected"

### End
Problem: when i run two instances of the
socketclien.py i get the following output on the
console window of socketserver.py

### OUTPUT from socketserver.py
D:\Python\works pace\practice\s ocket>socketser ver.py
Client Connected: ('10.10.1.8', 1336)
Client Connected: ('10.10.1.8', 1337)
Disconnected: ('10.10.1.8', 1337) (10054, 'Connection
reset by peer')
Closing Connection: ('10.10.1.8', 1337)
Disconnected: ('10.10.1.8', 1336) (10054, 'Connection
reset by peer')
Closing Connection: ('10.10.1.8', 1336)
^C
D:\Python\works pace\practice\s ocket>
### End of Out put

as you can see two connections have been made at one
after another. and i have specify 1 in listen()
argument then why two clients can connect to it. i am
not good in english and thats why i am sorry if this
mail is long and if there are mistakes of grammar and
spellings. i have few more questions about socket
module and socket scripting, i will ask those
questions in my next mail because this mail is already
long enough. Anyways, Thanks in advance.

Hameed khan.

_______________ _______________ ____
Do you Yahoo!?
Yahoo! Mail SpamGuard - Read only the mail you want.
http://antispam.yahoo.com/tools

Jul 18 '05 #1
0 1596

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

Similar topics

1
2487
by: John Hunter | last post by:
I have a test script below which I use to fetch urls into strings, either over https or http. When over https, I use m2crypto.urllib and when over http I use the standard urllib. Whenever, I import sockets and setdefaulttimeout, however, using m2crypto.urllib tends to cause a http.BadStatusLine to be raised, even if the timeout is set to be...
0
1934
by: Jacob Lee | last post by:
I'm getting a rather bizarre error while using the socket module. If I start out disconnected from the net and then connect while the program or interpreter session is open, I do not always gain the ability to resolve or connect to hosts by name. Here is the problematic scenario: <Unplug network cable> >>> from socket import * >>>...
1
1743
by: VB | last post by:
Dear friends, I have a big problem. With an usersonline script I need to extract in php the output of a cgi using buffering output in this way: <?php ob_start(); include('/home/mysite/public_html/cgi-bin/usersonline/online.cgi'); $contents = ob_get_contents(); ob_end_clean();
0
1465
by: Robert Björn | last post by:
I'm trying to process a MemoryStream of XML data, which at any point in time may have incomplete fragments (because data arrives from a socket). If I process data until XmlTextReader.Read() returns false, fills it with more data, and reverses the position of the underlying MemoryStream, the XmlTextReader immediately stops working. I...
0
4475
by: soaphead | last post by:
Can I use Socket.Poll for blocking socket? It is unclear in documentation if I can use this with a synchronous socket, can my code possibly turn a synchronous socket to asynchronous? It seems to work but I have some problems with outside.Receive, it gives me a SocketException "An existing connection was forcibly closed by the remote host" at...
2
11889
by: Rick | last post by:
Hi, I'm trying to get a simple UdpClient app working. I've been looking at the MSDN info regarding UdpClient. When I set it up on my own PC and send messages to myself it works OK. If I try to send messages to another PC with another IP address I get multiple exception, the first one being "The requested address is not valid in its...
0
3456
by: ZR | last post by:
I am writing two applications which needs to (among other things) communicate through network, so one of them is a client and the other one is a server. I have used asynchronous socket examples found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconblockingclientsocketexample.asp as templates (they have to...
1
3173
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket). The README file in the Tools/freeze directory of the Python-2.4.4 distribution says the following (and many other things): Previous...
0
2323
by: clemima | last post by:
Hi, i have a script that listens on a port for simple text messages and then executes sql to a remote mssql database . The script is started via crontab . There are 2 problems : 1 . The line to the remote database appears to go to sleep in the sense that the script receives a null return from the database instead of a message containing the...
0
7915
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
8339
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
7967
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
8220
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
6619
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
5392
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
3840
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
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.