server用於接收來自client傳送的檔案。server和client位於同乙個區域網內就可以進行通訊。
檔案讀寫必須用二進位制方式。有client連線時,開始接收資料,儲存為檔案。
#include
#include
#include
#include
#include
#include
using
namespace
std;
using
std::ofstream;
using
std::string;
using
std::thread;
#pragma comment(lib, "ws2_32.lib")
const
int bufsize = 1024; //緩衝大小 1024位元組
int myrecv(socket sockconn) // 收到客戶端訊息後儲存檔案
; // filename
int ret = recv(sockconn, buffer, bufsize, 0);
if (ret == -1)
#define _cpp
#ifdef _cpp
string filename(buffer);
cout
<< ", \""
<< filename << "\"";
// recvive file size
recv(sockconn, buffer, bufsize, 0);
double count = atof(buffer);
cout
<< ", size = "
<< count/1024
<< "kb ";
// recvive file content
ofstream of(filename, ofstream::binary);
memset(buffer, 0, bufsize);
int sum = 0;
while ((ret = recv(sockconn, buffer, bufsize, 0)) > 0) // 反覆接收二進位制資料塊
of.close();
#else
file * fd = fopen(buffer, "wb");
memset(buffer, 0, bufsize);
while ((ret = recv(sockconn, buffer, bufsize, 0)) > 0)
fclose(fd);
#endif
cout
<< " received done!"
<< endl;
return0;}
int main(int argc, char * argv)
wsadata wsadata;
int err;
err = wsastartup(makeword(2, 2), &wsadata);
if (err != 0)
if (lobyte(wsadata.wversion) != 2 || hibyte(wsadata.wversion) != 2)
socket socksrv = socket(af_inet, sock_stream, 0);
if (socksrv == invalid_socket)
cerr
<< "error: socket unsuccessful"
<< endl;
sockaddr_in addrsrv;
addrsrv.sin_addr.s_un.s_addr = htonl(inaddr_any);
addrsrv.sin_family = af_inet;
addrsrv.sin_port = htons(port);
///!!!!!!! 操蛋, 這裡加::全域性空間。。。否則客戶端死活連線不上。。。
err = ::bind(socksrv, (sockaddr *)&addrsrv, sizeof(addrsrv));
if (err != 0)
err = listen(socksrv, 5); // 監聽
if (err != 0)
//std::cout << "socket ptr: " << socksrv << endl;
sockaddr_in addrclient;
int len = sizeof(sockaddr);
cout
<< "wait for conneting..."
<< endl;
while (true)
return
0;}
給定server端的ip和port,建立socket連線後傳送檔案。
#include
#include
#include
//#include
#include
#include
using
namespace
std;
#pragma comment(lib, "ws2_32.lib")
const
int bufsize = 1024; //緩衝大小 1024位元組
int myclient(int port, char * filename1, char * ip)
cout
<< "file name: "
<< tmp << endl;
char filename_buf[255] = ; // 檔名 用於傳送
strcpy(filename_buf, tmp.c_str());
wsadata wsadata;
int err;
err = wsastartup(makeword(2, 2), &wsadata);
if (err != 0)
return -1;
if (lobyte(wsadata.wversion) != 2 || hibyte(wsadata.wversion) != 2)
socket sockclient = socket(af_inet, sock_stream, 0);
if (sockclient == invalid_socket)
sockaddr_in addrsrv;
addrsrv.sin_addr.s_un.s_addr = inet_addr(ip);
addrsrv.sin_family = af_inet;
addrsrv.sin_port = htons(port);
//std::cout << "socket ptr: " << sockclient << endl;
int ret = connect(sockclient, (sockaddr*)&addrsrv, sizeof(addrsrv)); // 建立連線
if(ret != 0 )
// send filename
ret = send(sockclient, filename_buf, strlen(filename_buf) + 1, 0);
if (ret == -1)
// send content
#define _cpp
#ifdef _cpp
string filename(filename1);
ifstream fd(filename, ifstream::binary);
fd.seekg(0, ifstream::end);
// file size
sleep(100);
char buf[bufsize] = ;
double filesize = fd.tellg();
sprintf(buf, "%lf", filesize);
send(sockclient, buf, strlen(buf), 0); // send file size
fd.seekg(0);
memset(buf, 0, bufsize);
sleep(500);
while (fd.good())
memset(buf, 0, bufsize);
}fd.close();
#else
file * fd = fopen(filename_buf, "rb");
sleep(500);
char buf[bufsize] = ;
while (true)
memset(buf, 0, bufsize);
}fclose(fd);
#endif
cout
<< "send "
<< filename1 << " done."
<< endl;
return0;}
int main(int argc, char * argv)
//argv[1] = r"(c:\users\wanggao\desktop\新建文字文件.txt)";
//argv[2] = "192.168.3.113";
int port = 6000; // 與server相同
char * filename; // 檔案
char * server_ip; // server位址
filename = argv[1];
server_ip = argv[2];
return myclient(port, filename, server_ip);
}
區域網傳輸大檔案的方案
最近新買乙個筆記本,需要把舊筆記本的資料拷貝過來,發現這也是個大問題啊。先後試過如下方法 1 windows檔案分享。速度貌似不是很快,放棄了。2 u盤拷貝,拷到什麼時候,放棄。3 feiq區域網傳輸檔案,用的wifi發現速度也一般,2mb s忍不了。4 ftp,配置很複雜,乙個配置沒弄好就老是報錯...
socket的區域網廣播
很多區域網都定義了乙個特殊的保留位址,稱為廣播位址.當資訊頭中目的位址域的內容為廣播位址時,該幀被區域網上所有計算機接收.這個過程稱為廣播.合法的廣播位址就是主機的網路位址加上最大的主機號。網路位址通過機器的ip位址與子網掩碼按位與得出,我們 以ip位址為10.78.202.175 子網掩碼為255...
socket的區域網廣播
很多區域網都定義了乙個特殊的保留位址,稱為廣播位址.當資訊頭中目的位址域的內容為廣播位址時,該幀被區域網上所有計算機接收.這個過程稱為廣播.合法的廣播位址就是主機的網路位址加上最大的主機號。網路位址通過機器的ip位址與子網掩碼按位與得出,我們 以ip位址為10.78.202.175 子網掩碼為255...