EmbLogic's Blog

create socket programme using client and server

CLIENT:-

head    1.1;
access;
symbols;
locks
root:1.1; strict;
comment    @ * @;

1.1
date    2014.06.17.10.36.05;    author root;    state Exp;
branches;
next    ;

desc
@creat reqclient for socket.
@

1.1
log
@Initial revision
@
text
@#include”header.c”
int main()
{
char data1[3]={5,4,’+’};
char sum;
int len,server_fd,count;
struct sockaddr_in client_addr;
client_addr.sin_family=AF_INET;
client_addr.sin_port=htons(2224);
client_addr.sin_addr.s_addr=inet_addr(“127.0.0.1″);
/*struct data data1;
data1.a=2;
data1.b=4;
data1.opr=’+’;*/
int client_fd=socket(AF_INET,SOCK_STREAM,0);
if(client_fd<0)
{
perror(“\n socket is failure”);
exit(-1);
}
int fd=connect(client_fd,(struct sockaddr *)&client_addr,sizeof(struct sockaddr_in));
if(fd<0)
{
perror(“\n connect is failure”);
exit(-1);
}
len=sizeof(struct data);
count=write(client_fd,&data1,3);
printf(“no.of bytes writtern %d\n”,count);
read(client_fd,&sum,1);
printf(“sum= %d\n”,sum);
return 0;

}
@

SERVER:-

locks
root:1.1; strict;
comment    @ * @;

1.1
date    2014.06.17.10.34.40;    author root;    state Exp;
branches;
next    ;

desc
@server for socket impl.
@

1.1
log
@Initial revision
@
text
@#include”header.c”
int main()
{
char buff[3];
socklen_t len;
len=sizeof(struct sockaddr_in);
struct data data1;
int server_fd,server_fd1,true=1,ret,count,count1;
char sum;
struct sockaddr_in server_addr,client_addr,reqclient_addr,server_addr1,client_addr1;
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(2224);
server_addr.sin_addr.s_addr=inet_addr(“127.0.0.1″);
server_fd=socket(AF_INET,SOCK_STREAM,0);
server_fd1=socket(AF_INET,SOCK_STREAM,0);
if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))==-1)
{
perror(“\n setsockopt is failure”);
exit(-1);
}
if(setsockopt(server_fd1,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int))==-1)
{
perror(“\n setsockopt is failure”);
exit(-1);
}

ret=bind(server_fd,(const struct sockaddr *)&server_addr,sizeof(struct sockaddr_in));
if(ret<0)
{
perror(“\n bind is failure”);
exit(-1);
}
printf(“gfhgkjj,hkh,”);
ret=listen(server_fd,5);
if(ret<0)
{
perror(“\n listen is failure”);
exit(-1);
}

int fd=accept(server_fd,( struct sockaddr *)&client_addr,&len);
if(fd<0)
{
perror(” accept is failure\n”);
exit(-1);
}

//        len=sizeof(struct data);
count=read(fd,buff,3);
printf(“VALUE OF 1ST VARIABLE OF BUFF IN SERVER —>>[%d]\n”,buff[0]);
printf(“VALUE OF 2nd VARIABLE OF BUFF IN SERVER —>>[%d]\n”,buff[1]);
printf(“VALUE OF 3rd VARIABLE OF BUFF IN SERVER —>>[%c]\n”,buff[2]);
//printf(“\n count=%d,data1.a=%d,data1.b=%d,data1.opr=%c\n”,count,data1.a,data1.b,data1.opr);

server_addr1.sin_family=AF_INET;
server_addr1.sin_port=htons(6666);
server_addr1.sin_addr.s_addr=inet_addr(“127.0.0.1″);

ret=bind(server_fd1,(const struct sockaddr *)&server_addr1,sizeof(struct sockaddr_in));
if(ret<0)
{
perror(“\n bind is failure”);
exit(-1);
}
ret=listen(server_fd1,5);
if(ret<0)
{
perror(“\n listen is failure”);
exit(-1);
}
int fd1=accept(server_fd1,( struct sockaddr *)&reqclient_addr,&len);
if(fd<0)
{
perror(“\n accept is failure”);
exit(-1);
}
len=sizeof(struct data);
count=write(fd1,buff,3);
printf(“no.of bytes writtern %d\n”,count);
count1=read(fd1,&sum,1);
printf(“count is =%d,sum=%d”,count1,sum);
count=write(fd,&sum,1);
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>