亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 2937 | 回復(fù): 17
打印 上一主題 下一主題

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2004-01-03 14:37 |只看該作者 |倒序?yàn)g覽
最近搞網(wǎng)絡(luò)編程,理論是學(xué)習(xí)了不少,實(shí)踐起來真不是那么回事,搞了個最簡單的TCP協(xié)議的Server-Client試驗(yàn)了下,還有一個錯誤不能搞定,希望有人能指正一下

我的系統(tǒng)是Redhat 9.0+oracle 9.2.0.1
剛裝的系統(tǒng),應(yīng)該很干凈

[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. if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1)

  49. {

  50. /* calls accept() */

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

  52. exit(1);

  53. }



  54. /* prints client's IP */

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

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

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



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

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

  60. }
復(fù)制代碼

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2004-01-03 14:38 |只看該作者

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家

  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. }
復(fù)制代碼

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
3 [報(bào)告]
發(fā)表于 2004-01-03 14:47 |只看該作者

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家


int sin_size;
改為
socklen_t sin_size;
編譯錯誤信息說得已經(jīng)很清楚了。
還有以后把代碼排好版,有代碼編輯功能的。

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2004-01-03 14:54 |只看該作者

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家

感謝感謝,可能還是理論不清晰,實(shí)踐中遇到很多問題

socklen_t是個什么類型的數(shù)據(jù)?能否解釋一二,多謝多謝

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
5 [報(bào)告]
發(fā)表于 2004-01-03 15:24 |只看該作者

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家

原帖由 "yufeng8552" 發(fā)表:
感謝感謝,可能還是理論不清晰,實(shí)踐中遇到很多問題

socklen_t是個什么類型的數(shù)據(jù)?能否解釋一二,多謝多謝

socklen_t一般是這個樣子。
  1. typedef unsigned int      socklen_t;
復(fù)制代碼

你去/usr/include/sys/socket.h看看就知道了。

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2004-01-03 16:13 |只看該作者

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家

好象我用int  也可以的哦是不是系統(tǒng)不一樣的啊

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2004-01-03 16:17 |只看該作者

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家

// 重復(fù)性服務(wù)器,該程序等候客戶連接,一旦連接則顯示客戶的地址,
// 然后接收來自該客戶的信息(字符串).每當(dāng)收到一個字符串,則顯示該字符串,
// 并將字符串反轉(zhuǎn),再將反轉(zhuǎn)的字符發(fā)回客戶.
// 之后,繼續(xù)等待接收該客戶的信息直至該客戶關(guān)閉連接.
// 完成與該客戶交互后,服務(wù)器開始等待下一客戶,并重復(fù)上述過程.

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

  2. #include <string.h>;          /* for bzero() */

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

  4. #include <sys/types.h>;

  5. #include <sys/socket.h>;

  6. #include <netinet/in.h>;

  7. #include <arpa/inet.h>;

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

  9. #define BACKLOG 5   /* Number of allowed connections */

  10. #define MAXDATASIZE 1000  



  11. void process_cli(int connectfd, sockaddr_in client);



  12. main()

  13. {

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

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

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

  17.         socklen_t sin_size;



  18.         /* Create TCP socket  */

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

  20.         {

  21.                 /* handle exception */

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

  23.                 exit(1);

  24.         }



  25.         int opt = SO_REUSEADDR;

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



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

  28.         server.sin_family=AF_INET;

  29.         server.sin_port=htons(PORT);

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

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

  32.         {

  33.                 /* handle exception */

  34.                 perror("Bind error.");

  35.                 exit(1);

  36.         }



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

  38.         {  

  39.                 /* calls listen() */

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

  41.                 exit(1);

  42.         }



  43.         sin_size=sizeof(struct sockaddr_in);

  44.         while (1)

  45.         {

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

  47.             {

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

  49.                         exit(1);

  50.             }   

  51.                 process_cli(connectfd, client);

  52.         }



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

  54. }



  55. void process_cli(int connectfd, sockaddr_in client)

  56. {

  57.         int num;

  58.         char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE];



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

  60.         /* prints client's IP */



  61.         while (num = recv(connectfd, recvbuf, MAXDATASIZE,0))

  62.         {

  63.                 recvbuf[num] = '\0';

  64.                 printf("Received client message: %s",recvbuf);



  65.                 for (int i = 0; i < num - 1; i++)

  66.                 {

  67.                         sendbuf[i] = recvbuf[num - i -2];

  68.             }

  69.                 sendbuf[num - 1] = '\0';



  70.                 send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */

  71.         }



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

  73. }
復(fù)制代碼


編譯出錯
PRG6_3.C: In function `int main()':
PRG6_3.C:27: `exit' undeclared (first use this function)
PRG6_3.C:27: (Each undeclared identifier is reported only once for each
   function it appears in.)
PRG6_3.C:116:2: warning: no newline at end of file

論壇徽章:
1
榮譽(yù)版主
日期:2011-11-23 16:44:17
8 [報(bào)告]
發(fā)表于 2004-01-03 16:18 |只看該作者

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家

[quote]原帖由 "mills"]好象我用int  也可以的哦是不是系統(tǒng)不一樣的啊[/quote 發(fā)表:

是能編譯通過,可是有警告,你的難道連警告都沒有?

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2004-01-03 18:32 |只看該作者

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家

加多個頭函數(shù)
#include<stdlib.h>;

最好用sin_size=sizeof(he) 或sin_size=sizeof(server),可能好一點(diǎn)

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2004-01-03 19:58 |只看該作者

網(wǎng)絡(luò)編程中的一個錯誤,搞不定,求助大家

36    }
    37    bzero(&server,sizeof(server));
    38    server.sin_family = AF_INET;
    39    server.sin_port = htons(PORT); /* htons() is needed again */
    40    server.sin_addr = *((in_addr *)he->;h_addr);
    41  /*he->;h_addr passes "*he"'s info to "h_addr" */
    42    if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1) {
    43  /* calls connect() */
    44    printf("connect() error\n";
    45    exit(1);
    46    }


# gcc -c socketD.c
socketD.c: In function `main':
socketD.c:40: `in_addr' undeclared (first use in this function)
socketD.c:40: (Each undeclared identifier is reported only once
socketD.c:40: for each function it appears in.)
socketD.c:40: parse error before ')' token

我的不知道怎么錯?
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP