Demonstrate Socket For Datagram Client Unix C Program

Important Funciton and Keyword For socket Data gram For client
Initialize the variable
int sockfd,nbytes,addrlen;
Check Using If function, struct sockaddr_in *, &myaddr,sizeof(myaddr is Greater Than zero
if(bind(sockfd,(struct sockaddr_in *) &myaddr,sizeof(myaddr))<0)
struct sockaddr_in myaddr,servaddr;
Source Code Programming For Socket Data gram Client
 #include<stdio.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
main(int argc,char * argv[])
{
int sockfd,nbytes,addrlen;
char buff[512];
struct sockaddr_in myaddr,servaddr;
if(argc!=2)
{
printf("usage port: client");
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("socket failed: client");
}
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(INADDR_ANY);
myaddr.sin_addr.s_addr=htonl(0l);
if(bind(sockfd,(struct sockaddr_in *) &myaddr,sizeof(myaddr))<0)
{
perror("bind failed: client");
}
if((nbytes=read(0,buff,512))<0)
{
perror("read failed: client");
}
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(atoi(argv[1]));
PAGE NO.
52
52
SWARNA BHARATHI COLLEGE OF ENGINEERING MCA III SEM
servaddr.sin_addr.s_addr=htonl(0l);
if(sendto(sockfd,buff,nbytes,0,(struct sockaddr *)
&servaddr,sizeof(servaddr))!=nbytes)
{
perror("send failed: client");
}
if(write(1,buff,nbytes)!=nbytes)
{
perror("write failed: client");
}
}socket for datagram client Unix C Program

Related post