linux下的socket程序的小疑问

许鑫 发布于 2011/06/11 13:29
阅读 349
收藏 1
之前弄的2个linux的socket编程,一个服务端一个客户端的。只是当时作为一个socket练习做的。

现在想做qos通信方面的实验。所以想添加 客户端到服务端传输数据的响应时间。来解决。传输1m和10m或者1000m的数据时 时间的不同。(gettimeofday???)我调用这个函数如下
---------------------------

#include <sys/time.h>
int gettimeofday(struct tim *tv, struct timezone *tz);
int settimeofday(const struct tim *tv , const struct timezone *tz);
结构tim的定义为:
strut tim {long tv_sec; long tv_usec; };


gettimeofday( &start, NULL );
foo();
gettimeofday( &end, NULL );
timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
timeuse /= 1000000;
--------------------------------------------------
但是发现几个问题
1,添加进去之后报错
2,开始时间和结束时间 我要添加在那里?

对socket很不熟悉。

麻烦高手帮忙

另外我的环境是fedora。我想在比较他们在qos使用的情况和不使用的情况下比较。这个qos应该怎么设置呢?

---------------------------------------------------






 

下附prgrame:

cilent.c

#include <netdb.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define EHCO_PORT 12345
#define MAX_CLIENT_NUM 10
#define SIZE 101


int main(int argc,char *argv[])
{
 int socketfd;
 struct sockaddr_in sa;
 char buf[SIZE];
 int i;
 if(argc<2){
  printf("Usage : %s <IP_ADDR>\n",argv[0]);
  exit(0);
 }
 socketfd=socket(AF_INET,SOCK_STREAM,0);
 if(socketfd<0){
  perror("Create stream socket");
  exit(1);
 }
 
 sa.sin_family = AF_INET;
 sa.sin_addr.s_addr=htonl(inet_network(argv[1]));
 sa.sin_port=htons(EHCO_PORT);
 if(connect(socketfd,(struct sockaddr *)&sa,sizeof(sa))<0){
  perror("\nconnecting server stream socket");
  exit(1);
 }
 printf("Connected.\n");
 
  //client begin to write:
  for(;;)
  {if(fgets(buf,SIZE,stdin)==NULL) exit(0);
  if(write(socketfd,buf,strlen(buf))<0){
  perror("sending stream message");
  exit(1);
  }
   
  //receive data from the server:
  bzero(buf,SIZE);
 int n;
  if((n = recv(socketfd,buf,100,0 )) > 0)
  { 
  printf("receive the data which is revised by the server :\n ");
  write(STDOUT_FILENO,buf, n);
  bzero(buf,n);
  }  
  }
 close(socketfd);
 printf("Connection closed.\n\n");
 exit(0);
}

 

---------------------------------------------------------------------------------------------------------------

sever.c

 #include <netdb.h>
 #include <sys/socket.h>
 #include <errno.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
 #define EHCO_PORT 12345
 #define MAX_CLIENT_NUM 10
 
 int main()
 {
  int socketfd;
  socketfd = socket(AF_INET, SOCK_STREAM, 0);
   
  if(socketfd == -1)
  {
  printf("errno=%d\n ", errno);
  exit(1);
  }
  else
  {
  printf("socket create successfully \n");
  }
 
  struct sockaddr_in sa;
  bzero(&sa, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(EHCO_PORT);


  sa.sin_addr.s_addr = htons(INADDR_ANY);
  bzero(&(sa.sin_zero), 8);
 
  if(bind(socketfd, (struct sockaddr *)&sa, sizeof(sa))!= 0)
  {
  printf("bind failed ");
  printf("errno=%d\n ", errno);
  exit(1);
  }
  else
  {
  printf("bind successfully\n");
  }
 
  //listen
  if(listen(socketfd ,MAX_CLIENT_NUM) != 0)
  {
  printf("listen error \n ");
  exit(1);
  }
  else
  {
  printf("listen successfully \n");
  }
 
  int clientfd;
  struct sockaddr_in clientAdd;
  char buff[101],rbuff[101];
  socklen_t len = sizeof(clientAdd);
  int closing =0;
  int i=0,j=0;
  while( closing == 0 && (clientfd = accept(socketfd, (struct sockaddr *)&clientAdd, &len)) >0 )
  {
   
  int n;
  while((n = recv(clientfd,buff, 100,0 )) > 0)
  {
   
  printf("number of receive bytes = %d\n ", n);
  write(STDOUT_FILENO, buff, n);
   
  //for(i=0;i<n;i++) printf("%d",buff[i]);

  buff[n-1]='\0';  
  if(strcmp(buff,"quit") == 0)
  {
  break;
  }
  else if(strcmp(buff,"close") == 0)
  {
  //server closing
  closing = 1;
  printf("server is closing \n");
  break;

  }

  //revise data
  i=0;
  for(j=n-1;j>=0;j--)
  {
  rbuff[i++]=buff[j];
   
  }
  send(clientfd, rbuff, n, 0);
  bzero(buff,n);
  }
   
  } close(clientfd);
  close(socketfd); 
  return 0;
  }

加载中
返回顶部
顶部