This article is for programmers with the following requirements:
Before you start learning socket programming make sure you already have a certain basic knowledge to network such as understand what is IP address, TCP, UDP.
Before we start our tutorial, keep in mind that the following tutorial only works for Linux OS environment. If you are using Windows, I have to apologize to you because Windows has its own socket programming and it is different from Linux even though the connection concept is the same. Well, first copy and paste the following code and run it on server and client, respectively.
Both code can be run on the same computer.
It is always easy to understand after getting the code work.
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> int main(void) { int listenfd = 0,connfd = 0; struct sockaddr_in serv_addr; char sendBuff[1025]; int numrv; listenfd = socket(AF_INET, SOCK_STREAM, 0); printf("socket retrieve success\n"); memset(&serv_addr, '0', sizeof(serv_addr)); memset(sendBuff, '0', sizeof(sendBuff)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(5000); bind(listenfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr)); if(listen(listenfd, 10) == -1){ printf("Failed to listen\n"); return -1; } while(1) { connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL); // accept awaiting request strcpy(sendBuff, "Message from server"); write(connfd, sendBuff, strlen(sendBuff)); close(connfd); sleep(1); } return 0; }
#include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <arpa/inet.h> int main(void) { int sockfd = 0,n = 0; char recvBuff[1024]; struct sockaddr_in serv_addr; memset(recvBuff, '0' ,sizeof(recvBuff)); if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0) { printf("\n Error : Could not create socket \n"); return 1; } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(5000); serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0) { printf("\n Error : Connect Failed \n"); return 1; } while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0) { recvBuff[n] = 0; if(fputs(recvBuff, stdout) == EOF) { printf("\n Error : Fputs error"); } printf("\n"); } if( n < 0) { printf("\n Read Error \n"); } return 0; }
After debugging both source files, run Socket-server.out, then run Socket-client. Attention here, never mess up with the order of executing Socket-server.out and Socket-client. Socket-server must be executed first then execute Socket-client.out and never try to break Socket-server forever loop. It means, you need to open two terminals to run each of the outputs.
When you execute Socket-cli, I guess you will get the following result:
If you see the message above, congratulations, you have success with your first step to networking programming. Otherwise, do some checking on your development environment or try to run some simple code for instance hello world.
The answer is the server and client both are software but not hardware. It means what is happening on the top is there are two different software executed. To be more precise, the server and client are two different processes with different jobs. If you are experienced with constructing a server you might find out that a server can be built on a home computer by installing a server OS. It is because server is a kind of software.
Imagine a socket as a seaport that allows a ship to unload and gather shipping, whereas socket is the place where a computer gathers and puts data into the internet.
Things that need to be initialized are listed as follows:
int socket(int domain, int type, int protocol)
Next, decide which struct needs to be used based on what domain is used above.
struct sockaddr_un { sa_family_t sun_family ; char sun_path[]; };
struct sockaddr_in { short int sin_family ; int sin_port; struct in_addr sin_addr; };
On this article, I will explain sockadd_in that showed on the code above.
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
Based on example above, server is using port 5000. You can check it by following command
sudo netstat -ntlp
Then, you will see following list
Inside red bracket, you will found 0.0.0.0:5000 and Socket-server, it means port 5000 is used and listen to any valid incoming address.
On client side, serv_addr.sin_port = htons(127.0.0.1) is declared in order to listen internal network.
The flow chart below shows the interaction between client and server. The flow chart might looks complicated but make sure you don’t lost your patient due to the following flow chart. Because every process on the flow chart is needed and it acts a very important roles on network connection.
After all setup on struct sockaddr_in is done, declare bind function. As flow chart, bind function must be declared on both server and client.
Server and client will start interact with each other after the bind function and it is the most important session. From what flow chart shows, listen, accept, connect, three function play a very important roles.
Imagine that server looks like an ATM, and only one person can be used the ATM. So, what happen if there is 2 or more people come at one time? The answer is simple, lining up and wait the front people finished using with ATM. It is exactly same as what happening in server.
Listen function acts as waiting room, asking the traffic wait on the waiting room. Accept function acts as person who asking the traffic waiting inside the waiting room to be ready for the meeting between server. Last, connect function acts as the person who want to carry out some work with server.
This article was publish on 2013/5/1 and I was still new to networking programming on this period. Maybe there is some point that I am not make clear enough, I have tried all of my best to present all my knowledge to this article. Hope you can get the good basic beginning over here. Thank you.