标签:des style blog color os io 使用 ar 数据
只有几个基本的示例如下
建立服务端的绑定监听等过程的函数
1 #include<sys/types.h> 2 #include<sys/socket.h> 3 4 #include<netinet/in.h> 5 #include<arpa/inet.h> 6 #include<unistd.h> 7 8 #include<stdio.h> 9 #include<stdlib.h> 10 11 #include<strings.h> 12 #define PORT 2345 13 14 int main(void) 15 { 16 int sockfd,newsockfd; //socket 描述符 17 18 struct sockaddr_in addr; //IPv4套接口数据结构体 19 20 // struct sockaddr_in 21 // { 22 // uint8_t sin_len; //长度,固定16位 23 // sa_family_t sin_family; //地址族,IPv4为AF_INET 24 // in_port_t sin_port; //端口号 25 // struct in_addr sin_addr; 26 // // 27 // // struct in_addr 28 // // { 29 // // in_addr_t s_addr; //32位的IP地址 30 // // } 31 // // 32 // // 33 // unsigned char sin_zero[8]; //未使用的字段被填充为0 34 // } 35 36 int addr_len=sizeof(struct sockaddr_in); 37 38 if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) 39 //创建socket,返回描述符,使用IPv4和流 40 { 41 perror("create socket error!\n"); 42 exit(1); 43 } 44 else 45 { 46 printf("successfully create socket\n"); 47 printf("socket id is %d\n",sockfd); 48 } 49 50 //将表示地址的结构体变量addr数据清空 51 bzero(&addr,sizeof(struct sockaddr_in)); 52 53 //重新设定相关参数 54 //使用IPv4,设置port号,还有IP地址 55 addr.sin_family=AF_INET; 56 addr.sin_port=htons(PORT); 57 //绑定夲机默认地址 58 addr.sin_addr.s_addr=htonl(INADDR_ANY); 59 60 61 62 if(bind(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr))<0) 63 //绑定套接口和参数 64 { 65 perror("bind error\n"); 66 exit(1); 67 } 68 else 69 { 70 printf("bind succefully\n"); 71 printf("local POST is %d\n",PORT); 72 } 73 74 //下面是监听过程 75 if(listen(sockfd,5)<0) 76 { 77 perror("listen error\n"); 78 exit(1); 79 } 80 else 81 { 82 printf("listening........\n"); 83 } 84 85 if((newsockfd=accept(sockfd,(struct sockaddr *)(&addr),&addr_len)<0)) 86 { 87 perror("accept error\n"); 88 } 89 else 90 { 91 printf("accept a new socket client\n"); 92 printf("socket id is %d\n",newsockfd); 93 } 94 95 return 0; 96 }
连接远程主机的函数,用于服务端,使用百度的IP测试:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<strings.h> 4 #include<netinet/in.h> 5 #include<arpa/inet.h> 6 #include<sys/types.h> 7 #include<sys/socket.h> 8 #include<sys/stat.h> 9 #include<fcntl.h> 10 #include<unistd.h> 11 12 #define PORT 80 13 #define REMOTE_IP "119.75.218.77" 14 15 int main(void) 16 { 17 int sockfd; 18 struct sockaddr_in addr; 19 if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) 20 { 21 perror("error to socket :"); 22 exit(1); 23 } 24 else 25 { 26 printf("have open socket in %d\n",sockfd); 27 } 28 29 bzero(&addr,sizeof(struct sockaddr_in)); 30 addr.sin_family=AF_INET; 31 addr.sin_port=htons(PORT); 32 addr.sin_addr.s_addr=inet_addr(REMOTE_IP); 33 34 if(connect(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr))<0) 35 { 36 perror("error to connect : "); 37 exit(1); 38 } 39 else 40 { 41 printf("connect successfully\n"); 42 } 43 return 0; 44 }
相关结构体定义:
/* Structure describing an Internet socket address. */ struct sockaddr_in { __SOCKADDR_COMMON (sin_); in_port_t sin_port; /* Port number. */ struct in_addr sin_addr; /* Internet address. */ /* Pad to size of `struct sockaddr‘. */ unsigned char sin_zero[sizeof (struct sockaddr) - __SOCKADDR_COMMON_SIZE - sizeof (in_port_t) - sizeof (struct in_addr)]; };
使用recv接受远程服务器的返回信息
ftp.gnu.org IP=208.118.235.20
1 #include<stdio.h> 2 #include<strings.h> 3 #include<arpa/inet.h> 4 #include<netinet/in.h> 5 #include<unistd.h> 6 #include<stdlib.h> 7 #include<fcntl.h> 8 #include<sys/stat.h> 9 #include<sys/socket.h> 10 #include<sys/types.h> 11 12 //ftp默认端口是21 13 #define PORT 21 14 #define REMOTE_IP "208.118.235.20" 15 16 17 int main(void) 18 { 19 int sockfd; 20 struct sockaddr_in addr; 21 char buf[1024]; 22 23 if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) 24 { 25 perror("error to socket :"); 26 exit(1); 27 } 28 else 29 { 30 printf("successful sockfd is %d\n",sockfd); 31 } 32 33 bzero(&addr,sizeof(struct sockaddr_in)); 34 addr.sin_family=AF_INET; 35 addr.sin_port=htons(PORT); 36 addr.sin_addr.s_addr=inet_addr(REMOTE_IP); 37 38 if(connect(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr))<0) 39 { 40 perror("error on connect :"); 41 exit(1); 42 } 43 else 44 { 45 printf("connect gun successfully\n"); 46 printf("IP=%s\n",REMOTE_IP); 47 printf("PORT=%d\n\n",PORT); 48 } 49 50 //接受数据 51 recv(sockfd,buf,sizeof(buf),0); 52 53 printf("msg receved\n"); 54 //输出数据 55 printf("%s",buf); 56 57 printf("\n"); 58 59 return 0; 60 }
使用read读取数据:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<strings.h> 4 #include<netinet/in.h> 5 #include<arpa/inet.h> 6 #include<sys/types.h> 7 #include<sys/socket.h> 8 #include<sys/stat.h> 9 #include<fcntl.h> 10 #include<unistd.h> 11 12 #define PORT 2345 13 #define REMOTE_IP "192.168.1.102" 14 15 int main(void) 16 { 17 int sockfd; 18 int newsockfd; 19 20 struct sockaddr_in addr; 21 22 int addr_len=sizeof(struct sockaddr_in); 23 24 char msgbuf[256]; 25 26 if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0) 27 { 28 perror("error to socket :"); 29 exit(1); 30 } 31 else 32 { 33 printf("have open socket in %d\n",sockfd); 34 } 35 36 bzero(&addr,sizeof(struct sockaddr_in)); 37 addr.sin_family=AF_INET; 38 addr.sin_port=htons(PORT); 39 addr.sin_addr.s_addr=htons(INADDR_ANY); 40 41 if(bind(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr))<0) 42 { 43 perror("bind error :"); 44 exit(1); 45 } 46 else 47 { 48 printf("bind OK!\n"); 49 } 50 51 if(listen(sockfd,5)<0) 52 { 53 printf("listen error :"); 54 exit(1); 55 } 56 else 57 { 58 printf("listening ...\n"); 59 } 60 61 if((newsockfd=accept(sockfd,(struct sockaddr *)(&addr),&addr_len))) 62 { 63 perror("accept error"); 64 exit(1); 65 } 66 else 67 { 68 printf("accept OK!\n"); 69 70 if(read(newsockfd,msgbuf,sizeof(msgbuf))<=0) 71 { 72 perror("read error :"); 73 exit(1); 74 } 75 else 76 { 77 printf("msgbox : %s \n",msgbuf); 78 } 79 } 80 81 return 0; 82 }
标签:des style blog color os io 使用 ar 数据
原文地址:http://www.cnblogs.com/lhyz/p/3958600.html