Socket Binding Source code in C Programming Network CS1305

To create and bind a socket by using the socket function. The socket bind function is used in the chat application programming.
Algorithm Free Source code in C C++ Programming
STEP 1: Start the program.
STEP 2: Declare the variables fd, b and the port number.
STEP 3: Declare the family, type and protocol for the socket function.
STEP 4: Using the object initialize the port, address and family.
STEP 5: If fd value is positive then the socket is created else not created.
STEP 6: Use the bind function.
STEP 7: If the bind value is negative then binding error occurs.
STEP 8: otherwise the port number is printed.
STEP 9: Stop the program.

PROGRAM CS1305 Network Lab Socket Binding
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define PORT 2000
int main( )
{
int fd,b;
struct sockaddr_in myaddr;
fd=socket(AF_INET,SOCK_STREAM,0);
if(fd==-1)
printf(“Socket Creation Error”);
else
{
printf(“Socket is Created”);
printf(“Socket Field Descriptor Value is : %d”,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”,PORT);
}
return 0;
}
Output CS1305 Network Lab Socket Binding
Socket is Created
Socket Field Descriptor Value is 3
Socket is binded at port 2000
RESULT : Thus the program for binding the socket has
been created and verified in C source code Network lab CS1305.

Related post