Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do Sockets Work in C?

Tags:

I am a bit confused about socket programming in C.

You create a socket, bind it to an interface and an IP address and get it to listen. I found a couple of web resources on that, and understood it fine. In particular, I found an article Network programming under Unix systems to be very informative.

What confuses me is the timing of data arriving on the socket.

How can you tell when packets arrive, and how big the packet is, do you have to do all the heavy lifting yourself?

My basic assumption here is that packets can be of variable length, so once binary data starts appearing down the socket, how do you begin to construct packets from that?

like image 378
kaybenleroll Avatar asked Sep 08 '08 00:09

kaybenleroll


People also ask

How do sockets work?

Sockets are commonly used for client and server interaction. Typical system configuration places the server on one machine, with the clients on other machines. The clients connect to the server, exchange information, and then disconnect. A socket has a typical flow of events.

What is socket type in C?

Type C (electrical socket/electrical plug) The Type C plug (also called the Europlug) has two round pins. The pins are 4 to 4.8 mm wide with centers that are spaced 19 mm apart; the plug fits any socket that conforms to these dimensions.

What is socket () bind () listen () accept () and connect ()?

The steps involved in establishing a TCP socket on the server side are as follows: Create a socket with the socket() function; Bind the socket to an address using the bind() function; Listen for connections with the listen() function; Accept a connection with the accept() function system call.

How is TCP socket implemented?

TCP sockets are used for communication between a server and a client process. The server's code runs first, which opens a port and listens for incoming connection requests from clients. Once a client connects to the same (server) port, the client or server may send a message.


1 Answers

Short answer is that you have to do all the heavy lifting yourself. You can be notified that there is data available to be read, but you won't know how many bytes are available. In most IP protocols that use variable length packets, there will be a header with a known fixed length prepended to the packet. This header will contain the length of the packet. You read the header, get the length of the packet, then read the packet. You repeat this pattern (read header, then read packet) until communication is complete.

When reading data from a socket, you request a certain number of bytes. The read call may block until the requested number of bytes are read, but it can return fewer bytes than what was requested. When this happens, you simply retry the read, requesting the remaining bytes.

Here's a typical C function for reading a set number of bytes from a socket:

/* buffer points to memory block that is bigger than the number of bytes to be read */ /* socket is open socket that is connected to a sender */ /* bytesToRead is the number of bytes expected from the sender */ /* bytesRead is a pointer to a integer variable that will hold the number of bytes */ /*           actually received from the sender. */ /* The function returns either the number of bytes read, */ /*                             0 if the socket was closed by the sender, and */ /*                            -1 if an error occurred while reading from the socket */ int readBytes(int socket, char *buffer, int bytesToRead, int *bytesRead) {     *bytesRead = 0;     while(*bytesRead < bytesToRead)     {         int ret = read(socket, buffer + *bytesRead, bytesToRead - *bytesRead);         if(ret <= 0)         {            /* either connection was closed or an error occurred */            return ret;         }         else         {            *bytesRead += ret;         }     }     return *bytesRead; } 
like image 198
dfjacobs Avatar answered Oct 04 '22 23:10

dfjacobs