Learn Cyber Security, How to InfoSec, Tutorials, and more about CyberSecurity!

Tuesday, 27 October 2015

Sockets





Maybe you heard before or not about sockets. Today I am gonna talk a little about this subject

What is a socket?
You may have heard some UNIX hacker state, "Jeez, everything in UNIX is a file!". What that person may have been talking about is the fact that when UNIX programs do any sort of I/O (input/output), they do it by reading or writing to a file descriptor. A file descriptor is simply an integer associated with an open file. But, that file can be a network connection, a FIFO, a pipe, a terminal, a real on-the-disk file, or just about anything else. Everything in UNIX is a file! So when you want to communicate with another program over the Internet you're gonna do it through a file descriptor.
You make a call to the socket() system routine. It returns the socket descriptor, and you communicate through it using the specialized send() and recv() socket calls.

Question: "If it's a file descriptor why can't I just use the normal read() and write() calls to communicate through the socket?"
Answer: You ca, but send() and recv() offer much greater control over your data transmission.

There are all kinds of sockets:
- Internet Sockets
- UNIX Sockets
- X.25 Sockets
- etc.

There are a lot of Internet sockets, but I am gonna talk about only two in this post:
- Stream Sockets (SOCK_STREAM)
- Datagram Sockets (SOCK_DGRAM) or Connectionless Sockets

Stream sockets are reliable two-way connected communication streams. If you output two items into the socket in the order "1, 2", they will arrive in the order "1, 2" at the opposite end. They will also be error-free.

Question: "What uses stream sockets?"
Answer: Well, you may have heard of the telnet application; it uses stream sockets. All the characters you type need to arrive in the same order you type them. Also, web browsers use the HTTP protocol which uses stream sockets to get pages.

Question: "How do stream sockets achieve this high level of data transmission quality?"
Answer: They use a protocol called "The Transmission Control Protocol" otherwise knows as TCP. TCP makes sure your data arrives sequentially and error-free. You may have heard "TCP" before as the better half of "TCP/IP" where IP (Internet Protocol) deals primarily with Internet routing and is not generally responsible for data integrity.

Datagram sockets: if you send a datagram, it may arrive. It may arrive out of order. If it arrives, the data whithin the packet will be error-free.
Datagram sockets also use IP for routing, but they don't use TCP; they use the "User Datagram Protocol" (UDP).

Question: Why are Datagram sockets connectionless?
Answer: Well, basically, it's because you don't have to maintain an open connection as you do with stream sockets. You just build a packet, slap an IP header on it with destination information, and send it out. No connection needed. They are generally used either when a TCP stack is unavailable or when a few dropped packets here and there don't mean the end of the Universe.
Sample applications: tftp(trivial file transfer protocol), dhcpcd(a DHCP client), multiplayer games, streaming audio, video conferencing, etc.
The TFTP and similar programs have their own protocol on top of UDP. For example, the TFTP protocol says that for each packet that gets sent, the recipient has to send back a packet that says, "I got it!"(an ACK packet). If the sender of the original packet gets no reply in a limited time, he'll re-transmit the packet until he finally gets an ACK. The acknowledgment procedure is very important when implementing reliable SOCK_DGRAM applications.
For unreliable applications like games, audio or video, you just ignore the dropped packets, or perhaps try to cleverly compensate for them.

Question: Why would you use an ureliable underlying protocol?
It's way faster to fire-and-forget than it is to keep track of what has arrived safely and make sure it's in order and all that. If you're sending chat messages, TCP is great; if you're sending 40 positional updates per second of the players in the world, maybe it doesn't matter so much if one or two get dropped, and UDP is a good choice.

References:
1. Beej's Guide to Network Programming - Copyright © 2015 Brian “Beej Jorgensen” Hall

1 comment:

  1. Sockets ~ Learn-And-Crack >>>>> Download Now

    >>>>> Download Full

    Sockets ~ Learn-And-Crack >>>>> Download LINK

    >>>>> Download Now

    Sockets ~ Learn-And-Crack >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete

Popular Posts

Recent Posts

Powered by Blogger.