Write A C-Program To Demonstrate Socket For Stream Client.

Demonstrate Socket For Stream Client.
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<errno.h>
#include<netinet/in.h>
#define SERV_HOST_ADDR "192.168.1.42"
main(int argc,char *argv[])
{
int sockfd,nbytes,nbyte,newfd;
char buff[512];
struct sockaddr_in seraddr;
if(argc!=3)
{
printf("usage port: client serverport,servaddr\n");
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket failed");
}
seraddr.sin_family=AF_INET;
seraddr.sin_port=htons(3490);
seraddr.sin_addr.s_addr=inet_addr(SERV_HOST_ADDR);
if(connect(sockfd,(struct sockaddr *) &seraddr,sizeof(seraddr))<0)
{
perror("connection failed");
}
while((nbytes=read(0,buff,512))!=0)
{
if(nbytes<0)
{
perror("read fail");
PAGE NO.
49
49
SWARNA BHARATHI COLLEGE OF ENGINEERING MCA III SEM
}
if(write(sockfd,buff,nbytes)!=nbytes)
{
perror("write failed");
}
}
if((nbyte=read(sockfd,buff,512))!=nbytes)
{
perror("read failed");
}
if(write(newfd,buff,nbytes)!= nbytes)
{
perror("write failed");
}
}Write a c-program to demonstrate socket for stream client.

Related post