网络编程 三要素:协议 IP 端口
C/S模型: 服务器 客户端 1、建立一个套接字 1、建立一个套接字 2、绑定端口和地址 2、发出连接请求 3、监听队列 3、收发数据 4、接收连接 4、关闭连接 5、收发数据 6、关闭连接
服务器: 1、建立一个套接字 socket
#include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol); 参数1:地址族 AF_INET 参数2:指定套接字的类型 SOCK_STREA 参数3:通常为0 自动匹配协议 返回值:如果建立成功会返回socket描述符 失败返回-12、绑定端口和地址 bind
#include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 参数1:socket描述符 参数2:需要填充的网络信息要素 参数3:填充结构体的大小 返回值:绑定成功返回0,失败返回-1 struct sockaddr { sa_family_t sa_family; //地址族 char sa_data[14]; }; 通常使用sockaddr_in来设置端口和地址,最后强制转换成sockaddr struct sockaddr_in { u_short sin_family; //地址族 u_short sin_port; //端口 struct in_addr sin_addr; //ip char sin_sero[8]; };3、监听队列 listen
#include <sys/types.h> #include <sys/socket.h> int listen(int sockfd, int backlog); 参数1:socket描述符 参数2:监听队列的大小 返回值:成功返回0,失败返回-14、接受客户端连接请求
#include <sys/types.h> #include <sys/socket.h> int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); 参数1:socket描述符 参数2:网络信息要素 参数3:结构大小的首地址 返回值:收发数据通道的句柄5、send
#include <sys/types.h> #include <sys/socket.h> ssize_t send(int sockfd, const void *buf, size_t len, int flags); 参数1:accept的返回值 参数2:数据缓存区 参数3:发送数据的大小 参数4:默认为06、recv
#include <sys/types.h> #include <sys/socket.h> ssize_t recv(int sockfd, void *buf, size_t len, int flags); 参数1:accept的返回值 参数2:数据缓存区 参数3:接收数据的大小 参数4:默认为07、connect
#include <sys/types.h> #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen); 客户端这边收发数据用的socket描述符还是scoket函数的返回值虚拟机中查看自己的IP地址指令:ifconfig 以下是客户端与服务端的源码:
/*=============================================================== * Copyright (C) 2020 All rights reserved. * * 文件名称:client.c * 创 建 者:wang * 创建日期:2020年10月13日 * 描 述: * * 更新日志: * ================================================================*/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <strings.h> #include <sys/socket.h> #include <sys/types.h> #include <arpa/inet.h> #include <netinet/in.h> int main() { //1.socket int sockfd = socket(AF_INET,SOCK_STREAM,0); if(sockfd < 0) { printf("socket failed!\n"); return -1; } printf("socket ok!\n"); //2.connect struct sockaddr_in stServer; memset(&stServer,0,sizeof(stServer)); stServer.sin_family = AF_INET; stServer.sin_port = htons(8888); stServer.sin_addr.s_addr = inet_addr("192.168.1.194");//什么当服务器就写那个IP,板子当服务器就写板子,虚拟机当服务器就写虚拟机 int fd = connect(sockfd,(struct sockaddr *)&stServer,sizeof(stServer)); if(fd < 0) { printf("connect failed!\n"); return -1; } printf("connect ok!\n"); //3.recv char buf[100] = {0}; int ret = recv(sockfd,buf,sizeof(buf),0); if(ret < 0) { printf("recv failed!\n"); return -1; } printf("recv ok!buf : %s\n",buf); //4.send ret = send(sockfd,buf,strlen(buf),0); if(ret < 0) { printf("send failed!\n"); return -1; } printf("send ok!\n"); //close close(sockfd); return 0; } /*=============================================================== * Copyright (C) 2020 All rights reserved. * * 文件名称:server.c * 创 建 者:wang * 创建日期:2020年10月12日 * 描 述: * * 更新日志: * ================================================================*/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <strings.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> int main() { //1.socket int iServer = socket(AF_INET,SOCK_STREAM,0); if(iServer < 0) { printf("socket failed!\n"); return -1; } printf("socket ok!\n"); //2.bind struct sockaddr_in stServer; memset(&stServer,0,sizeof(stServer)); stServer.sin_family = AF_INET; stServer.sin_port = htons(8888); stServer.sin_addr.s_addr = inet_addr("192.168.1.194"); int ret = bind(iServer,(struct sockaddr *)&stServer,sizeof(stServer)); if(ret < 0) { printf("bind failed!\n"); return -1; } printf("bind ok!\n"); //3.listen ret = listen(iServer,5); if(ret < 0) { printf("listen failed!\n"); return -1; } printf("listen ok!\n"); //4.accept struct sockaddr_in stClient; memset(&stClient,0,sizeof(stClient)); socklen_t len = sizeof(stClient); int iClient = accept(iServer,(struct sockaddr *)&stClient,&len); if(iClient < 0) { printf("accept failed!\n"); return -1; } else { //5.send printf("accept ok!\n"); char buf[100] = "helloworld"; //printf("input string:\n"); //fgets(buf,99,stdin); ret = send(iClient,buf,strlen(buf),0); if(ret < 0) { printf("send failed!\n"); return -1; } printf("send ok!\n"); //6.recv memset(buf,0,sizeof(buf)); ret = recv(iClient,buf,sizeof(buf),0); if(ret < 0) { printf("recv failed!\n"); return -1; } printf("recv ok!buf : %s\n",buf); } //7.close close(iClient); return 0; }