我写的代码,编译无错,但是目标没有实现,请高手帮我看看指导下。

云端的人 发布于 2012/03/19 17:44
阅读 186
收藏 0

我写的代码 能实现连接了,但是服务端无法打印出客户端的信息,还有无法实现多个客户端和服务端的连接,求高手看完代码给点指教,不吝啬的话写个代码跟我参考下,谢谢!

服务端=========

#include<sys/types.h>
#include<ctype.h>
#include<string.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<ctype.h>
#include<errno.h>
#include<sys/time.h>
#include<stdio.h>
#include<strings.h>
#include<sys/select.h>
#include<stdlib.h>
#include<pthread.h>
#include<time.h>


struct pack
{
  char ID[32];
  char IP[32];
  char name[32];
  char password[32];
  time_t ltime;
  struct pack *next;
};
struct client_info
{
  char ID[32];
  char name[32];
  char password[32];
  time_t t;
  struct client_info *next;
};
struct pack *Head=NULL;

void c_printf();

int main()
{
 int ret;
 pthread_t idx;
 struct sockaddr_in s_servaddr,s_cliaddr;
 int request_sock,new_sock;
 int nfound,fd,maxfd,readnumber;
 uint32_t addrlen;
 fd_set rset,set;
 char buf[1024];
 //user_line("./data/user_data");
 request_sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//开始socket编程
  if(request_sock < 0 )
  {
   printf("socket error");
   return -1;
  }
 memset(&s_servaddr,0,sizeof(s_servaddr));
 s_servaddr.sin_family = AF_INET;//协议族 IPV4
 s_servaddr.sin_addr.s_addr = INADDR_ANY;//经由任何可绑定的地址 IPV4
 s_servaddr.sin_port = htons(8088);//端口
 printf("bind...\n");
        //绑定计算机IP地址和端口信息到socket上
    if(bind(request_sock,(struct sockaddr *)&s_servaddr,sizeof(s_servaddr))<0)
    {
       printf("bind error");
       return -1;
    }
 printf("listen...\n");
       //已绑定的套接口开始监听 10为规定内核为套接口排队的最大连接数
 if(listen(request_sock,10) < 0)
 {
  printf("listen error");
  return -1;
 }
 FD_ZERO(&set);//对指定文件描述符进行清空初始化,否则出问题
 FD_SET(request_sock,&set);//在文件描述符集合中增加一个新的文件描述符
 maxfd = request_sock;
 //ret = pthread_create(&idx,NULL,(void *)thread_live,NULL);
 //if(ret != 0)//成功返回0
      // {
    //  printf("creat pthread error!\n");
  //      exit(1);
    // }
 while(1)
 {
  rset = set;
  printf("select now...\n");
  sleep(3);
                 //确定一个和多个套接口的状态(可读)
  nfound = select(maxfd + 1,&rset,(fd_set *)0,(fd_set *)0,NULL);
  printf("%d\n",nfound);
   if(nfound < 0)//不修改任何描述符集
   {
    printf("select error");
    return -1;
   }
   else if(nfound == 0)//描述符被清0
   {
    printf(".");fflush(stdout);
    continue;
   }
   if(FD_ISSET(request_sock,&rset))//检测request有无被改变
   {
    printf("request_sock\n");
    addrlen = sizeof(s_cliaddr);
    printf("accept...\n");
                               //连接。。
                                 addrlen=sizeof(struct sockaddr_in);
   new_sock = accept(request_sock,(struct sockaddr*)&s_cliaddr,&addrlen);                   
    if(new_sock < 0)//错误时返回-1
    {
     printf("accept error");
     return -1;
    }
  printf("connection form host %s,port %d,socket%d\r\n",inet_ntoa(s_cliaddr.sin_addr),ntohs(s_cliaddr.sin_port),new_sock);
    FD_SET(new_sock,&set);
    if(new_sock > maxfd)
            maxfd = new_sock;
                            inet_ntoa(s_cliaddr.sin_addr);
                              FD_CLR(request_sock,&rset);//把负责监听的新我文件描述符清空
                            nfound--;
                        }
                      /* if((readnumber=read(new_sock,buf,1024))<0)
                       {
                             printf("read error");
                             exit(-1);
                      }

               
   //FD_CLR(request_sock,&rset);//把负责监听新连接的文件描述符清零
   //nfound--;
                       FD_CLR(request_sock,&rset);//循环下一个
                 }
                close(new_sock);
                return 0;
               
}*/                 struct client_info *ptr;
                    struct pack *str;
                 for(fd=0;fd<=maxfd&&nfound>0;fd++)
                 {
                     if(FD_ISSET(fd,&rset))
                     {
                        nfound--;
                       ptr=(struct client_info *)malloc(sizeof(struct client_info));
                        memset(ptr,0,sizeof(struct client_info));
                        printf("读包头\n");
                        if((readnumber=read(fd,ptr,sizeof(struct client_info)))<0)
                        {
                          printf("read error");
                           return -1;
                         }
                      /*  if(readnumber==0)
                        {
                          printf("read over\n");
                          //FD_CLR(fd,&set);
                          close(fd);
                          continue;
                         }*/
                     }
                      str=(struct pack *)malloc(sizeof(struct pack));
                     memset(str,0,sizeof(struct pack));
                //    printf("把所读到的信息存入链表里面\n");
                     strcpy(str->ID,ptr->ID);
                     strcpy(str->name,ptr->name);
                     strcpy(str->password,ptr->password);
                     str->ltime=ptr->t;
                      str->next=Head;
                      Head=str;  
                  }
              }
            c_printf();
          //  close(new_sock);
            return 0;
}

void c_printf()
{
   printf("显示在线人员信息\n");
    struct pack *p;
     p=Head;
     printf("客户ID 客户昵称 客户密码 连接时间\n");
    while(p!=NULL)
    {
      printf("%3s,%7s,%8s,%d\n",p->ID,p->name,p->password,p->ltime);
       p=p->next;
    }
}
客户端=============

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<errno.h>
#include<sys/time.h>
#include<ctype.h>
#include<sys/select.h>
#include<pthread.h>
#include<time.h>


struct client_info
{
  char ID[32];
  char name[32];
   char password[32];
   time_t t;
   struct client_info *next;
};
static int connect_server(struct sockaddr_in c_servaddr)
{
  int chose;
  int ret;
  char buf[100]="hello word";
  int c_sockfd;
  struct hostent *server;
  if(inet_pton(AF_INET,"192.168.103.224",&c_servaddr.sin_addr.s_addr)<=0)
  {
    printf("inet_pton error");
   return -1;
  }
 if((c_sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
  {
    printf("c_sockfd error");
    return -1;
  }
  c_servaddr.sin_family=AF_INET;
  c_servaddr.sin_port=htons(8088);
     struct client_info *ptr;
      ptr=(struct client_info *)malloc(sizeof(struct client_info));
       printf("请输入ID号\n");
       scanf("%s",ptr->ID);
       getchar();
       printf("请输入昵称\n");
       scanf("%s",ptr->name);
       getchar();
       printf("请输入密码\n");
       scanf("%s",ptr->password);
      ptr-> t=time(NULL);
        getchar();
   printf("连接中发送中........\n");

   ret=connect(c_sockfd,(struct sockaddr *)&c_servaddr,sizeof(c_servaddr));
  /* if(ret<0)
    {
      printf("connect error");
      return -1;
    }
    if(write(c_sockfd,buf,strlen(buf))==-1)
    {
       printf("write error");
       return -1;
     }*/
 if(ret<0)
 {
   printf("connect error\n");
   return -1;
 }
 else
 { 
    if(write(c_sockfd,ptr,sizeof(struct client_info))<0)
    {
       printf("write error");
       return -1;
    }
  }
 
 fflush(stdout);
}
int main()
{
  struct sockaddr_in c_servaddr;
  connect_server(c_servaddr);
  return 0;
}

 

加载中
0
Jeky
Jeky
C++只能看明白的Javaer路过,帮顶,没人来帮帮新手么?
云端的人
云端的人
感谢帮顶
返回顶部
顶部