TCP實現檔案傳輸

2021-10-09 11:01:25 字數 4532 閱讀 1523

一直想著給之前的clouddisk專案加上乙個c/s架構的檔案傳輸模組,因為之前是nginx+fastcgi架構的b/s架構,自己又不會前段**,沒有辦法繼續增加新的功能塊。最近終於抽出時間開始寫專案了,已經選用tcp完成linux下的cs架構檔案上傳功能模組,這裡展示tcp檔案傳輸模組。

socket類裡僅僅封裝了tcp連線所需的api和一些必要變數。

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

"file.h"

#include

"logger.h"

#define buffer_size 1024

#define port 8888

#define max_connect 20

using

namespace std;

class

socket

bool

sockinitserver()

;bool

sockinitclient()

;bool

sockbind()

;bool

socklisten()

;int

sockconnect()

;int

sockaccpet()

;int

sockrecv

(int recvfd,

char buf,

int len)

;int

socksend

(int sendfd,

const

char buf,

int len)

;bool

sockclose()

;int m_sockfd;

int recvlen;

int sendlen;

struct sockaddr_in m_sockaddr;};

#include

"socket.h"

socket::

socket()

else

}bool socket::

sockinitserver()

bool socket::

sockinitclient()

bool socket::

sockbind()

else

return

true;}

bool socket::

socklisten()

else

return

true;}

int socket::

sockconnect()

else

return sendfd;

}int socket::

sockaccpet()

else

return recvfd;

}int socket::

sockrecv

(int recvfd,

char buf,

int len)

else

return recvlen;

}int socket::

socksend

(int sendfd,

const

char buf,

int len)

else

return sendlen;

}bool socket::

sockclose()

server類裡面完成了對檔案資料的接收解析,拿到檔案的名字並建立檔案,然後將其傳給fdfs模組。這個類後續還會做很多優化,以及加上檔案操作的其他功能模組。

#include

"socket.h"

#include

"uploadfile.h"

class

server

~server()

bool

serverinit()

;bool

serveraccept()

;bool

getfilename()

;bool

writefile

(int recvfd,

int fd);}

;#include

"server.h"

bool server::

serverinit()

bool server::

serveraccept()

else

cout <<

"filename: "

<< file.filename << endl;

int fd =

open

(file.filename, o_creat | o_rdwr,

0664);

if(fd <0)

else

return

true;}

bool server::

getfilename()

else

}strcpy

(file.filename, file.filepath +

(strlen

(file.filepath)

- k)+1

);return

true;}

bool server::

writefile

(int recvfd,

int fd)

write

(fd, file.filedata, socket.recvlen);if

(socket.recvlen < buffer_size)

else

cout <<

"continue..."

<< endl;

memset

(file.filedata,0,

sizeof

(file.filedata));

} cout <<

"recv finished."

<< endl;

close

(fd)

;return

true;}

intmain()

if(server.

serverinit()

)else

while

(true

)else

} server.socket.

sockclose()

;return0;

}

client主要用來傳輸檔案,後面將作為介面程式的介面,會增加其他的功能模組。

#include

"socket.h"

class

client

~client()

bool

clientinit()

;bool

clientsend()

;bool

sendfilepath()

;bool

sendfiledata();};

#include

"client.h"

bool client::

clientinit()

bool client::

clientsend()

sendfiledata()

;return

true;}

bool client::

sendfilepath()

else

sendlen = socket.

socksend

(socket.m_sockfd, file.filepath,

100);if

(sendlen <0)

else

return

true;}

bool client::

sendfiledata()

else

if(sendlen == buffer_size)

else

}else

if(sendlen < buffer_size)

else

break;}

memset

(file.filedata,

0, buffer_size);}

close

(fd)

;return

true;}

intmain()

else

while

(true

)else

} client.socket.

sockclose()

;return0;

}

Python實現TCP檔案傳輸

廢話少說,直接上 1 傳送端 2import socket,os 3from struct import pack45 def send file file name,file socket socket.socket 6try 7 f open file name,rb 8 size os.pat...

tcp檔案傳輸

include include include include include include include include define buffer size 100 1024 1024 define file name max size 512 intmain int argc,char a...

TCP檔案傳輸(一)

最近在做網路抓包和檔案傳輸,今天遇到乙個奇怪的現象,自己查了半天也沒搞清楚,導致花了乙個下午時間就只實現了檔名和檔案長度的傳送,首先給出檔案傳輸的客戶端和伺服器端 一 客戶端 1 winsock com元件初始化和釋放 include stdafx.h ifndef winsock startup ...