EmbLogic's Blog

client server programme using socket

#include
#include
//#include
#include
#include
int main()
{
int sockfd,true,aret,bret;
char ch[5];
struct sockaddr_in server_add,client_add;
socklen_t len;

server_add.sin_family = AF_INET;
server_add.sin_port=htons(7770);
server_add.sin_addr.s_addr = inet_addr(“127.0.0.1″);
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))==-1)
{
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf(“fail in openning socket”);
}
}

printf(“sockfd =%d\n”,sockfd);

bret = bind(sockfd,(struct sockaddr *)&server_add,sizeof(struct sockaddr));
printf(“bind return %d\n”,bret);
if(bret==-1)
{
printf(“error in binding”);
}

listen(sockfd,3);

len = sizeof(client_add);
//printf(“len %d\n”,*len);
aret = accept(sockfd,(struct sockaddr *)&client_add,&len);
printf(“accept return %d\n”,aret);
read(aret,ch,5);
printf(“SERVER:%s\n”,ch);
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>