|
楼主 |
发表于 2003-7-9 08:57:11
|
显示全部楼层
#include<unistd.h>
#include<fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include<unistd.h>
#include<arpa/inet.h>
#define servport 3333
#define maxlen 1024
int main (int argc,char *argv[])
{
int sockfd;
int n;
char buf[maxlen];
struct sockaddr_in myaddr;
long spendtime,sendtime,recvtime;
struct timeval now; int fp;
if (argc<2)
{
printf("Please enter the server's hostname and port !");
exit(1);
}
if ((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket error");
exit(1);
}
memset(&myaddr,0,sizeof(struct sockaddr_in));
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(servport);
if(inet_pton(AF_INET,argv[1],&myaddr.sin_addr)<=0)
{
perror("IP error!");
exit(1);
}
if (connect(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr))<0)
{
perror("Connect error!");
exit(2);
}
gettimeofday(&now,0);
memset(buf,0,sizeof(buf));
if((fp=open("/mnt/linuxsource.htm",O_RDONLY))<0)
{
printf("Can not open file!");
exit(0);
}
sendtime=now.tv_sec*1000000+now.tv_usec;
/*memset(buf,1,sizeof(buf));*/
while((n=read(fp,buf,sizeof(buf)))>0)
write(sockfd,buf,n);
close(sockfd);
close(fp);
printf("file tranfer finish!");
/* if ((recvbytes=recv(sockfd,buf,maxlen,0))<0) {
perror("receive error!");
exit(1);
}*/
gettimeofday(&now,0);
recvtime=now.tv_sec*1000000+now.tv_usec;
spendtime=recvtime-sendtime;
printf("\t\t\t spend time=%ld\n",spendtime);
fflush(stdout);
return 1;
}
以上部分是客户端的代码!#include<fcntl.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
#include<sys/wait.h>
#include<errno.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<setjmp.h>
#include<netdb.h>
#define servport 3333
#define maxlen 1024
int main()
{
int sockfd,clientfd,socklen;
int fp;
/* FILE *fp;*/
char buf[maxlen];
long nread;
struct sockaddr_in myaddr;
struct sockaddr_in remoteaddr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket error!");
exit(0);
}
memset(&myaddr,0,sizeof(struct sockaddr_in));
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(servport);
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if((bind(sockfd,(struct sockaddr*)&myaddr,sizeof(myaddr)))<0)
{
perror("bind error!");
exit(0);
}
if(listen(sockfd,10)<0)
{
perror("listen error!");
exit(0);
}
socklen=sizeof(struct sockaddr);
if((fp=open("/root/111.htm",O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
{
perror("file open error!");
exit(0);
}
while(1)
{
if((clientfd=accept(sockfd,(struct sockaddr*)&remoteaddr,&socklen))<0)
{
perror("accept error!");
continue;
}
printf("connect finish!\n");
printf("received a conncetion from %s", inet_ntoa(remoteaddr.sin_addr));
printf("enter childprogress!");
fflush(stdout);
memset(buf,0,maxlen);
if((fork())==0)
{
printf("enterd fork");
fflush(stdout);
nread=0;
while((nread=read(clientfd,buf,sizeof(buf)))>0)
write(fp,buf,nread);
close(fp);
printf("&Icirc;&Auml;&frac14;&thorn;&frac12;&Oacute;&Ecirc;&Otilde;&Iacute;ê±&Iuml;");
fflush(stdout);
printf("sending data....!");
fflush(stdout);
if(send(clientfd,"Hello data is accpetted!",30,0)<0)
{
perror("send error!");
exit(0);
}
close(clientfd);
}
else close(clientfd);
}
return 1;
}
这是服务器的! |
|