LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1045|回复: 8

网络编程中的一个错误,搞不定,求助大家

[复制链接]
发表于 2004-1-3 14:28:51 | 显示全部楼层 |阅读模式
最近搞网络编程,理论是学习了不少,实践起来真不是那么回事,搞了个最简单的TCP协议的Server-Client试验了下,还有一个错误不能搞定,希望有人能指正一下

[root@BillingServer unix]# gcc -o PRG4_1 PRG4_1.C
PRG4_1.C: In function `int main()':
PRG4_1.C:55: invalid conversion from `int*' to `socklen_t*'
PRG4_1.C:69:2: warning: no newline at end of file

  1. // server.c
  2. #include <stdio.h>          /* These are the usual header files */

  3. #include <strings.h>         /* for bzero() */

  4. #include <unistd.h>          /* for close() */

  5. #include <sys/types.h>

  6. #include <sys/socket.h>

  7. #include <netinet/in.h>

  8. #include <arpa/inet.h>

  9. #include <netinet/tcp.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <netdb.h>


  13. #define PORT 1234   /* Port that will be opened */

  14. #define BACKLOG 1   /* Number of allowed connections */



  15. main()

  16. {

  17.         int listenfd, connectfd; /* socket descriptors */

  18.         struct sockaddr_in server; /* server's address information */

  19.         struct sockaddr_in client; /* client's address information */

  20.         int sin_size;



  21.         /* Create TCP socket */

  22.         if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

  23.         {

  24.         /* handle exception */

  25.                 perror("Creating socket failed.");

  26.                 exit(1);

  27.         }



  28.         /* set socket can be reused */

  29.         int opt = SO_REUSEADDR;

  30.         setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));



  31.         bzero(&server,sizeof(server));   /* fill server with 0s */

  32.         server.sin_family=AF_INET;

  33.         server.sin_port=htons(PORT);

  34.         server.sin_addr.s_addr = htonl (INADDR_ANY);

  35.         if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1)

  36.         {

  37.                 /* handle exception */

  38.                 perror("Bind error.");

  39.                 exit(1);

  40.         }   



  41.         if(listen(listenfd,BACKLOG) == -1)

  42.         {  

  43.                 /* calls listen() */

  44.                 perror("listen() error\n");

  45.                 exit(1);

  46.         }



  47.         sin_size = sizeof(struct sockaddr_in);

  48.        
  49.         if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1)

  50.         {

  51.                 /* calls accept() */

  52.                 perror("accept() error\n");

  53.                 exit(1);

  54.         }



  55.         /* prints client's IP */

  56.         printf("You got a connection from %s\n",inet_ntoa(client.sin_addr) );

  57.         /* send to the client welcome message */

  58.         send(connectfd,"Welcome to my server.\n",22,0);

  59.        

  60.         close(connectfd); /*  close connectfd */

  61.         close(listenfd);   /* close listenfd */         

  62. }
复制代码
 楼主| 发表于 2004-1-3 14:29:56 | 显示全部楼层
  1. //client.c

  2. #include <stdio.h>

  3. #include <unistd.h>

  4. #include <strings.h>

  5. #include <sys/types.h>

  6. #include <sys/socket.h>

  7. #include <netinet/in.h>

  8. #include <netdb.h>        /* netbd.h is needed for struct hostent  */

  9. #include <netinet/ip.h>
  10. #include <netinet/tcp.h>
  11. #include <stdlib.h>
  12. #include <errno.h>



  13. #define PORT 1234   /* Open Port on Remote Host */

  14. #define MAXDATASIZE 100   /* Max number of bytes of data */



  15. int main(int argc, char *argv[])

  16. {

  17.         int fd, numbytes;   /* files descriptors */

  18.         char buf[MAXDATASIZE];  /* buf will store received text */

  19.         struct hostent *he;     /* structure that will get information about remote host */

  20.         struct sockaddr_in server;  /* server's address information */



  21.         if (argc !=2)

  22.         {      

  23.                 /* this is used because our program will need one argument (IP) */

  24.                 printf("Usage: %s <IP Address>\n",argv[0]);

  25.                 exit(1);

  26.         }



  27.         if ((he=gethostbyname(argv[1]))==NULL)

  28.         {

  29.                 /* calls gethostbyname() */

  30.                 printf("gethostbyname() error\n");

  31.                 exit(1);

  32.         }



  33.         if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1)

  34.         {  

  35.                 /* calls socket() */

  36.                 printf("socket() error\n");

  37.                 exit(1);

  38.         }



  39.         bzero(&server,sizeof(server));

  40.         server.sin_family = AF_INET;

  41.         server.sin_port = htons(PORT); /* htons() is needed again */

  42.         server.sin_addr = *((in_addr *)he->h_addr);  /*he->h_addr passes "*he"'s info to "h_addr" */



  43.         if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1)

  44.         {

  45.                 /* calls connect() */

  46.                 printf("connect() error\n");

  47.                 exit(1);

  48.         }



  49.         if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1)

  50.         {  

  51.                 /* calls recv() */

  52.                 printf("recv() error\n");

  53.                 exit(1);

  54.         }



  55.         buf[numbytes]='\0';

  56.         printf("Server Message: %s\n",buf); /* it prints server's welcome message  */



  57.         close(fd);   /* close fd */

  58. }
复制代码
 楼主| 发表于 2004-1-3 14:32:24 | 显示全部楼层
出现的错误是
[root@BillingServer unix]# gcc -o PRG4_1 PRG4_1.C
PRG4_1.C: In function `int main()':
PRG4_1.C:55: invalid conversion from `int*' to `socklen_t*'
PRG4_1.C:69:2: warning: no newline at end of file
 楼主| 发表于 2004-1-3 15:21:52 | 显示全部楼层
发表于 2004-1-3 21:11:30 | 显示全部楼层
你试验试验,把你的头文件的顺序调换一下,看看行不?
我以前好像遇到你这样的问题
发表于 2004-1-4 03:41:39 | 显示全部楼层
我编译了一下你的程序,没遇到你说的错误。
不过client程序的:
server.sin_addr = *((in_addr *)he->h_addr); /*he->h_addr passes "*he"'s info to "h_addr" */
应该改成:
if(inet_aton(he->h_addr, &server.sin_addr) == 0){
  fprintf(stderr, "inet_aton error\n");
  exit(-1);
}
发表于 2004-1-4 03:46:54 | 显示全部楼层
看一下man gethostbyname

  1. struct hostent {
  2.   char    *h_name;        /* official name of host */
  3.   char    **h_aliases;    /* alias list */
  4.   int     h_addrtype;     /* host address type */
  5.   int     h_length;       /* length of address */
  6.   char    **h_addr_list;  /* list of addresses */
  7. }
  8. #define h_addr  h_addr_list[0]  /* for backward compatibility */
复制代码

h_addr的类型是char *,不能直接赋值给sin_addr
 楼主| 发表于 2004-1-4 11:22:40 | 显示全部楼层
我问过其他地方,有个老兄的解释是
PRG4_1.C:55: invalid conversion from `int*' to `socklen_t*'

把变量类型改改,从int改为socklen_t
后来我靠这样编译过去了,也没有出现版主提到的客户端错误呢,不知为何
发表于 2004-1-4 15:07:56 | 显示全部楼层
占位贴!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表