当前位置: 首页>>代码示例>>C++>>正文


C++ Load::receiveFile方法代码示例

本文整理汇总了C++中Load::receiveFile方法的典型用法代码示例。如果您正苦于以下问题:C++ Load::receiveFile方法的具体用法?C++ Load::receiveFile怎么用?C++ Load::receiveFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Load的用法示例。


在下文中一共展示了Load::receiveFile方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: run


//.........这里部分代码省略.........
				if (connect(sock, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0) {
					printf("Failed to connect to server %s\n", serverName);
				}
				else {

					boolean correctCmd = false;
					do {
						printf("\nType the command you want to perform (get, put, list) : ");
						cin >> cmd;

						if(strcmp(cmd,"get")==0) {
							smsg.type=REQ_GET;
							correctCmd = true;
						}
						else if (strcmp(cmd,"put")==0)
						{
							smsg.type=REQ_PUT;
							correctCmd = true;
						}
						else if (strcmp(cmd,"list")==0) {
							smsg.type=REQ_LIST;
							correctCmd = true;
						}
						else {
							printf("Wrong request command.\nAvailable:\n \t\tget\n\t\tput\n\t\tlist");
							correctCmd = false;
						}
					}while(!correctCmd);

					if(smsg.type == REQ_PUT || smsg.type == REQ_GET) {
						printf("\nType name of file to be transferred:  ");
						cin >> fileName;

						strcpy_s(req.filename, FILENAME_LENGTH, fileName);

						if(smsg.type == REQ_PUT)
						{
							fopen_s(&pFile, fileName,"rb");
							if(pFile == NULL) {
								printf("\nCannot open file %s : the file might not exist, or you don't have the permissions to open it.", fileName);
								smsg.type=REQ_CANCEL;
							}
						}
					}

					//send out the message
					memcpy(smsg.buffer,&req,sizeof(req)); //copy the request to the msg's buffer
					smsg.length=sizeof(req);
					if (msg_send(sock,&smsg) != sizeof(req))
						err_sys("Sending req packet error.,exit");

					if(smsg.type != REQ_CANCEL)
						printf("\nSent request to %s, waiting...\n", serverName);

					//receive the response
					if(msg_recv(sock,&rmsg)!=rmsg.length)
						err_sys("recv response error,exit");

					//cast it to the response structure
					respp=(Resp *)rmsg.buffer;

					// File not found
					if(strcmp(respp->response, "No such a file") == 0) 
					{
						// File does not exist
						printf("\n%s\n", respp->response);
					}
					// PUT
					else if(strcmp(respp->response, "OK") == 0) 
					{
						if (pFile!=NULL){
							loader->sendFile(pFile, fileName, serverName, sock);
						}

					} // END PUT

					// GET
					else if(strcmp(respp->response, "READY") == 0) 
					{
						loader->receiveFile(fileName, serverName, sock);
					} // END GET

					// LIST
					else if(strcmp(respp->response, "List files") == 0){
						getList(serverName);
					} // END LIST

					//close the client socket
					closesocket(sock);
				}
			}
		} // END if severname = quit
		else {
			printf("\nClient closing...");
			printf("\nPress any key to quit...");
			fflush(stdin);
			getchar();
		}

	}
开发者ID:Dzeta,项目名称:COMP445,代码行数:101,代码来源:client.cpp


注:本文中的Load::receiveFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。