• <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>

            旭++

            張旭的C++學習筆記
            posts - 5, comments - 8, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            socket編程學習筆記(1)

            Posted on 2007-12-01 00:22 張旭 閱讀(634) 評論(0)  編輯 收藏 引用
                  今天重新把socket編程中的每一個函數(shù)的功能和說明都仔細的看了一遍,也有了更深一層的理解。在經(jīng)歷一次面試的失利之后,我覺得最大的問題就出在沒有對學過的知識力求甚解,導致對概念不清楚。所以在看這部分知識的時候,倍加用心的研究。
                  socket編程中主要用到一個結構 sockaddr和以下幾個函數(shù),socket(),bind(),connect(),listen(),accept(),send(),recv()。
                  bind函數(shù)是使用listen函數(shù)的必要條件,如果只需要connect的話,bind是不需要的。bind的作用是將套接字和機器的端口聯(lián)系起來。
                  connect()是建立兩臺電腦連接的必要函數(shù),首先要有一個已經(jīng)建立好的socket套接字,還有你要連接的目標主機的ip地址以及端口號等信息。
                  accept(),當connect的時候,主機需要通過accept來接受本次連接,accept需要對套接字進行bind()。當accept成功之后,函數(shù)會返回一個新的套接字描述符,通過新的描述符可以真對新的套接字進行send和recv操作。
                  listen()是對端口的監(jiān)聽,你可以設定一個列隊的數(shù)量上線,這樣,在多個訪問請求到達的時候,可以排成隊伍,超過列隊上限的訪問將被拒絕。

            簡單的服務器
              這個服務器所做的全部工作是在流式連接上發(fā)送字符串 "Hello, World!\n"。你要測試這個程序的話,可以在一臺機器上運行該程序,然后 在另外一機器上登陸:
               $ telnet remotehostname 3490
            remotehostname 是該程序運行的機器的名字。
            服務器代碼:
             1#include <stdio.h>
             2  #include <stdlib.h>
             3  #include <errno.h>
             4  #include <string.h>
             5  #include <sys/types.h>
             6  #include <netinet/in.h>
             7  #include <sys/socket.h>
             8  #include <sys/wait.h>
             9#define MYPORT 3490 /*定義用戶連接端口*/ 
            10#define BACKLOG 10 /*多少等待連接控制*/ 
            11main() 
            12   
            13   int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd 
            14*/
             
            15   struct sockaddr_in my_addr; /* my address information */ 
            16   struct sockaddr_in their_addr; /* connector's address information */ 
            17   int sin_size;
            18if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1
            19   perror("socket"); 
            20   exit(1); 
            21   }
             
            22
            23my_addr.sin_family = AF_INET; /* host byte order */ 
            24   my_addr.sin_port = htons(MYPORT); /* short, network byte order */ 
            25   my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ 
            26   bzero(&(my_addr.sin_zero),; /* zero the rest of the struct */ 
            27
            28if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct 
            29sockaddr))== -1
            30   perror("bind"); 
            31   exit(1); 
            32   }
             
            33if (listen(sockfd, BACKLOG) == -1
            34   perror("listen"); 
            35   exit(1); 
            36   }
             
            37
            38while(1/* main accept() loop */ 
            39   sin_size = sizeof(struct sockaddr_in); 
            40   if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \ 
            41   &sin_size)) == -1
            42   perror("accept"); 
            43   continue
            44   }
             
            45   printf("server: got connection from %s\n", \ 
            46   inet_ntoa(their_addr.sin_addr)); 
            47   if (!fork()) /* this is the child process */ 
            48   if (send(new_fd, "Hello, world!\n"140== -1
            49   perror("send"); 
            50   close(new_fd); 
            51   exit(0); 
            52   }
             
            53   close(new_fd); /* parent doesn't need this */ 
            54while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */ 
            55   }
             
            56   }
             
            57
            58

            簡單的客戶程序
              這個程序比服務器還簡單。這個程序的所有工作是通過 3490 端口連接到命令行中指定的主機,然后得到服務器發(fā)送的字符串。
            客戶代碼:
            #include <stdio.h>
              #include 
            <stdlib.h>
              #include 
            <errno.h>
              #include 
            <string.h>
              #include 
            <sys/types.h>
              #include 
            <netinet/in.h>
              #include 
            <sys/socket.h>
              #include 
            <sys/wait.h>
            #define PORT 3490 /* 客戶機連接遠程主機的端口 */ 
            #define MAXDATASIZE 100 /* 每次可以接收的最大字節(jié) */ 
            int main(int argc, char *argv[]) 
               

               
            int sockfd, numbytes; 
               
            char buf[MAXDATASIZE]; 
               
            struct hostent *he; 
               
            struct sockaddr_in their_addr; /* connector's address information */ 
            if (argc != 2
               fprintf(stderr,
            "usage: client hostname\n"); 
               exit(
            1); 
               }
             
            if ((he=gethostbyname(argv[1])) == NULL) /* get the host info */ 
               herror(
            "gethostbyname"); 
               exit(
            1); 
               }
             

            if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1
               perror(
            "socket"); 
               exit(
            1); 
               }
             

            their_addr.sin_family 
            = AF_INET; /* host byte order */ 
              their_addr.sin_port 
            = htons(PORT); /* short, network byte order */ 
              their_addr.sin_addr 
            = *((struct in_addr *)he->h_addr); 
              bzero(
            &(their_addr.sin_zero),; /* zero the rest of the struct */ 
            if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct 
            sockaddr)) 
            == -1
               perror(
            "connect"); 
               exit(
            1); 
               }
             
            if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1
               perror(
            "recv"); 
               exit(
            1); 
               }
             
            buf[numbytes] 
            = '\0'
            printf(
            "Received: %s",buf); 
            close(sockfd); 
            return 0
               }
             

            久久综合给合综合久久| 久久国产乱子伦免费精品| 久久精品成人| 久久久噜噜噜久久中文字幕色伊伊| 精品国产99久久久久久麻豆| av国内精品久久久久影院 | 亚洲精品国产综合久久一线| 久久婷婷午色综合夜啪| 久久婷婷成人综合色综合| 久久久久亚洲AV无码专区桃色| 久久久www免费人成精品| 欧美日韩中文字幕久久伊人| 偷窥少妇久久久久久久久| 国产精品久久久久久久久| 色青青草原桃花久久综合| 婷婷综合久久狠狠色99h| 免费精品久久天干天干| 国产精品成人99久久久久 | A级毛片无码久久精品免费| 国内精品久久国产大陆| 亚洲第一永久AV网站久久精品男人的天堂AV| 久久人人爽人人爽人人av东京热| 亚洲国产成人久久综合碰碰动漫3d | 久久久久亚洲精品天堂| 亚洲精品tv久久久久久久久久| 国产日产久久高清欧美一区| 久久久久久综合网天天| 亚洲精品无码久久毛片| 久久久久亚洲AV成人网人人网站| 日本三级久久网| 精品久久久久久成人AV| 久久精品国产亚洲av麻豆色欲| 久久久久波多野结衣高潮| 伊人久久大香线蕉综合5g| 午夜精品久久久久久| 久久精品国产一区二区三区 | 久久青青色综合| 久久精品免费全国观看国产| 国产美女亚洲精品久久久综合| 精品国产乱码久久久久久呢| 色偷偷久久一区二区三区|