user10343741
user10343741

Reputation: 21

TCP Connection in python

Ok guys, I can't understand this code:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

What does:

socket.AF_INET
socket.SOCK_STREAM

do? I really read everything abount them, but I can't understand what dows they do, could you please explain me, them in simple words?Thanks for read, have a great day!

Upvotes: 0

Views: 6043

Answers (3)

Léopold Houdin
Léopold Houdin

Reputation: 1525

TL;DR

socket.AF_INET = The type of socket address

socket.SOCK_STREAM = The type of socket

Explanation

Whenever you provide socket.AF_INET to your server you instruct it to listen to an IPv4 address, and to your client to connect to an IPv4 address. This will work. Same for IPv6. However mixing them up doesn't.

That would be the same me waiting for you to talk to me on StackOverflow while you send me messages by email. We are not looking at the same spot, so we won't be able to communicate.

Same for socket.SOCK_STREAM (and the other values). The difference lies in the fact that this tells python's socket how we are going to communicate. socket.SOCK_STREAM will be TCP, while socket.SOCK_DGRAM will be UDP.

Let's come back to our "real world" example and let's imagine we agreed on communicating by email. I could expect either one email from you (explaining me everything you have to tell) or several (with a part of what you have to say in each email). That's the same as TCP vs UDP.

References

Well, I guess you have read both:

Potentially:

Also, I guess:

Super long explanation but mostly testing

So. If after all that you don't understand. Let's try:

# server.py

import socket

s = socket.socket()
s.bind(('localhost', 5050))
s.listen(5)

while True:
    (clientsocket, address) = s.accept()
    print 'client connected'

And:

# client.py

import socket

s = socket.socket(socket.AF_INET)
s.connect(('127.0.0.1', 5050))

print "Yeah! I'm connected :)"

So far, everything as in the how to.

We launch our server:

$ python server.py

And then we launch our client:

$ python client.py
Yeah! I'm connected :)

Everything works fine. That's good.

Now, lets change our client:

# client.py

import socket

s = socket.socket(socket.AF_INET6)
s.connect(('127.0.0.1', 5050))

print "Yeah! I'm connected :)"

And relaunch our new client:

$ python client.py
Traceback (most recent call last):
  File "client.py", line 4, in <module>
    s.connect(('127.0.0.1', 5050))
  File "/.../lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 65] No route to host

Aie! Everything breaks!

But what happens? 127.0.0.1:5050 is an IPv4 address, hence the socket module tells us it's not happy about what we are doing! Indeed, we said our connection will be using an IPv6 address but we are providing it with an IPv4 address.

Okay... But if I use the same address but in IPv6, will it work? Let's try by changing our client (you could check out this SO answer for the equivalent of 127.0.0.1 for IPv6):

# client.py

import socket

s = socket.socket(socket.AF_INET6)
s.connect(('::1', 5050))

print "Yeah! I'm connected :)"

and our server:

# server.py

import socket

s = socket.socket(socket.AF_INET6)
s.bind(('::1', 5050))
s.listen(5)

while True:
    (clientsocket, address) = s.accept()
    print 'client connected'

We relaunch our server and our client:

$ python client.py
Yeah! I'm connected :)

Success!

The same procedure could be used to understand/test the socket.SOCK_STREAM parameter (but I think my answer is already long enough).

Hope this helped :).

Upvotes: 3

David Montgomery
David Montgomery

Reputation: 473

AF_INET is well described in there. It is basically the method you are using for sending data over to the other socket. SOCK_STREAM basically describes that you are sending using TCP and essentially describes rules for the endpoint to which you are sending and recieving data (IP adress and port number).

But since you seem confused over these terms I'd suggest just just think of them as specifications on how you are going to transmit data between your two socket endpoints.

Upvotes: 0

C.Nivs
C.Nivs

Reputation: 13106

socket.STREAM is the kind of socket you want. In this case, you are looking to stream bytes to and from the host you want to connect to, rather than just issue a one-time call. This means that the socket will listen for bytes until it receives an empty byte b'', at which point it will terminate the connection (because the stream is complete).

I would imagine you aren't too worried about the type of socket, so low-level understanding here isn't paramount, nor could I give you a particularly satisfactory answer to that, either.

socket.AF_INET is the AddressFamily, ipv4 or ipv6. This tells sockets what kind of address to expect. You will most likely use ipv4, so (host, port) tuple will work just fine.

Upvotes: 0

Related Questions