📄 broadcast.cpp
字号:
#include <qapplication.h>#include <qwidget.h>#include <qpushbutton.h>#include <qpainter.h>#include <qpixmap.h>#include <qpicture.h>#include <qmessagebox.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/ioctl.h>#include <net/if.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <syslog.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <termios.h>#include <sys/socket.h>#include <pthread.h>#include "mytypes.h"#include <semaphore.h>#include <time.h>//related to msg list#include <sys/ipc.h>#include <sys/shm.h>#include <sys/msg.h>int udpclient(){ int sock=socket(AF_INET,SOCK_DGRAM,0); if(sock==-1) { printf("create sock error\n"); return -1; } struct sockaddr_in addr; memset(&addr,0,sizeof(struct sockaddr_in)); addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_family=AF_INET; addr.sin_port=htons(7010); if(bind(sock,(struct sockaddr*)&addr,sizeof(struct sockaddr))==-1) { printf("bind error\n"); return -1; } int recvlen,clientsock; char recvbuff[2000]; bool recving=false; FILE* pfile=NULL; int hasrecv=0; int oldfiletag=-1; for(;;) { if((recvlen=recvfrom(sock,recvbuff,2000,0,NULL,NULL))>0) { printf("recvlen:%d\n",hasrecv+=recvlen-sizeof(BROADCASTHEAD)); if (recvlen>0) { if(!recving||oldfiletag!=PBROADCASTHEAD(recvbuff)->ifiletag) { if(pfile) { fclose(pfile); hasrecv=0; } pfile=fopen("main.bmp","w+b"); printf("cread file\n"); char* ptemp=new char[PBROADCASTHEAD(recvbuff)->itotalsize]; fwrite(ptemp,1,PBROADCASTHEAD(recvbuff)->itotalsize,pfile); recving=true; oldfiletag=PBROADCASTHEAD(recvbuff)->ifiletag; } printf("recv size:%d,ipos:%d\n",recvlen,PBROADCASTHEAD(recvbuff)->ipos); //printf("data:%s",recvbuff+sizeof(BROADCASTHEAD)); fseek(pfile,PBROADCASTHEAD(recvbuff)->ipos*1024,SEEK_SET); fwrite(recvbuff+sizeof(BROADCASTHEAD),1,recvlen-sizeof(BROADCASTHEAD),pfile); if(hasrecv==PBROADCASTHEAD(recvbuff)->itotalsize) { printf("close file\n"); fclose(pfile); pfile=NULL; recving=false; hasrecv=0; } } } } close(sock); return -1;}int main(){ udpclient(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -