winSock 實現檔案的傳輸

2021-06-17 18:07:43 字數 2362 閱讀 9007

簡單的c/s架構的檔案傳輸,使用的是阻塞模式,下次嘗試非阻塞的和非同步模式的。ps.最近在看客戶端

#include "stdafx.h"

#include #pragma comment(lib, "ws2_32.lib")

int main()

socket hsocket;

hsocket = socket(af_inet,sock_stream,ipproto_tcp);

if (hsocket == invalid_socket )

sockaddr_in addsend;

addsend.sin_family = af_inet;

addsend.sin_port = htons(6500);

addsend.sin_addr.s_addr = inet_addr("127.0.0.1");

err = connect(hsocket,(sockaddr*)&addsend , sizeof(sockaddr));

if (err == socket_error)

wchar_t* filename =_t("員工轉正審批表.doc");

handle hfile = createfile(filename,

generic_read,

file_share_read,

0,open_existing,

file_attribute_normal,

null);

if (hfile == invalid_handle_value)

char sendbuffer[1024];

dword dwread;

bool breadfile;

while(true)

else if (dwread == 0)

else

}closehandle(hfile);

closesocket(hsocket);

wsacleanup();

return 0;

}

伺服器端

#include "stdafx.h"

#include #pragma comment(lib, "ws2_32.lib")

int _tmain(int argc, _tchar* argv)

socket hsocket = socket(af_inet,sock_stream,ipproto_tcp);

if (hsocket == invalid_socket)

sockaddr_in sockaddr;

sockaddr.sin_addr.s_un.s_addr = htonl(inaddr_any);

sockaddr.sin_family = af_inet;

sockaddr.sin_port = htons(6500);

err = bind(hsocket,(sockaddr*)&sockaddr,sizeof(sockaddr));

if (err == socket_error)

err = listen(hsocket,somaxconn);

if (err == socket_error)

sockaddr_in addrrecv;

int len = sizeof(sockaddr);

socket sockrecv = accept(hsocket,(sockaddr*)&addrrecv,&len);

if (sockrecv == invalid_socket)

wchar_t* filename =_t("員工轉正審批表.doc");

handle hfile =createfile(filename,

generic_write,

file_share_write,

0,create_new,

file_attribute_normal,

null);

if (hfile == invalid_handle_value)

char recvbuffer[1024];

dword dwwrite;

dword dwrecv;

while (true)

if (dwrecv == 0)

bool bwritefile = writefile(hfile,recvbuffer,dwrecv,&dwwrite,null);

if (bwritefile == 0)

}closehandle(hfile);

closesocket(hsocket);

wsacleanup();

return 0;

}

實現FTP自動傳輸檔案

實現ftp自動傳輸檔案 一 linux環境使用.netrc 在linux下可以使用.netrc檔案來達到自動傳輸的目的。1.在當前使用者目錄下建立.netrc檔案,內容如下 cat netrc machine 192.168.1.117 login oracle9i password oracle9...

python實現檔案傳輸

我程式設計的時候,開了兩個python的shell,乙個做server,乙個做client 然後就直接在命令提示符下一行一行的編,感覺自己是在使用一套高階命令列,而不是在程式設計。server端 import socket sersock socket.socket socket.af inet,s...

Python FTP檔案傳輸實現

ftp一般流程 ftp對應pasv和port兩種訪問方式,分別為被動和主動,是針對ftp伺服器端進行區分的,正常傳輸過程中21號埠用於指令傳輸,資料傳輸埠使用其他埠。pasv 由客戶端發起資料傳輸請求,伺服器端返回並攜帶資料埠,並且伺服器端開始監聽此埠等待資料,為被動模式 port 客戶端監聽埠並向...