[Tutor] Socket Programming issue

Alan Gauld alan.gauld at btinternet.com
Tue Jun 21 09:15:55 CEST 2011


"aditya" <nauty.me04 at gmail.com> wrote

> is that although the Vbscript writes the output to the file 
> correctly but
> when the client sends the contents of the file to the server via 
> socket , it
> sometimes prints all the lines of the file on the server and 
> sometimes it
> doesnt , i dont know whether the issue is with the client or the 
> server ,

Use the return values from functions.

> CLIENT.PY
>
> import socket
> import os
> import sys
> os.system("C:\Python26\ping.vbs")
> host = "localhost"
> port= 23333
> size = 1024
>
> s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.connect((host,port))
>
> f=open('output.txt','r')
> while 1:
> data = f.readline()

You could print the data here to check that you got something
meaningful from the file....

> if data: s.send(data)

The socket documentation specifically says:
---------------------------------
socket.send(string[, flags])
  Send data to the socket. The socket must be connected to a remote 
socket. The optional flags argument has the same meaning as for recv() 
above. Returns the number of bytes sent. Applications are responsible 
for checking that all data has been sent; if only some of the data was 
transmitted, the application needs to attempt delivery of the 
remaining data.

------------------------------------------
Notice that it tells you how many bytes have been sent, and it
is your responsibility to check and retry if necessary.
I see no check nor any attempt to retry....

That should tell you if the data is leaving the client at least.


> SERVER.PY
> import socket
>
> host = ""
> port = 23333
> size = 1024
> backlog=5
> s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
> s.bind((host,port))
> s.listen(backlog)
>
> while 1:
> client, addr =s.accept()
> data=client.recv(size)
> if data:
> print data
> else:
> print  "client exit"

This implies you should always get something printed, do you?
When you say it sometimes prints the lines and sometimes
not, does it print the exit message instead?

> s.close()

I'm not clear where this line sits since the mail has lost
its indentation. Is it inside the else or at the while level?

Just some things to c0onsider,

HTH


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list