Linux上的一个简单的多人聊天室(2)

/* initialize server address */
 bzero(&server, sizeof(server));
 server.sin_family = AF_INET;
 inet_pton(AF_INET, "127.0.0.1", &server.sin_addr);
 server.sin_port = htons(SEVR_PORT);

perror("init");
 
 return (0);
}

char *i_get_time()
{
 time_t time_now;
 time(&time_now);

return(ctime(&time_now));
}
int i_lseek(int fd, off_t size, int position)
{
 if (-1 == lseek(fd, size, position))
 {
  perror("seek error");
  exit(1);
 }
 return(0);
}
int i_saveto_chat(struct msg *pmsg)

 struct chat_history hstr;


 bzero(&hstr, HSTR_LEN);
 count = count + 1;
 hstr.count =count;
 hstr.from = pmsg->id_from;
 hstr.to = pmsg->id_to;
 strncpy(hstr.content, pmsg->content, CNTNT_LEN);
 strncpy(hstr.time, i_get_time(), 25);

i_lseek(mainfd, 0, SEEK_END);

i_write(mainfd, &hstr, HSTR_LEN);

return(0);
}

int i_print_history(int len, int i)
{
 struct chat_history chat_reader;
 int j;
 int position;
 
 bzero(&chat_reader, HSTR_LEN);
 if (i != 0)
 {
  position = len*i*HSTR_LEN;
  i_lseek(mainfd, position, SEEK_END);
 }
 else
 {
  position = len*i*HSTR_LEN;

i_lseek(mainfd, HSTR_LEN, SEEK_SET);
 }
  
 for (j = 1; j <= len; j++)
 {
  
  i_read(mainfd, &chat_reader, HSTR_LEN);
  printf("\n#item%d:id%dto id%d \n", j,
   chat_reader.from, chat_reader.to);
  i_print(chat_reader.content, CNTNT_LEN);
  printf("\n  Time:%s\n", chat_reader.time);
 }

return(0);
}

#endif

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwxwsg.html