Linux Socket网络编程基础(2)

protocol: 特殊协议, 一般给0

准备通信地址: struct sockaddr{ //主要用于函数的形参类型, 很少定义结构体变量使用, 叫做通用的通信地址类型//$man bind sa_family_t sa_family; char sa_data[14]; } struct sockaddr_un{ //准备本地通信的通信地址 //$man un.h sa_family_t sun_family;//协议族,就是socket()的domain的AF_LOCAL char sun_path[];//文件的路径 } struct sockaddr_in{ //准备网络通信的通信地址 //$man in.h sa_family_t sin_family; //协议族, 就是socket()的domain的AF_INET in_port_t sin_port; //端口号 struct in_addr sin_addr; //IP地址 } struct in_addr{ in_addr_t s_addr; //整数类型的IP地址 } bind(): //把通信地址和socket文件描述符绑定,用在服务器端,成功返回0,失败返回-1设errno int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

sockfd: socket文件的fd(returned by socket())
addr: 需要强制类型转换成socketaddr_un或soketaddr_in, 参见上
addrlen: 通信地址的大小, 使用sizeof();

connect(): //初始化一个socket的连接,用在客户端,成功返回0,失败返回-1设errno int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

sockfd: socket文件的fd(returned by socket())
addr: 需要强制类型转换成socketaddr_un或soketaddr_in, 参见上
addrlen: 通信地址的大小, 使用sizeof();

例子 //xxx.socket/1vs1 , server #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/un.h> #include<stdio.h> #include<stdlib.h> #include<string.h> int main(){ //1. create socket int sockfd=socket(AF_LOCAL,SOCK_DGRAM,0); if(-1==sockfd) perror("socket"),exit(-1); printf("create socket successfully\n"); //2. prepare communication addr struct sockaddr_un addr; addr.sun_family=AF_LOCAL; strcpy(addr.sun_path,"a.sock"); //会自动创建a.sock文件 //3. bind socket and addr int res=bind(sockfd,(struct sockaddr*)&addr,sizeof(addr)); //客户端是connect() if(-1==res) perror("bind"),exit(-1); printf("bind successfully\n"); //4. communicate char buf[100]={0}; res=read(sockfd,&buf,sizeof(buf)); //客户端是write() if(-1==res) perror("read"),exit(-1); printf("data sent by client:%s,size is %d\n",buf,sizeof(buf)); //5. close socket res=close(sockfd); if(-1==res) perror("close"),exit(-1); printf("close successfully\n"); return 0; } //IP/1vs1,server #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdio.h> #include<stdlib.h> #include<string.h> int main(){ //1. create socket int sockfd=socket(AF_INET,SOCK_DGRAM,0); if(-1==sockfd) perror("socket"),exit(-1); printf("create socket successfully\n"); //2. prepare communication addr struct sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_port=htons(8888); //转化端口号为网络字节序 //1~1024被系统占用,这里随便给个8888 addr.sin_addr.s_addr=inet_addr("176.43.1.34"); //转化字符串IP为整数IP //客户端也写这个服务器的地址 //3. bind socket and addr int res=bind(sockfd,(struct sockaddr*)&addr,sizeof(addr)); //客户端是connect() if(-1==res) perror("bind"),exit(-1); printf("bind successfully\n"); //4. communicate char buf[100]={0}; res=read(sockfd,&buf,sizeof(buf)); //客户端是write() if(-1==res) perror("read"),exit(-1); printf("data sent by client:%s,size is %d\n",buf,sizeof(buf)); //5. close socket res=close(sockfd); if(-1==res) perror("close"),exit(-1); printf("close successfully\n"); return 0; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/145e30aaab3902f977e030e3dc56d859.html