C Programming

C: Connect Function System Call

We cannot emphasize enough socket programming and all its use-cases. However, since it is a pervasive topic, each of its components or the functions used while doing socket programming holds a crucial value and needs to be studied separately. The connect function or system call is also an essential function used while doing socket programming in the C programming language. In this article, we will see how we can use this function in the C programming language.

Purpose of Using the Connect Function System Call in the C Programming Language:

As the name of this function says, the connect function system call in the C programming language is literally used to connect something with another. However, here we need to understand what these things are. While doing socket programming, we generally aim to provide an interaction channel between a client and a server. The server is supposed to stay in the listening mode so that the client can easily connect to it whenever it wishes to. This is where we use the connect function system call of the C programming language.

This function is used at the client-side of the code, and it attempts to connect to a server at the specified socket and address. If this connection fails, an error is generated, after which the programming terminates. However, if the connection is made successfully, the client and the server can easily exchange messages. The connect function system call is implemented within the “sys/socket.h” header file, and its general syntax are as follows:

$ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

The return type of the connect function is “int.” It returns a “0” upon success and a “-1” upon failure. Then, this function accepts three different parameters that it uses to connect the client to the server. The “sockfd” parameter is the one that is bound to the address provided within the “addr” parameter to which the client wishes to connect. Again, the “addr” parameter is used to locate the address to which the client wishes to connect, whereas the “addrlen” variable specifies the length of that address.

Example of Using the Connect Function System Call in the C Programming Language:

To understand the connect function system call usage in the C programming language, you will have to understand the interaction between a client and a server. For that, you will have to explain both the server-side and the client-side code of our example.

Explanation of the Server Side Code:

The following image depicts the server-side code of our current example:

We will not go into the depth of this code since we are mainly concerned with the connect function system call of the C programming language, which is a part of our client-side code. Here, we will briefly try to sum up the server-side code. After including all the relevant libraries or header files, we have defined all the required variables within our “main()” function. Then, we have also defined the message that we want to pass to our client as a part of this interaction.

After that, we have creatGed a socket and bound it to the desired address by using the “bind()” function. Then, we have put this server into the listening mode by using the “listen()” function followed by the “accept()” function so that this server becomes capable of accepting the requests made by the clients. Once the server accepts a connection request, a success message will be printed on the server-side terminal, after which the server will exchange messages with the client. For enabling this interaction, we have made use of the “send()” and “read()” functions of the C programming language.

Explanation of the Client-Side Code:

The following image depicts the client-side code of our current example:

After including the necessary libraries and header files on the client-side, we have declared the required variables and the message we want to send to the server as part of this interaction. Then, we used some functions to look for all the possible errors. After that, we have our “connect” function in which we attempt to connect to the server. This function contains the relevant socket and the server’s address to which the client wants to connect as its parameters. If the connection is made successfully, a message will be printed on the client-side of the terminal. Finally, the client and the server will exchange messages with each other. For enabling this interaction, we have made use of the “send()” and “read()” functions of the C programming language.

Compilation and Execution of the Program:

For compiling both the server and the client-side programs, we have to open two different terminals and execute the following commands (one command on each of the two terminals):

$ gcc connectServer.c –o connectServer

$ gcc connectClient.c –o connectClient

For executing both sides of the code, we will have to run the commands shown below on each of the respective terminals:

$ ./connectServer

$ ./connectClient

The only thing you need to be careful about while executing these codes is that you have to run the server-side code first so that the server is in the listening mode, and as soon as you run the client-side code, the client can instantly connect to the server.

In the following outputs, you can visualize that the messages for a successful connection were displayed on both terminals, i.e., the client-side terminal and the server-side terminal. After that, the client and the server exchange messages printed on the respective terminals.

What can possibly go wrong if you Miss the Connect Function System Call within the Client-Side Code?

Well, in that case, the server will stay in the listening mode forever (if a session timeout has not been specified in the server-side code), and the client will never be able to connect with that server.

Conclusion:

This guide was meant to educate you about the connect function system call in the C programming language. For that, we first talked about why this function even exists in the C programming language, followed by a thorough description of its syntax, including the parameters that it accepts and its return type. Then, we shared a pervasive example to demonstrate the connect function system call usage in the C programming language. By going through this example, you will definitely understand how easy it is to use the connect function system call while doing socket programming in the C programming language.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Page was generated in 0.060921907424927