|
#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>
#define servport 3333
#define maxlen 1024
int main()
{
int sockfd,clientfd,socklen;
int cnt;
char buf[maxlen];
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);
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\n",
inet_ntoa(remoteaddr.sin_addr));
printf("enter childprogress!");
fflush(stdout);
memset(buf,0,maxlen);
if((fork())==0)
{
printf("enterd fork");
fflush(stdout);
do
{
printf("enter do!");
fflush(stdout);
if((cnt=recv(clientfd,buf,sizeof(buf),0))<0)
{
perror(" server read data error!");
close(clientfd); exit(7);
}
else if(cnt>0)
{
printf("continue receive!\n");
}
}
while(cnt!=0);
printf("My received is finished!");
fflush(stdout);
printf("sending data....!");
fflush(stdout);
if(send(clientfd,"Hello data is accpetted!", 30,0)<0)
{
perror("send error!");
close(clientfd);
exit(0);
}
}
else close(clientfd);
}
return 0;
}
以上是server端程序!
这里是client端程序!
#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
/*long tiemnow()
{
struct timeval now;
long lpassed;
gettimeofday(&now,0);
lpassed=now.tv_sec*1000000+now.tv_usec;
return lpassed;
}*/
int main (int argc,char *argv[])
{
int sockfd,recvbytes;
/* int n;*/
char buf[maxlen];
struct sockaddr_in myaddr;
long spendtime,sendtime,recvtime;
struct timeval now;
if (argc<3)
{
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);
}
memset(buf,0,sizeof(buf));
if (connect(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr))<0)
{
perror("Connect error!");
exit(2);
}
/*long spendtime,sendtime,recvtime;
struct timeval now;*/
gettimeofday(&now,0);
memset(buf,4,sizeof(buf));
sendtime=now.tv_sec*1000000+now.tv_usec;
/*memset(buf,1,sizeof(buf));*/
printf("\t\tsendtime=%ld",sendtime);
if (send(sockfd,"im connect you!",30,0)<0)
{
perror("send error!");
exit(0);
}
if ((recvbytes=recv(sockfd,buf,maxlen,0))<0)
{
perror("receive error!");
exit(1);
}
buf[recvbytes]='\0';
gettimeofday(&now,0);
recvtime=now.tv_sec*1000000+now.tv_usec;
spendtime=recvtime-sendtime;
printf("Received=%s Spend time=%ld",buf,spendtime);
close(sockfd);
return 0;
}
我在运行程序的时候当运行到server程序的 do循环时候程序就不能 正常运行了!清高手给予指点! 请详细给出错误所在! |
|