• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Hideto

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              2 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(17)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            server.c:
             1 #include <stdio.h>
             2 #include <stdlib.h>
             3 #include <strings.h>
             4 #include <unistd.h>
             5 #include <sys/types.h>
             6 #include <sys/socket.h>
             7 #include <netinet/in.h>
             8 #include <arpa/inet.h>
             9 
            10 #define PORT 8888
            11 #define BACKLOG 10
            12 
            13 int main(int argc, char *argv[])
            14 {
            15   int listenfd, connectfd;
            16   struct sockaddr_in server;
            17   struct sockaddr_in client;
            18   int sin_size;
            19 
            20   if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
            21     perror("Creating socket failed.");
            22     exit(0);
            23   }
            24 
            25   int opt = SO_REUSEADDR;
            26   setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
            27 
            28   bzero(&server, sizeof(server));
            29   server.sin_family = AF_INET;
            30   server.sin_port = htons(PORT);
            31   server.sin_addr.s_addr = htonl(INADDR_ANY);
            32 
            33   if (bind(listenfd, (struct sockaddr *)&server,
            34       sizeof(struct sockaddr)) == -1){
            35     perror("Bind error!");
            36     exit(0);
            37   }
            38 
            39   if (listen(listenfd,BACKLOG) == -1){
            40     perror("Listen error!");
            41     exit(0);
            42   }
            43 
            44   while(1){
            45     sin_size = sizeof(struct sockaddr_in);
            46 
            47     if((connectfd = accept(listenfd, (struct sockaddr *)&client, &sin_size)) == -1) {
            48       perror("Accept error!");
            49       break;
            50     }
            51 
            52     printf("You got a connection from %s\n", inet_ntoa(client.sin_addr));
            53 
            54     send(connectfd, "Fucking client!\n"160);
            55 
            56     close(connectfd);
            57   }
            58 
            59   close(listenfd);
            60   exit(0);
            61 }
            client.c:
             1 #include <stdio.h>
             2 #include <unistd.h>
             3 #include <strings.h>
             4 #include <stdlib.h>
             5 #include <sys/types.h>
             6 #include <sys/socket.h>
             7 #include <netinet/in.h>
             8 #include <netdb.h>
             9 
            10 #define PORT 8888
            11 #define MAXDATASIZE 100
            12 
            13 int main(int argc, char *argv[])
            14 {
            15   int fd, numbytes;
            16   char buf[MAXDATASIZE];
            17   struct hostent *he;
            18   struct sockaddr_in server;
            19 
            20   if (argc != 2) {
            21     printf("Usage: %s <IP Address>\n",argv[0]);
            22     exit(0);
            23   } 
            24 
            25   if((he = gethostbyname(argv[1])) == NULL) {
            26     printf("gethostbyname() error\n");
            27     exit(0);
            28   } 
            29 
            30   if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            31     printf("socket() error\n");
            32     exit(0);
            33   } 
            34 
            35   bzero(&server, sizeof(server));
            36   server.sin_family = AF_INET;
            37   server.sin_port = htons(PORT);
            38   server.sin_addr = *((struct in_addr *)he->h_addr);
            39 
            40   if(connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
            41     close(fd);
            42     printf("Connect error!");
            43     exit(0);
            44   }
            45 
            46   if((numbytes = recv(fd, buf, MAXDATASIZE, 0)) == -1) {  /* calls recv() */
            47     printf("Recv error!");
            48     exit(0);
            49   }
            50 
            51   buf[numbytes] = '\0';
            52   printf("Server Message: %s \n", buf);
            53 
            54   close(fd);
            55 }
            編譯:
            1 gcc -o server server.c
            2 gcc -o client client.c
            運行:
            1 # server端
            2 ./server
            3 # client端
            4 ./client 127.0.0.1
            posted on 2008-08-06 14:59 Hideto 閱讀(378) 評論(0)  編輯 收藏 引用
            久久久艹| 久久综合九色综合久99| 久久偷看各类wc女厕嘘嘘| 色偷偷88888欧美精品久久久| 久久久久久久人妻无码中文字幕爆 | 狠狠精品久久久无码中文字幕| 久久久久国产精品人妻| 亚洲国产成人精品无码久久久久久综合| 久久精品国产亚洲欧美| 久久综合伊人77777| 久久综合香蕉国产蜜臀AV| 国产毛片久久久久久国产毛片 | 亚洲国产成人久久一区WWW| 亚洲中文字幕无码一久久区| 国产免费久久精品99久久| 狠狠色婷婷久久综合频道日韩| 久久免费视频网站| 久久亚洲精品成人av无码网站| 久久久久18| 久久香蕉一级毛片| 精品久久久久久久久午夜福利 | 久久久久久国产精品无码下载| 国产精品九九久久免费视频| 潮喷大喷水系列无码久久精品| 久久热这里只有精品在线观看| 久久精品成人免费国产片小草| 日韩av无码久久精品免费| 久久精品国产亚洲AV影院| 亚洲国产精品狼友中文久久久| 一级做a爱片久久毛片| 久久99热狠狠色精品一区| 丰满少妇人妻久久久久久| 精品乱码久久久久久久| 久久人人爽人人人人片av| 久久人人添人人爽添人人片牛牛| 久久久久综合中文字幕 | 精品久久久久久亚洲精品 | 国产精品美女久久久久AV福利 | 久久综合九色综合久99| 久久久久久久久久久免费精品| 久久精品国产欧美日韩|