📄 receiver.cpp
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>#include <sys/socket.h>#include <sys/wait.h>#include <netinet/in.h>#include <arpa/inet.h>#include <pthread.h>int mySocket[5];struct sockaddr_in adr_s;pthread_t thread_id[5];void EOP(const char* errorCode) { printf("Receiver error: %s;\n",errorCode); exit(1); }void Combine(char *fileName) { FILE *out = fopen(fileName,"w+"),*in; char infn[5]; char buf[513]; int byte; strcpy(infn,"tmp "); for(int i=0;i<5;i++) { infn[3]=(char)(i+'0'); in = fopen(infn,"r"); if (in==NULL) EOP("Combine failed."); while( (byte=fread(buf,1,512,in))!=0 ) { if (byte==-1) EOP("Combine failed in read *in."); fwrite(buf,1,byte,out); } fclose(in); } fclose(out);}void ReceiveAndSave(int id) { long fileLens; int segment,temp,len; temp = read(mySocket[id],&fileLens,sizeof(long)); if (temp<0) EOP("ReceiveAndSave() failed."); temp = read(mySocket[id],&segment,sizeof(int)); if (temp<0) EOP("ReceiveAndSave() failed."); char *buf = (char*)malloc(sizeof(char)*fileLens+1),*buf_p; buf_p = buf; len = fileLens; while( (temp=read(mySocket[id],buf_p,len))!=0 ) { if (temp == -1) EOP ("One socket read failed"); len -= temp; buf_p += temp; } close(mySocket[id]); printf("Thread %d connect & received ok. --len:%ld--seg:%d-- now saving..\n",id,fileLens,segment); buf[sizeof(char)*fileLens] = 0; char tmpName[5]; strcpy(tmpName,"tmp "); tmpName[3] = (char)(segment+'0'); tmpName[4] = 0; FILE *file = fopen(tmpName,"w+"); if (file==NULL) EOP("in R&S file error."); fwrite(buf,1,fileLens,file); fclose(file); free (buf); printf("Thread %d saving complete.\n",id); }void Connect(char *IPaddress,int port) { int tmp; memset(&adr_s,0,sizeof adr_s); adr_s.sin_family=AF_INET; adr_s.sin_port=htons(port); adr_s.sin_addr.s_addr=inet_addr(IPaddress); if (adr_s.sin_addr.s_addr==INADDR_NONE) EOP("wrong address.."); int len,t_s,id[5]; len = sizeof adr_s; for (int i=0;i<5;i++) { mySocket[i]=socket(PF_INET,SOCK_STREAM,0); if(mySocket[i]<0) EOP("socket create failed.."); tmp=connect(mySocket[i],(struct sockaddr*)&adr_s,len); if(tmp<0) EOP("connect failed"); id[i] = i; t_s=pthread_create(&thread_id[i],NULL,(void*(*)(void*))ReceiveAndSave,(void*)id[i]); if (t_s!=0) EOP("thread creating failed."); }}int main(void) { char fileName[128],IP[20]; printf("Please input the Sender's IP address: "); scanf("%s",IP); printf("Then Input the filename want to save: "); scanf("%s",fileName); Connect(IP,8083); for(int i=0;i<5;i++) pthread_join(thread_id[i],NULL); printf("Client Receive OK,now Combine them..\n"); Combine(fileName); printf("+-- Combining complete --+\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -