[Tutor] Socket programming

Alan Gauld alan.gauld at btinternet.com
Sat Jan 3 14:20:01 CET 2015


On 03/01/15 11:48, pramod gowda wrote:

> client code:
>
> import socket
>
> client_socket=socket.socket()
> server_address='192.168.2.2'
> server_port= 80

see below regarding port numbers

> print("hello")
> client_socket.connect((server_address,server_port))
> print("hello")
> data=client_socket.recv(1024)
> print(data)
> client_socket.close()


I assume it is this code that is not doing what you expect?
Try using more descriptive print statements.
Also, when debugging, never use a print that could be blank,
always have a fixed string so you can see it. So instead of

print(data)

use

print('Received: ', data)

That way you can see that data was empty rather than that
the print didn't execute.

I'd also put a print(finished') or similar after closing
the socket.

> server code:
> import socket
>
> server_socket=socket.socket()
> server_name='192.168.2.2'
> server_port= 80

port 80 is usually reserved for http traffic, better
to choose a port number greater than 1024 for user
programs.

> server_socket.bind((server_name,server_port))
> server_socket.listen(1)

I'd up the queue size to at least 3  or 5.
You are not leaving much room for errors.

> while True:
>      print("hello")
>      c,address=server_socket.accept()
>      print("we got connection from:",address)

Is this print appearing OK?

>      c.send("hello,hw ru")

I'd add a print here too, to verify that the send worked.

>      c.close()

> I am not getting the output, i am using windows 7 OS,pyhton ver:..
> please  check and give me the solution.

It's not clear what you mean by "the output".
Are you getting anything printed? At the client and the server?

It might help to show us a cut n paste from both client
and server.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list