資源簡介
這是一個運行于Linux環(huán)境下的基于服務器的文件共享程序,擁有網(wǎng)絡連接,文件上傳下載以及管理等功能!

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?“Global.h“
using?namespace?std;
string?GetHostIP();
int?ConnectServer();
void?UploadFile();
void?DownloadFile();
void?LookoverList();
void?RefreshList();
int?clientSock;
list?gFileList;
list::iterator?gIter;
char?gMsgBuf[BUFSIZE];
int?main()
{
????while(1)
????{
????????system(“clear“);
????????cout<<“\t\t------------------Client------------------\n“< ????????cout<<“\n\t\tChoose?the?operation?from?the?menu:“< ????????cout<<“\t\t1.Upload?files?to?server“< ????????cout<<“\t\t2.Download?files?from?server“< ????????cout<<“\t\t3.Lookover?list?of?files“< ????????cout<<“\t\t0.Exit“< ????????cout<<“\t\tPlease?input?your?option:“;
????????int?opt;
????????cin>>opt;
????????switch(opt)
????????{
????????case?1:
????????????UploadFile();
????????????break;
????????case?2:
????????????DownloadFile();
????????????break;
????????case?3:
????????????LookoverList();
????????????break;
????????case?0:
????????????system(“clear“);
????????????return?0;
????????default:
????????????cout<<“\n\t\tinput?illegal!“< ????????????continue;
????????}
????}
????return?0;
}
int?ConnectServer()
{
????sockaddr_in?addr;
????if((clientSock=socket(AF_INETSOCK_STREAM0))<0)
????{
????????perror(“socket“);
????????exit(1);
????}
????bzero(&addrsizeof(addr));
????addr.sin_family=AF_INET;
????addr.sin_port=htons(PORT);
????addr.sin_addr.s_addr=inet_addr(REMOTE_IP);
????if(connect(clientSock(sockaddr*)&addrsizeof(addr))<0)
????{
????????perror(“connect“);
????????exit(1);
????}
????return?0;
}
void?UploadFile()
{
????RefreshList();
????ConnectServer();
????bzero(gMsgBufsizeof(gMsgBuf));
????FileItem?file;
????cout<<“\t\tPlease?input?the?path?of?file:\n\t\t“;
????string?strPath;
????cin>>strPath;
????int?pos=strPath.find_last_of(‘/‘);
????strcpy(file.attrib.namestrPath.substr(pos+1).c_str());
????strcpy(file.attrib.ipAddrGetHostIP().c_str());
????FILE?*fileStream;
????if((fileStream=fopen(strPath.c_str()“r“))==NULL)
????{
????????printf(“\n\t\tthe?file?was?not?opened!\n“);
????????close(clientSock);
????????return;
????}
????int?len=fread(file.content1900fileStream);
????file.attrib.size=len;
????Message?msg;
????msg.msgType=UPLOADFILE;
????memcpy(msg.date&filesizeof(FileItem));
????memcpy(gMsgBuf&msgsizeof(msg));
????if(send(clientSockgMsgBufBUFSIZE0)<0)
????????perror(“send“);
????fclose(fileStream);
????close(clientSock);
????gFileList.push_back(file.attrib);
}
void?DownloadFile()
{
????RefreshList();
????ConnectServer();
????cout<<“\t\tPlease?input?the?order?of?file?you?want?to?download:“;
????int?fileOrder;
????cin>>fileOrder;
????if(fileOrder>gFileList.size()||fileOrder<=0)
????{
????????cout<<“\n\t\tInput?illegal!“<
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1395??2012-06-30?17:02??Client代碼\Global.h
?????文件???????5806??2012-06-30?17:02??Client代碼\main.cpp
?????文件???????1461??2012-06-30?17:02??Server代碼\Global.h
?????文件??????14972??2012-06-30?17:02??Server代碼\main.cpp
?????目錄??????????0??2012-06-30?17:03??Client代碼
?????目錄??????????0??2012-06-30?17:03??Server代碼
-----------?---------??----------?-----??----
????????????????23634????????????????????6
評論
共有 條評論