關於用serversocket元件的clientread事件接收檔案的問題!!!!!!!!!!! |
尚未結案
|
wishmaster926
初階會員 發表:91 回覆:69 積分:32 註冊:2006-12-13 發送簡訊給我 |
各位大大:
有一問題,卡了好久,希望高手大大們給予指點迷津!!!!!!! 我目前寫二程式,一程式是c的console application,作用是winsock的client端, 一程式是用c builder開發的gui 程式,是server端. server端有一edit元件,當我輸入 "copy test.doc"時,client端就會把對應的檔案 利用winsock的send()函數傳給server端,而在server端,我是在ClientRead事件 中做接收. client 端程式 [code cpp] #pragma hdrstop #pragma argsused #include #include #include #include #include #include #pragma comment(lib,"ws2_32.lib") #pragma hdrstop #pragma argsused char blank[8]="", cmd[516], cmd_arg_1[4], cmd_arg_2[100], caRec[128], file_size[40], buf[516], *buffer=new Byte[1024] ; char *cmd_table[]={{"copy"},{"save"}}; FILE *fp; int bytes_received,bytes_send,nBytes,cmd_index=99; unsigned long filesize; void main(int argc, char* argv[]) { WORD wVersionRequested; WSADATA wsaData; wVersionRequested=MAKEWORD(2,0); WSAStartup(wVersionRequested,&wsaData); SOCKET client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if (client==INVALID_SOCKET) { printf("Failed socket!!\n"); return; } sockaddr_in sin; sin.sin_family=AF_INET; sin.sin_port=htons(atoi(argv[2])); sin.sin_addr.S_un.S_addr=inet_addr(argv[1]); if (!connect(client,(sockaddr*)&sin,sizeof(sin))==-1) { printf("Fail connect!!\n"); return; } while(true) { strcpy(caRec," "); strcpy(cmd_arg_1," "); strcpy(cmd_arg_2," "); nBytes=recv(client,caRec,128,0); printf("%s \n",caRec); for (int i=0;i if (caRec[i]=='\n'||caRec[i]=='\r') caRec[i]='\0'; } int j=0; cmd_index=99; //分析cmd命令 //先取命令 for (int i=0;i<4;i ) cmd_arg_1[i]=caRec[i]; cmd_arg_1[4]='\0'; //printf("%s \n",cmd_arg_1); for (int i=0;i<2;i ) { if (!strcmp(cmd_arg_1,cmd_table[i])) { cmd_index=i; break; } } int i=5; do { cmd_arg_2[j ]=caRec[i]; i ; }while(caRec[i]!='\0'); cmd_arg_2[j]='\0'; //printf("%s \n",cmd_arg_2); switch(cmd_index) { case 0: //處理主控端要求檔案傳輸的要求 //計算傳送檔案大小 fp=fopen(cmd_arg_2,"rb"); HANDLE hFile; WIN32_FIND_DATA wfd; hFile=FindFirstFile(cmd_arg_2,&wfd); if (wfd.nFileSizeHigh==0) { filesize=wfd.nFileSizeLow; } else { filesize=wfd.nFileSizeHigh wfd.nFileSizeLow; } itoa(filesize,buffer,10); //先送檔案大小 send(client,buffer,(int)strlen(buffer),0); Sleep(200); while(filesize>0) { Sleep(200); if (filesize<1024) { fread(buffer,filesize,1,fp); bytes_send=send(client,buffer,filesize,0); break; } else { fread(buffer,1024,1,fp); bytes_send=send(client,buffer,1024,0); filesize-=bytes_send; printf("%d\n",filesize); } } fclose(fp); break; default: printf("no catch copy command\n"); break; } } } [/code] client端的發送檔案程式,我用另支c寫的console程式測過,沒有問題. 但我現在用c builder寫的server端接檔程式來接收,卻是怎麼樣都接不到. 檔案有fopen出來,但是最後size還是0. 因為我不太熟悉builder的clientread事件中,用receivebuff()函數的接收寫法, 故希望各位大大能指點迷津,感恩!!!!!!!!!!!!!!!!!!!!!!!!! server端程式 [code cpp] *************Edit元件送出copy test.doc指令************* void __fastcall TForm1::cmd_commandKeyPress(TObject *Sender, char &Key) { char *cmd_table[]={{"copy"},{"save"}}; AnsiString temp,temp_; String memo_showmessage; if (Key==13) { if (cmd_command->Text.IsEmpty()||current_host_sn==-1) { } else { //分析命令 //先取命令 cmd_arg_1=cmd_command->Text.SubString(1,4).c_str(); //先取命令 for (int i=0;i<2;i ) { if (!strcmp(cmd_arg_1.c_str(),cmd_table[i])) { cmd_index=i; break; } } //是copy或save命令 //再取參數 cmd_arg_2=cmd_command->Text.Delete(1,4); switch (cmd_index) { case 0://copy指令 server_socket1->Socket->Connections[current_host_sn]->SendText(cmd_command->Text); //開一新檔 fp=fopen(cmd_arg_2.c_str(),"wb"); Sleep(100); //先接檔案大小 filesize=atoi(server_socket1->Socket->Connections[current_host_sn]->ReceiveText().c_str()); //server_socket1->Socket->Connections[current_host_sn]->ReceiveText(); //開始接收檔案 start_receive_file=true; default: break; } }}} void __fastcall TForm1::server_socket1ClientRead(TObject *Sender, TCustomWinSocket *Socket) { String previous_receive_message, cmd_arg_1, cmd_arg_2; int client_sn=1,online_host=0,current_host_sn=-1,command_count=0, client_send_filesize_i,client_send_packetsize, bytes_received=0,bytes_send=0,cmd_index=99; unsigned long filesize=0; FILE *fp; if (start_receive_file) //在edit元件按下copy test.doc enter後,該旗標設為true. { byte length; while(filesize>0) { sleep(100); length=server_socket1->Socket->ReceiveLength(); //檔案尺寸比1024小的情況 if (filesize<1024) { bytes_received=server_socket1->Socket->Connections[current_host_sn]->ReceiveBuf(buffer,length); fwrite(buffer,filesize,1,fp); } else { bytes_received=server_socket1->Socket->Connections[current_host_sn]->ReceiveBuf(buffer,length); fwrite(buffer,1024,1,fp); } //假如檔案大小本來就比1024小 if (filesize<=1024) { receive_client->Lines->Add("檔案接收完成.\r\n"); break; } else { filesize-=bytes_received; //server_socket1->Socket->Connections[current_host_sn]->ReceiveLength(); //sprintf(memo_showmessage.c_str(),"尚剩餘%d bytes未接收",filesize); //receive_client->Lines->Add(memo_showmessage); } } fclose(fp); start_receive_file=false; } } [/code]
------
~~~~~~~~時時多爬文 勿使惹塵埃~~~~~~~~ ~~~~~~~~時時多爬文 勿使惹塵埃~~~~~~~~ ~~~~~~~~時時多爬文 勿使惹塵埃~~~~~~~~ |
wishmaster926
初階會員 發表:91 回覆:69 積分:32 註冊:2006-12-13 發送簡訊給我 |
|
wishmaster926
初階會員 發表:91 回覆:69 積分:32 註冊:2006-12-13 發送簡訊給我 |
===================引 用 wishmaster926 文 章=================== char blank[8]=" ", cmd[516], *buffer=new Byte[1024]; buf[1024]; AnsiString password="ab780000", line_feed="\n", echo_input="Your command:", current_host_remoteaddress; String previous_receive_message, cmd_arg_1, cmd_arg_2; int client_sn=1,online_host=0,current_host_sn=-1,command_count=0, client_send_filesize_i,client_send_packetsize, bytes_received=0,bytes_send=0,cmd_index=99; unsigned long filesize=0; FILE *fp;
------
~~~~~~~~時時多爬文 勿使惹塵埃~~~~~~~~ ~~~~~~~~時時多爬文 勿使惹塵埃~~~~~~~~ ~~~~~~~~時時多爬文 勿使惹塵埃~~~~~~~~ |
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |