Chinaunix首页 | 论坛 | 博客
  • 博客访问: 316978
  • 博文数量: 113
  • 博客积分: 446
  • 博客等级: 下士
  • 技术积分: 1229
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-09 16:01
个人简介

Let's go!!!!!

文章分类

全部博文(113)

文章存档

2019年(5)

2018年(4)

2017年(9)

2016年(5)

2015年(39)

2014年(6)

2013年(28)

2012年(17)

分类: LINUX

2013-01-21 18:10:28

//server.c

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define MAXBUF 1024
#define exit_err(str)  do{perror(str);exit(1);}while(0);

int main ()
{
 int sockfd, new_fd;
    socklen_t len;
    struct sockaddr_in my_addr, their_addr;
    char buf[MAXBUF + 1];
    fd_set rfds;
    struct timeval tv;
    int retval, lisnum, maxfd = 0;

    if ((sockfd = socket (PF_INET, SOCK_STREAM, 0)) == -1)
    exit_err ("socket error");

    bzero (&my_addr, sizeof (my_addr));
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons (7878);
    my_addr.sin_addr.s_addr = INADDR_ANY;


    len = sizeof (struct sockaddr);

    if (bind (sockfd, (struct sockaddr *) &my_addr, len) == -1)
      exit_err ("bind error");

    lisnum = 2;
    if (listen (sockfd, lisnum) == -1)
    exit_err ("listen error");

   while (1)
    {
      printf ("Wait accept\n");
      if ((new_fd =accept (sockfd, (struct sockaddr *) &their_addr, &len)) == -1)
        exit_err ("accept error");

      printf ("Connect success!! Start communicate!\n");
      while (1)
   {
   FD_ZERO (&rfds);
   FD_SET (0, &rfds);
   FD_SET (new_fd, &rfds);
   if (new_fd > maxfd)
    maxfd = new_fd;
   tv.tv_sec = 1;
   tv.tv_usec = 0;

   retval = select (maxfd + 1, &rfds, NULL, NULL, &tv);
   if (retval == -1)
     {
     exit_err ("select errror");
     }
   else if (retval == 0)
     {
     continue;
     }
   else
     {
       if (FD_ISSET (0, &rfds))
    {
    bzero (buf, MAXBUF + 1);
    fgets (buf, MAXBUF, stdin);
    if (!bcmp (buf, "quit", 4))
      {
        printf ("Communicate over\n");
        break;
      }
    if (send (new_fd, buf, strlen (buf) - 1, 0) == -1)
      {
        perror ("Send error");
        break;
      }
  }

     if (FD_ISSET (new_fd, &rfds))
  {
    bzero (buf, MAXBUF + 1);
    len = recv (new_fd, buf, MAXBUF, 0);
    if (len > 0)
     printf ("revieve msg:%s\n", buf);
    else
      {
        if (len < 0)
     perror ("Recieve error");
        else
     printf ("the othres exit\n");
        break;
      }
    }
      }
  }
       close (new_fd);
       printf ("Communicate to others(no->exit)\n");
       fflush (stdout);
       bzero (buf, MAXBUF + 1);
       fgets (buf, MAXBUF, stdin);
       if (!bcmp (buf, "no", 2))
    {
    printf ("Communicate Over\n");
    break;
   }
     }
   close (sockfd);
   return 0;
 }


 

//client.c

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define MAXBUF 1024
#define exit_err(str)  do{perror(str);exit(1);}while(0);

int main ()
{
  int sockfd, len;
  struct sockaddr_in dest;
  char buffer[MAXBUF + 1];
  fd_set rfds;
  struct timeval tv;
  int retval, maxfd = 0;

  if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
    exit_err ("Socket error");

  bzero (&dest, sizeof (dest));
  dest.sin_family = AF_INET;
  dest.sin_port = htons (7878);
  dest.sin_addr.s_addr = inet_addr ("127.0.0.1");

  if (connect (sockfd, (struct sockaddr *) &dest, sizeof (dest)) != 0)
    exit_err ("Connect errror");

  printf ("Conncect success!!Start communicate!\n");
  while (1)
    {
      FD_ZERO (&rfds);
      FD_SET (0, &rfds);
      FD_SET (sockfd, &rfds);
      if (sockfd > maxfd)
   maxfd = sockfd;
      tv.tv_sec = 1;
      tv.tv_usec = 0;
      retval = select (maxfd + 1, &rfds, NULL, NULL, &tv);
      if (retval == -1)
  {
      exit_err ("select error");
   }
      else if (retval == 0)
         {
      continue;
   }
      else
  {
     if (FD_ISSET (sockfd, &rfds))
     {
       bzero (buffer, MAXBUF + 1);
       len = recv (sockfd, buffer, MAXBUF, 0);
       if (len > 0)
       printf ("recieve msg:%s\n", buffer);
       else
    {
    if (len < 0)
      printf ("recieve error\n");
    else
      printf ("the other exit\n");
    break;
  }
     }
   if (FD_ISSET (0, &rfds))
      {
       bzero (buffer, MAXBUF + 1);
       fgets (buffer, MAXBUF, stdin);
       if (!bcmp (buffer, "quit", 4))
    {
    printf ("Communication over\n");
    break;
    }
       if (send (sockfd, buffer, strlen (buffer) - 1, 0) == -1)
  exit_err ("Send error");
     }
 }
    }
  close (sockfd);
  return 0;
}

 

阅读(489) | 评论(0) | 转发(0) |
0

上一篇:同步通信

下一篇:C语言中堆和栈的区别

给主人留下些什么吧!~~