Socket creation in UNIX


SOCKET CREATION
Program:
#include <sys/socket.h>
int main()
{
int sd,c;
printf("1.UDP\n2.TCP/IP\n");
printf("\n Enter your option");
scanf("%d",&c);
switch(c)
{
case 1:
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)
printf("error");
else
printf("UDP socket has been created ,SD is %d\n",sd)
break;
case 2:
sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("error");
else
printf("TCP socket has been created ,SD is %d\n",sd)
break;
}
return 0;
}
Sample i/p and o/p:
$ cc sock.c
$ ./a.out
Enter your choice
1 TCP
2 UDP
TCP socket is successfully created
UDP socket is successfully created