Socket Listen Source code in C Programming Network lab

To verify whether the socket is in listener mode and to find
the binded port number.It will be used in the chat application programming.
Source code in C programming Algorithm CS1305 Network Lab Socket Listen
STEP 1: Start the program.
STEP 2: Declare the variables fd, b, l.
STEP 3: Declare the family, type, protocol, queue length and
port number for the socket.
STEP 4: The socket is created and the field descriptor value is displayed.
STEP 5: The socket is binded at the specified port.
STEP 6: The listen function is used.
STEP 7: If the returned value is positive then the server is said
to be in listener mode.
STEP 8: Stop the program.

PROGRAM Free source code in C CS1305 Network Lab Socket Listen
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define PORT 450
#define quelen 5
int main( )
{
int fd,b,l;
struct sockaddr_in myaddr;
fd=socket(AF_INET,SOCK_STREAM,0);
if(fd==-1)
printf(“Socket Creation Error”);
else
{
printf(“Socket Created”);
printf(“Socket Field Description Value is%d\n”,fd);
}
myaddr.sin_family=AF_INET;
myaddr.sin_port=PORT;
myaddr.sin_addr.s_addr=INADDR_ANY;
b=bind(fd,(struct sockaddr*) &myaddr,Sizeof
(struct sockaddr));
if(b---1)
printf(“Socket Binding Error”);
else
{
printf(“socket is binded at port %d\n”,PORT);
}
l=listen(fd,quelen);
if(l==-1)
printf(“Socket listen Error”);
else
printf(“Server is in listener mode”);
return 0;
}
Output CS1305 Network Lab Socket Listen
Socket Created.
Socket Field Descriptor Value is 3
socket is binded at port 450
Server is in listener mode
RESULT Thus the program for performing socket listen has
been created and verified.

Related post