Ocket-Server.c: Int Void Int Struct Char Int
Ocket-Server.c: Int Void Int Struct Char Int
#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;
char sendBuff[1025];
int numrv;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL); // accept awaiting
request
close(connfd);
sleep(1);
}
return 0;
}
Socket-client.c
Hide Shrink Copy Code
#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;
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( 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.
Understand Sockets
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.
Configure Socket
At the beginning, a socket function needs to be declared to get the socket descriptor.
Next, decide which struct needs to be used based on what domain is used above.
AF_UNIX AF_INET
Hide Copy Code Hide Copy Code
struct sockaddr_un struct sockaddr_in
{ {
sa_family_t sun_family ; short int sin_family ;
char sun_path[]; int sin_port;
}; struct in_addr sin_addr;
};
In this article, I will explain sockadd_in that showed in the code above.
Based on the example above, server is using port 5000. You can check it by the following
command:
The flow chart below shows the interaction between client and server. The flow chart might look
complicated but make sure you don’t lose your patience due to the following flow chart. Because
every process on the flow chart is needed and it acts as a very important role 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.
bind function
Server and client will start interacting with each other after the bind function and it is the most
important session. From what flow chart shows, listen, accept, connect, three functions play
very important roles.
Imagine that server looks like an ATM, and only one person can be used the ATM. So, what
happens if there are 2 or more people that come at one time? The answer is simple, lining up and
wait for the front people to finish using with ATM. It is exactly the same as what is happening in
the server.
Listen function acts as a waiting room, asking the traffic wait on the waiting
room. Accept function acts as the person who is asking the traffic waiting inside the waiting
room to be ready for the meeting between server. Last, connect function acts as the person
who wants to carry out some work with the server.
listen function
accept function
connect function
Finally, after the request is accepted, what should server and client do is send and read data. It is
the most simple part in this entire article. read function is used to read the buffer data
and write function is used to send the data. That’s all.
read function
write function
Personal Comment
This article was published on 2013/5/1 and I was still new to networking programming at that
time. Maybe there is some point that I am not making clear enough. I have tried my best to
present all my knowledge to this article. Hope you can get the good basic beginning over here.
Thank you!