标签:pre 情况 nat bin socket stat makefile route clean
#include <sys/socket.h> int socket (int family, int type, int protocol); //返回:若成功则为非负描述符,若出错则为-1
family | 说 明 |
AF_INET AF_INET6 AF_LOCAL AF_ROUTE AF_KEY |
IPv4协议 IPv6协议 Unix域协议 路由套接口 密钥套接口 |
type | 说 明 |
SOCK_STREAM SOCK_DGRAM SOCK_SEQPACKET SOCK_RAW |
字节流套接口 数据报套接口 有序分组套接口 原始套接口 |
protocol | 说 明 |
IPPROTO_TCP IPPROTO_UDP IPPROTO_SCTP |
TCP传输协议 UDP传输协议 SCTP传输协议 |
#include <sys/socket.h> int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); //返回:若成功则为0,若出错则为-1
#include <sys/socket.h> int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); //返回,成功为0,出错为-1
#include <sys/socket.h> int listen (int sockfd, int backlog); //返回,成功为0,出错-1
#include <sys/socket.h> int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); //返回:若成功为非负描述符,出错为-1
server.c
#include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #define SERVER_PORT 5555 #define MAXLINE 4096 int main(void) { struct sockaddr_in serveraddr, clientaddr; int sockfd, addrlen, confd, len, i; char ipstr[128]; char buf[MAXLINE]; //1.socket sockfd = socket(AF_INET, SOCK_STREAM, 0); //2.bind bzero(&serveraddr, sizeof(serveraddr)); /* 地址族协议IPv4 */ serveraddr.sin_family = AF_INET; /* IP地址 */ serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons(SERVER_PORT); bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); //3.listen listen(sockfd, 128); while (1) { //4.accept阻塞监听客户端链接请求 addrlen = sizeof(clientaddr); confd = accept(sockfd, (struct sockaddr *)&clientaddr, &addrlen); //输出客户端IP地址和端口号 printf("client ip %s\tport %d\n", inet_ntop(AF_INET, &clientaddr.sin_addr.s_addr, ipstr, sizeof(ipstr)), ntohs(clientaddr.sin_port)); //和客户端交互数据操作confd //5.处理客户端请求 len = read(confd, buf, sizeof(buf)); i = 0; while (i < len) { buf[i] = toupper(buf[i]); i++; } write(confd, buf, len); close(confd); } close(sockfd); return 0; }
client.c
#include <netinet/in.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #define SERVER_PORT 8000 #define MAXLINE 4096 int main(int argc, char *argv[]) { struct sockaddr_in serveraddr; int confd, len; char ipstr[] = "192.168.6.254"; char buf[MAXLINE]; if (argc < 2) { printf("./client str\n"); exit(1); } //1.创建一个socket confd = socket(AF_INET, SOCK_STREAM, 0); //2.初始化服务器地址 bzero(&serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; //"192.168.6.254" inet_pton(AF_INET, ipstr, &serveraddr.sin_addr.s_addr); serveraddr.sin_port = htons(SERVER_PORT); //3.链接服务器 connect(confd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); //4.请求服务器处理数据 write(confd, argv[1], strlen(argv[1])); len = read(confd, buf, sizeof(buf)); write(STDOUT_FILENO, buf, len); //5.关闭socket close(confd); return 0; }
Makefile
all:server client server:server.c gcc $< -o $@ client:client.c gcc $< -o $@ .PHONY:clean clean: rm -f server rm -f client
6.数据报接受与发送
#include <sys/socket.h> ssize_t recvfrom(int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen); ssize_t sendto(int sockfd, const void *buff, size_t nbytes, int flags, const struct sockaddr *to, socklen_t addrlen); //均返回:若成功则为读或写的字节数,出错为-1 默认情况recvfrom函数没有接收到对方数据时候是阻塞的
UDP cs模型
server.c
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> #include <netinet/in.h> #include <ctype.h> #define SERVER_PORT 5556 #define MAXLINE 1024 int main(void) { int sockfd,i; ssize_t len; struct sockaddr_in serveraddr,clientaddr; char buf[MAXLINE]; char ipstr[INET_ADDRSTRLEN];//16个字节 socklen_t clientlen; //构造用于UDP通信的套接字 sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&serveraddr,sizeof(serveraddr)); serveraddr.sin_family =AF_INET; serveraddr.sin_addr.s_addr=htonl(INADDR_ANY); serveraddr.sin_port=htons(SERVER_PORT); //printf("%x\n",INADDR_ANY); bind(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr)); while(1){ clientlen=sizeof(clientaddr); len=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr *)&clientaddr,&clientlen); printf("client IP %s\t PORT %d\n", inet_ntop(AF_INET,&clientaddr.sin_addr.s_addr,ipstr,sizeof(ipstr)), ntohs(clientaddr.sin_port)); i=0; while(i<len){ buf[i]=toupper(buf[i]); i++; } buf[i]=‘\0‘; sendto(sockfd,buf,len,0,(struct sockaddr*)&clientaddr,sizeof(clientaddr)); } close(sockfd); return 0; }
client.c
#include <netinet/in.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <ctype.h> #define SERVER_PORT 5556 #define MAXLINE 4096 int main(int argc,char *argv[]) { struct sockaddr_in serveraddr; int confd; ssize_t len; char ipstr[]="123.206.59.138"; char buf[MAXLINE]; if(argc <2){ printf("./client str\n"); exit(1); } //1.创建一个socket confd=socket(AF_INET,SOCK_DGRAM,0); //2.初始化服务器地址 bzero(&serveraddr,sizeof(serveraddr)); serveraddr.sin_family=AF_INET; // inet_pton(AF_INET,ipstr,&serveraddr.sin_addr.s_addr); serveraddr.sin_port =htons(SERVER_PORT); //3向务器发送数据 sendto(confd,argv[1],strlen(argv[1]),0,(struct sockaddr *)&serveraddr,sizeof(serveraddr)); len=recvfrom(confd,buf,sizeof(buf),0,NULL,0); write(STDIN_FILENO,buf,len); close(confd); return 0; }
标签:pre 情况 nat bin socket stat makefile route clean
原文地址:http://www.cnblogs.com/tla001/p/6603027.html