socket传图片
//client.cpp #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #define BUF_SIZE 1024 //typedef struct Image //{ //int time; //char name[10]; //}Image; int main(){ //先输入文件名,看文件是否能创建成功 char filename[100] = "recv.jpg"; //文件名 FILE *fp = fopen(filename, "wb"); //以二进制方式打开(创建)文件 if(fp == NULL){ printf("Cannot open file, press any key to exit!\n"); system("pause"); exit(0); } //向服务器(特定的IP和端口)发起请求 struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充 serv_addr.sin_family = AF_INET; //使用IPv4地址 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址 serv_addr.sin_port = htons(1234); //端口 //创建套接字 int sock = socket(AF_INET, SOCK_STREAM, 0); connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));//发送请求 //读取服务器传回的数据 char buffer[BUF_SIZE]={0}; int nCount; while( (nCount = recv(sock, buffer, BUF_SIZE,0)) > 0) { fwrite(buffer,nCount,1,fp); } puts("file transfer success!"); fclose(fp); //关闭套接字 close(sock); return 0; }
//server.cpp #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h>//struct sockaddr_in #include <sys/socket.h> #include <netinet/in.h> #define BUF_SIZE 1024 int main(){ char const*filename="send.jpg"; FILE *fp=fopen(filename,"rb"); if(fp == NULL) { printf("Cannot open file, press any key to exit!\n"); system("pause"); exit(0); } //创建套接字 int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //将套接字和IP、端口绑定 struct sockaddr_in serv_addr;//IP地址和端口设置 memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充 serv_addr.sin_family = AF_INET; //使用IPv4地址 (地址族,IPv4 or IPv6:AF_INET6) serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的32位IP地址,inet_addr将一个IP字符串转化为一个网络字节序的整数值,反向操作inet_ntoa() serv_addr.sin_port = htons(1234); //端口号 //将套接字serv_sock将IP地址和端口绑定 bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); //进入监听状态,等待用户发起请求 listen(serv_sock, 20); //接收客户端请求 struct sockaddr_in clnt_addr; socklen_t clnt_addr_size = sizeof(clnt_addr); //程序被阻塞,直到客户端发起请求 int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size); //向客户端发送数据 char buffer[BUF_SIZE] = {0}; int nCount; //write(clnt_sock, str, sizeof(str)); while((nCount = fread(buffer,1,BUF_SIZE,fp)) > 0) { send(clnt_sock,buffer,nCount,0); } shutdown(clnt_sock,SHUT_WR); recv(clnt_sock,buffer,BUF_SIZE,0); fclose(fp); //关闭套接字 close(clnt_sock); close(serv_sock); return 0; }
json:
https://www.h5w3.com/245029.html
c++:
https://blog.csdn.net/Keep_Trying_Go/article/details/119575632
python opencv:
https://blog.csdn.net/weixin_45885074/article/details/112661063
python :
https://blog.csdn.net/qq_24282081/article/details/106498871