网络TCP/UDP编程学习

Socket分为三种:

1.      SOCK_STREAM  TCP

2.      SOCK_DGRAM  UDP

3.      SOCK_RAW   基于IP协议,用于新的网络协议的测试。

常用结构体:

Struct sockaddr

{

       U_shortsa_family;//协议族,AF_XXXX

       Charsa_data[14]; //14字节的特定协议地址。

}

等价于上述结构体的是sockaddr_in比较常用这个结构体

Struct sockaddr_in

{

       Shortsin_family;

       Unsignedshort int sin_port;

       Structin_addr sin_addr; //

       Unsignedchar sin_zero[8]; //

}

 

Typedef struct in_addr

{

       Union{

              Struct

{

Unsigned char s_b1,

S_b2,

S_b3,

S_b4;
}S_un_b;

              Struct{

                     Unsignedshort s_w1,

                     S_w2;

}s_un_w;

Unsigned long s_addr;

}s_un

}IN_ADDR

 

由于大小端的缘故,提供几个函数进行转换

Htons    short 型转换 host to network

Htonl   long  型转换 host to network

Ntohs   short 型转换network  to host

Ntohl    long  型转换 network to host

 

 

TCP 程序设计:

server:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <errno.h>    
  4. #include <string.h>    
  5. #include <netdb.h>    
  6. #include <sys/types.h>    
  7. #include <netinet/in.h>    
  8. #include <sys/socket.h>    
  9.   
  10. #define MAX_CONN_NUM (20)   
  11. #define PORT_NUM (2000)  
  12.   
  13. int main(int argc, char **argv)  
  14. {  
  15.     int ret = 0;  
  16.     int sock_fd, new_fd;  
  17.     struct sockaddr_in server_addr, client_addr;  
  18.     int sock_in_size;  
  19.     int n_bytes;  
  20.     char buf[512];  
  21.       
  22.     /* 1 create socket */  
  23.     sock_fd = socket(AF_INET, SOCK_STREAM, 0);  
  24.     if (-1 == sock_fd) {  
  25.         fprintf(stdout, "create sock err = %s\n", strerror(errno));  
  26.         exit(1);  
  27.     }  
  28.   
  29.     /*set default val*/  
  30.     bzero(&server_addr, sizeof(struct sockaddr_in));  
  31.     server_addr.sin_family = AF_INET;  
  32.     server_addr.sin_addr.s_addr = htonl(INADDR_ANY);  
  33.     server_addr.sin_port = htons(PORT_NUM);  
  34.   
  35.     /*2 bind socket*/  
  36.     if (bind(sock_fd,   
  37.         (struct sockaddr *)(&server_addr),  
  38.         sizeof(struct sockaddr)) == -1) {  
  39.         fprintf(stdout, "bind error = %s\n",strerror(errno));  
  40.         exit(1);  
  41.     }  
  42.     /*3 listen*/  
  43.     if(listen(sock_fd, MAX_CONN_NUM) == -1) {  
  44.         fprintf(stdout, "listen error = %s\n", strerror(errno));  
  45.         exit(1);      
  46.     }  
  47.       
  48.     /**/  
  49.     while(1) {  
  50.         /*4 accept */  
  51.         sock_in_size = sizeof(struct sockaddr_in);  
  52.         new_fd = accept(sock_fd,  
  53.                 (struct sockaddr *)(&client_addr),  
  54.                 &sock_in_size);  
  55.         if (-1 == new_fd) {  
  56.             fprintf(stdout, "accept error : %s\n", strerror(errno));  
  57.             exit(1);  
  58.         }     
  59. //      fprintf(stdout, "connection from : %s\n", inet_ntoa(client_addr.sin_addr));//inet_ntoa 函数在ubuntu上用不了,用inet_ntop  
  60.         fprintf(stdout, "connection from : \n");  
  61.   
  62.         /*5 read/write */     
  63.         n_bytes = read(new_fd, buf, 512);  
  64.         if (-1 == n_bytes) {  
  65.             fprintf(stdout,"read error : %s\n", strerror(errno));  
  66.             exit(1);  
  67.         }  
  68.         buf[n_bytes] = '\0';  
  69.         fprintf(stdout,"client:%s\n", buf);  
  70.           
  71.         close(new_fd);  
  72.     }  
  73.     /*close socket*/  
  74.     close(sock_fd);  
  75.     exit(0);      
  76.     return ret;  
  77. }  
client:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <errno.h>    
  4. #include <string.h>    
  5. #include <netdb.h>    
  6. #include <sys/types.h>    
  7. #include <netinet/in.h>    
  8. #include <sys/socket.h>    
  9.   
  10. #define MAX_CONN_NUM (20)   
  11. #define PORT_NUM (2000)  
  12.   
  13. int main(int argc, char **argv)  
  14. {  
  15.     int ret = 0;  
  16.     int sock_fd;  
  17.     struct sockaddr_in server_addr;  
  18.     char buf[512];  
  19.     struct hostent *host;  
  20.     if (argc != 2) {  
  21.         fprintf(stderr, "input server hostname\n");  
  22.         exit(1);  
  23.     }     
  24.       
  25.     host = gethostbyname(argv[1]);  
  26.     if (NULL == host) {  
  27.         fprintf(stderr,"get hostname error\n");  
  28.         exit(1);  
  29.     }  
  30.     /* 1 create socket */  
  31.     sock_fd = socket(AF_INET, SOCK_STREAM, 0);  
  32.     if (-1 == sock_fd) {  
  33.         fprintf(stderr, "create sock err = %s\n", strerror(errno));  
  34.         exit(1);  
  35.     }  
  36.   
  37.     /*set default val*/  
  38.     bzero(&server_addr, sizeof(struct sockaddr_in));  
  39.     server_addr.sin_family = AF_INET;  
  40.     server_addr.sin_addr = *((struct in_addr *)host->h_addr);  
  41.     server_addr.sin_port = htons(PORT_NUM);  
  42.   
  43.     /*2 connect socket*/  
  44.     if(-1 == connect(sock_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr))) {  
  45.         fprintf(stderr, "connect error = %s\n",strerror(errno));  
  46.         exit(1);  
  47.     }  
  48.     /*3 read/write */  
  49.     printf("please input:\n");  
  50.     fgets(buf, 512, stdin);  
  51.     write(sock_fd, buf, strlen(buf));  
  52.           
  53.     /*4 close socket*/  
  54.     close(sock_fd);  
  55.     exit(0);      
  56.     return ret;  
  57. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值