tcp檔案傳輸

2021-10-09 16:56:44 字數 4841 閱讀 6911

#include

#include

#include

#include

#include

#include

#include

#include

#define buffer_size 100*1024*1024

#define file_name_max_size 512

intmain

(int argc,

char

*ar**)

int reuse =1;

if(setsockopt

(socketfd_listen, sol_socket, so_reuseaddr,

(void*)

&reuse,

sizeof

(reuse)

)<0)

//構造繫結的位址

bzero

(&server_addr,

sizeof

(struct sockaddr_in));

server_addr.sin_family=af_inet;

server_addr.sin_addr.s_addr=

inet_addr

("192.168.14.252");

server_addr.sin_port=

htons

(portnum)

;//繫結伺服器位址if(

bind

(socketfd_listen,

(struct sockaddr *)(

&server_addr)

,sizeof

(struct sockaddr)

)<0)

printf

("bind success!\n");

//開啟監聽if(

listen

(socketfd_listen,5)

<0)

//等待連線請求

len=

sizeof

(struct sockaddr)

; socketfd_connect =

accept

(socketfd_listen,

(struct sockaddr *)(

&client_addr)

,&len);if

(socketfd_connect<0)

char

*buffer =

(char*)

malloc

(sizeof

(char

)*buffer_size)

;while(1

)char fn[

1000]=

;printf

("%s\n"

, filename)

;sprintf

(fn,

"/home/gec/download/%s"

, filename)

;printf

(" %s\n"

, fn)

;//建立同名空檔案(如果原本存在,就以覆蓋方式重寫)

fp =

fopen

(fn,

"w");if

(null

== fp )

//接收檔案長度

char msg_buff[

2048]=

; ssize_t msg_len =0;

msg_len =

recv

(socketfd_connect, msg_buff,

2048,0

);//轉換收到的檔案大小為整形

int file_size =0;

sscanf

(msg_buff,

"%d\n"

,&file_size)

;printf

("file_size = %d\n"

, file_size)

;int recvlen =0;

//接收到的實際資料長度

//迴圈接收檔案

while

( length =

recv

(socketfd_connect, buffer, buffer_size,0)

)//將接收到的資料寫入檔案

write_length =

fwrite

(buffer,

sizeof

(char

),length,fp);if

(write_lengthbzero

(buffer,buffer_size)

;//檔案接收完成

recvlen +

= length;

if(recvlen >= file_size)

break;}

printf

("recieve file: %s from client finished\n"

,filename)

;fclose

(fp);}

//關閉套接字

close

(socketfd_listen)

;close

(socketfd_connect)

;return0;

}

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define buffer_size 100*1024*1024

#define file_name_max_size 512

intmain

(int argc,

char

*ar**)

//構造伺服器位址

bzero

(&s_add,

sizeof

(struct sockaddr_in)

);

s_add.sin_family = af_inet;

s_add.sin_addr.s_addr =

inet_addr

("192.168.63.130");

s_add.sin_port=

htons

(portnum)

;//傳送連線伺服器請求if(

connect

(socketfd,

(struct sockaddr *)(

&s_add)

,sizeof

(struct sockaddr)

)<0)

printf

("connect success!\n");

while(1

)char filename[

1000];

memset

(filename,

'\0'

,sizeof

(filename));

int i=

0,k=0;

for(i=

strlen

(filepath)

;i>=

0;i--

)else

break;}

strcpy

(filename,filepath+

(strlen

(filepath)

-k)+1)

;printf

("filename is %s \n"

,filename)

;//傳送檔名

len=

send

(socketfd,filename,

strlen

(filename),0

);if(len<0)

else

//傳送檔案大小

printf

("**************************************\n");

// sleep(2);

int file_size =0;

fseek

(fp,0,

seek_end);

//將檔案指標偏移到檔案末尾

file_size =

ftell

(fp)

;//獲取當前的偏移量(偏移量就是檔案大小)

fseek

(fp,0,

seek_set);

//將檔案指標偏移到檔案開頭

char send_buff[

2048]=

;int len =0;

sprintf

(send_buff,

"%d"

, file_size)

; len =

send

(socketfd,send_buff,

2048,0

);// sleep(2);

//傳送檔案到伺服器

if(fp ==

null

)else

bzero

(buffer,

sizeof

(buffer));

}fclose

(fp)

;printf

("file:file transfer finished!\n");

}}//關閉套接字

close

(socketfd)

;return0;

}

TCP檔案傳輸(一)

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

TCP實現檔案傳輸

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

基於TCP的檔案傳輸

伺服器端處理客戶請求 public class serverthread extends thread override public void run file file new file directory.getabsolutepath file.separatorchar filename ...