socket 基本套接字1

  1. 簡單的時間獲取客戶程序

    1.1套接字地址結構

    IPv4套接字地址結構
    
    #include <netinet/in.h>
    
    struct in_addr {
       in_addr_t    s_addr;           // 32 bit IPv4 address    
    };                                // network byte ordered
    
    struct sockaddr_in {
           uint8_t          sin_len;      // length of structure(16)    
       sa_family_t      sin_family;   // AF_INET
       in_port_t        sin_port;     // 16 bit TCP or UDP port number,network byte ordered
       struct in_addr   sin_addr;     // 32 bit IPv4 address,network byte ordered
       char             sin_zero[8]   // unused     
    };    
    
    

    1.2 客戶程序

//client01.c 
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#define     MAXLINE     1024

int main(int argc,char *argv[])
{
  int sockfd , n ;
  char recvline[MAXLINE];
  struct sockaddr_in   servaddr;

  if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 ){
    printf(" socket error \n");
    return -1;     
  } 

  /*  清空結構體   #include<string.h> */
  /*  void  bzero(void *s, size_t n)*/
  bzero(&servaddr,sizeof(struct sockaddr_in));
  servaddr.sin_family = AF_INET;

  /*  uint16_t  htons(uint16_t hostshort) 
   *   host ----> network    
   */
  servaddr.sin_port = htons(13) ;

  /*  #include <arpa/inet.h> 
   *  int  inet_pton(int af, const char *src, void *dst)
   *  成功: 返回 1,出錯返回 -1,若對指定的family而言輸入的的字符串不是有效的表達式,則返回0
   *  該函數對ipv4 和 ipv6 地址都適用
   *  該函數將 字符串 src 轉換爲 二進制結果存放在dst 地址中
   */
  if(inet_pton(AF_INET,argv[1],(void *)&servaddr.sin_addr) <= 0){
    printf(" inet_pton error %d \n",__LINE__);
    close(sockfd);
  }

  while( n = read(sockfd,recvline,MAXLINE) > 0){
    recvline[n] = 0;  // 標準輸出中0表示文件結尾? 
    if(fputs(recvline,stdout) == EOF)  // EOF表示出錯
      printf("fputs error \n");
  }

  if( n < 0)
    printf("read error \n");

  exit(0);
}

1.3 相關知識點

 1.3.1  NULL \0 , 0 , EOF  區別 
     NULL: 空指針,在 stddef.h 文件中有如下定義

         #unddf  NULL
         #if  define(__cplusplus)
         #define  NULL   0
         #else
         #define  NULL   (void *)0
         #endif


    \0: 字符串結束標誌     
     0: 整數值
   EOF: 文件結束標誌,通常定義爲(-1).文件結尾時會返回EOF,出錯時也會返回EOF.   

相關EOF問題參考:http://www.cnblogs.com/dolphin0520/archive/2011/10/13/2210459.html
http://www.ruanyifeng.com/blog/2011/11/eof.html

1.3.2 read 和 fputs 函數(參考 《UNIX環境高級編程》 第三版)

read 函數原型:
#include <unistd.h>
ssize_t read(int fd,void *buf,size_t nbytes);
        返回值: 讀到的字節數,若已到文件尾,返回0;若出錯,返回 -1
fputs 函數原型:
#include <stdio.h>
int fputs(const char *restrict str, FILE *restrict fp);
int puts(const char *str);
         Both return: non-negative value if OK, EOF on error

The function fputs writes the null-terminated string to the specified stream. The null
byte at the end is not written. Note that this need not be line-at-a-time output, since the
string need not contain a newline as the last non-null character. Usually, this is the
case — the last non-null character is a newline—but it’s not required

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章