Socket Accept Source in C programming Network lab

To verify the socket accept function by using other socket functions. It is used in the chat application program for connection oriented.
Free Source code Algorithm in C programming Socket Accept.
STEP 1: Start the program.
STEP 2: Declare the variables port number and queue length.
STEP 3: Using the object declare the family , address and port.
STEP 4: The socket is binded at a particular port.
STEP 5: The port number is displayed.
STEP 6: If the socket is in the listener mode then the accept
function is executed.
STEP 7: The server is to be waiting to connect the client.
STEP 8: Stop the program.

Free Source code Program CS1305 Network Lab

#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<arpa/inet.h>
#define PORT 2000
#define quelen 5
int main( )
{
int fd;
struct sockaddr_in server,client;
fd=socket(AF_INET,SOCK_STREAM,0);
if(fd==-1)
printf(“Socket Creation Error\n”);
else
{
printf(“Socket Creation\n”);
printf(“Socket created descriptor value is %d\n”,fd);
server.sin_family=AF_INET;
server.sin_port=PORT;
server.sin_addr.s_addr=INADDR_ANY;
if(bind(fd,(struct sockaddr*)&server,sizeof(struct sockaddr_in))!=-1)
printf(“Socket is binded at port %d”,PORT);
else
{
printf(“Socket Binding Error”);
}
if(listen(fd,quelen)==-1)
printf(“Listen Error”);
else
{
printf(“Server is in Listener Mode”);
printf(“Server is waiting to connect to the client”);
}
while(1)
{
if(accept(fd,struct sockaddr*)&client,sizeof(struct sockaddr_in*))=-1)
printf(“Accept Error”);
else
{
printf(“Server is in Accept Mode”);
printf(“Connection mode in with client %s”,inet_ntoa(client.sin_addr));
}}}
Output CS1305 Network Lab Socket Accept
Socket Created
Socket Field Descriptor Value is 3
Socket is binded at port 2000
Server is in Listener mode
Server is waiting to connect to the client.
RESULT : Thus the program for performing socket accept has
been created and verified. It will be used in the chat application connection oriented programming.

Related post