+ -
当前位置:首页 → 问答吧 → 求助,最简单的socket程序。

求助,最简单的socket程序。

时间:2010-08-15

来源:互联网

最近开始接触socket程序。

拿unp第5章的那个tcp cli/serv实例来实验一下。

结果问题出现了,启动客户端程序都时候总是error。

另外,被几个问题搞混了。

1、AF_UNIX和AF_INET到底有什么区别?我是在本机上实验的,那么这个参数该选什么呢?
是不是服务器设置成什么,客户端也要设置成什么吗?

2、还有就是 sockadd 的问题,到底是用 sockadd还是 sockadd_in。
如果用AF_UNIX的话,sockadd里面的地址该写成什么呢。

问题太多了,并且感觉很复杂啊。

作者: pandaiam   发布时间: 2010-08-15

  1. /**
  2. * Tcp Server program, It is a simple example only.
  3. * zhengsh 200520602061 2
  4. * when client connect to server, send a welcome message and timestamp in server.
  5. */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/socket.h>
  10. #include <unistd.h>
  11. #include <sys/types.h>
  12. #include <netinet/in.h>
  13. #include <time.h>

  14. #define SERVER_PORT 20000 // define the defualt connect port id
  15. #define LENGTH_OF_LISTEN_QUEUE 10 //length of listen queue in server
  16. #define BUFFER_SIZE 255
  17. #define WELCOME_MESSAGE "welcome to connect the server. \n "


  18. int main(int argc, char **argv)
  19. {
  20.       int servfd,clifd;
  21.       struct sockaddr_in servaddr,cliaddr;

  22.       if ((servfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
  23.       {
  24.              printf("create socket error!\n");
  25.              exit(1);
  26.       }
  27.       bzero(&servaddr,sizeof(servaddr));
  28.       servaddr.sin_family = AF_INET;
  29.       servaddr.sin_port = htons(SERVER_PORT);
  30.       servaddr.sin_addr.s_addr = htons(INADDR_ANY);

  31.       if (bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
  32.       {
  33.              printf("bind to port %d failure!\n",SERVER_PORT);
  34.              exit(1);
  35.       }

  36.       if (listen(servfd,LENGTH_OF_LISTEN_QUEUE) < 0)
  37.       {
  38.              printf("call listen failure!\n");
  39.              exit(1);
  40.       }

  41.       while (1)
  42.       {//server loop will nerver exit unless any body kill the process
  43.              char buf[BUFFER_SIZE];
  44.              long timestamp;
  45.              socklen_t length = sizeof(cliaddr);
  46.              clifd = accept(servfd,(struct sockaddr*)&cliaddr,&length);
  47.              if (clifd < 0)
  48.              {
  49.                     printf("error comes when call accept!\n");
  50.                     break;
  51.              }
  52.              strcpy(buf,WELCOME_MESSAGE);
  53.              //inet_ntop(INET_ADDRSTRLEN,cliaddr.sin_addr,buf,BUFFER_SIZE);
  54.             
  55.              //printf("from client,IP:%s,Port:%d\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port));
  56.              timestamp = time(NULL);
  57.              strcat(buf,"timestamp in server:");
  58.              strcat(buf,ctime(&timestamp));
  59.              send(clifd,buf,strlen(buf) + 1,0);
  60.              close(clifd);         
  61.             
  62.       }//exit
  63.       close(servfd);
  64.       return 0;
  65. }
复制代码
  1. /* Tcp client program, It is a simple example only.
  2. * zhengsh 200520602061 2
  3. * connect to server, and echo a message from server.
  4. */


  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/socket.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <netinet/in.h>

  12. #define SERVER_PORT 20000 // define the defualt connect port id
  13. #define CLIENT_PORT ((20001+rand())%65536) // define the defualt client port as a random port

  14. #define BUFFER_SIZE 255
  15. #define REUQEST_MESSAGE "welcome to connect the server.\n"

  16. void usage(char *name)
  17. {
  18.       printf("usage: %s IpAddr\n",name);
  19. }

  20. int main(int argc, char **argv)
  21. {   
  22.       int servfd,clifd,length = 0;
  23.       struct sockaddr_in servaddr,cliaddr;
  24.       socklen_t socklen = sizeof(servaddr);
  25.       char buf[BUFFER_SIZE];
  26.       
  27.       if (argc < 2)
  28.       {
  29.              usage(argv[0]);
  30.              exit(1);
  31.       }
  32.       
  33.       if ((clifd = socket(AF_INET,SOCK_STREAM,0)) < 0)
  34.       {
  35.              printf("create socket error!\n");
  36.              exit(1);
  37.       }
  38.       srand(time(NULL));//initialize random generator
  39.       bzero(&cliaddr,sizeof(cliaddr));
  40.       cliaddr.sin_family = AF_INET;
  41.       cliaddr.sin_port = htons(CLIENT_PORT);
  42.       cliaddr.sin_addr.s_addr = htons(INADDR_ANY);

  43.       bzero(&servaddr,sizeof(servaddr));
  44.       servaddr.sin_family = AF_INET;
  45.       inet_aton(argv[1],&servaddr.sin_addr);
  46.       servaddr.sin_port = htons(SERVER_PORT);
  47.       //servaddr.sin_addr.s_addr = htons(INADDR_ANY);

  48.       if (bind(clifd,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0)
  49.       {
  50.              printf("bind to port %d failure!\n",CLIENT_PORT);
  51.              exit(1);
  52.       }

  53.       if (connect(clifd,(struct sockaddr*)&servaddr, socklen) < 0)
  54.       {
  55.              printf("can't connect to %s!\n",argv[1]);
  56.              exit(1);
  57.       }
  58.       
  59.       length = recv(clifd,buf,BUFFER_SIZE,0);
  60.       if (length < 0)
  61.       {
  62.              printf("error comes when recieve data from server %s!",argv[1]);
  63.              exit(1);
  64.       }
  65.       printf("from server %s :\t%s \n",argv[1],buf);

  66.       close(clifd);
  67.       return 0;
  68. }
复制代码

作者: evaspring   发布时间: 2010-08-15

楼上不要只铁代码啊。

解答一下问题。

作者: pandaiam   发布时间: 2010-08-15