server.c
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int s;
int fd;
int len;
int l1=0,l2=0;
int i;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
int sin_size;
char buf[BUFSIZ];
memset(&my_addr,0,sizeof(my_addr));
my_addr.sin_family=AF_INET;
my_addr.sin_addr.s_addr=INADDR_ANY;
my_addr.sin_port=htons(8000);
if((s=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket\n");
return 1;
}
if (bind(s,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)
{
perror("bind\n");
return 1;
}
listen(s,5);
sin_size=sizeof(struct sockaddr_in);
if((fd=accept(s,(struct sockaddr *)&remote_addr,&sin_size))<0)
{
perror("accept\n");
return 1;
}
printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr));
//len=send(fd,"Welcome to my server\n",21,0);
char rbuf[1025];
for(i=0;i<1024;i++)
{
rbuf[i]=1;
}
char sbuf[1024];
for(i=0;i<1024;i++)
{
sbuf[i]='s';
}
while(1)
{
len=recv(fd,rbuf,1024,0);
if(len==1024)
{
l1++;
}
l2++;
printf("1024 package %d in total %d\n",l1,l2);
bzero(rbuf,sizeof(rbuf));
len=send(fd,sbuf,1024,0);
}
close(fd);
close(s);
return 0;
}
client.c
#include
#include
#include
#include
#include
#include
#define BUFSIZE 1024
int main(int argc, char *argv[])
{
int s;
int len;
int i;
int l1=0,l2=0;
struct sockaddr_in remote_addr;
char buf[BUFSIZE];
memset(&remote_addr,0,sizeof(remote_addr));
remote_addr.sin_family=AF_INET;
remote_addr.sin_addr.s_addr=inet_addr("192.168.0.210");
remote_addr.sin_port=htons(8000);
if((s=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket");
return 1;
}
if (connect(s,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)
{
perror("connect");
return 1;
}
printf("connected to server\n");
char sbuf[1024];
for(i=0;i<1024;i++)
{
sbuf[i]='c';
}
char rbuf[1025];
for(i=0;i<1024;i++)
{
rbuf[i]=0;
}
while(1)
{
len=send(s,sbuf,1024,0);
len=recv(s,rbuf,1024,0);
if(len==1024)
{
l1++;
}
l2++;
printf("1024 package %d in total %d\n",l1,l2);
bzero(rbuf,sizeof(rbuf));
}
close(s);
return 0;
}
阅读(1180) | 评论(0) | 转发(0) |