Python Socket Programming Tutorial (3)

Posted: July 11, 2013 in Not Categorized

Hello everyone, in the last two tutorial we have learn about the socket declaration and how to create a server socket using bind, listen and accept function.

In this tutorial we will look at the client socket, more specific we will look at the socket.connect() function. If we are dealing with the connection oriented network service (SOCK_STREAM) or (SOCK_SEQPACKET) then before we exchange data, we need to create a connection between the socket of the process requesting the service (the client) and the process providing the service (the server).

Writing a client socket is fairly easy if we compare it with server. we just have to use connect() function to connect the server.

Syntax :-

socket.connect(address)

The address we specify with connect is the address of the server with which we want to communicate.

When we try to connect with a server, the connect request might fail for several reasons.

1) The machine to which we are trying to connect must be up and running.

2) The server must be bound to the address we are trying to contact.

3) There must be room in the server pending queue.(we will look this in the example later)

Any application you are writing must be able to handle the connect error return that might be caused by the transient condition,we will look at the error handling.

This is all about the client in the next tutorial we will look at the address look up.

Follow us on Facebook.

Thanks
Abhishek.

Leave a comment