TCP IP程式設計實現遠端檔案傳輸

2021-06-27 10:38:38 字數 1862 閱讀 3712

tcp/ip程式設計實現遠端檔案傳輸

在tcp/ip網路結構中,為了保證網路安全,網路人員往往需要在路由器上新增防火牆,禁止非法使用者用ftp等安全危害較大的tcp/ip協議訪問主機

網路環境搭建好後,我們可以在伺服器端編寫fwq.c程式,負責接受客戶機的連線請求,並將從原始檔中讀取的資料傳送到客戶機客戶機程式khj.c向伺服器傳送連線請求,接收從伺服器端發來的資料,並將接收到的資料寫入目標檔案源程式如下:

/*伺服器源程式fwq.c*/

#include

#include

#include

#include

#include

#include

#include

main()

printf(″\n\t\t在傳送檔案,稍候…″);

hp=gethostbyname(″server″);

if (hp==null)

s=socket(af_inet,sock_stream,0);

if(s<0)

sin.sin_family=af_inet;

sin.sin_port=htons(1500);/*使用埠1500*/

bcopy(hp->h_addr,&sin.sin_addr,hp->h_length);

if(bind(s,&sin,sizeof(sin))<0)

if(listen(s,5)<0

while(1)

lseek(source,ol,0);/*每次接受客戶機連線,應將用於讀的原始檔指標移到檔案頭*/

write(ns,file,sizeof(file)); /*傳送檔名*/

while((k=read(source,buf,sizeof(buf)))>0)

write(ns,buf,k);

printf(″\n\n\t\t傳輸完畢!!!\n″);

close(ns);

}close(source);

exit(0);

/*客戶機源程式khj.c*/

#include

#include

#include

#include

#include

#include

#include

#include

main()

s=socket(af_inet,sock_stream,0);

if(s<0)

sin.sin_family=af_inet;

sin.sin_port=htons(1500);/*埠號需與伺服器程式使用的一致*/

bcopy(hp->h_addr,&sin.sin_addr,hp->h_length);

printf(″\n\n\t\t正在與伺服器連線…″);

if(connect(s,&sin,sizeof(sin),0)<0)

while((k=read(s,file,sizeof(file)))<=0/*接收檔名*/

if((target=open(file,o_wronly|o_creat|o_trunc,0644))<0)

strcat(strs,file);

strcat(strs,″,稍候…″);

write(1,strs,strlen(strs));

while((k=read(s,buf,sizeof(buf)))>0)

write(tatget,buf,k);

printf(″\n\n\t\t接收檔案成功!!!\n″);

close(s);

close(target);

}上述程式在sco unix system v3.2及sco tcp/ip rumtime環境下除錯通過

TCP IP程式設計實現遠端檔案傳輸

tcp ip程式設計實現遠端檔案傳輸在lunix中一般都採用套接字 socket 系統呼叫。採用客戶 伺服器模式,其程式編寫步驟如下 include sys types.h include sys socket.h int socket int family,int type,int protoco...

基於TCP IP協議實現檔案傳輸

public class fileserver extends thread override public void run 將map集合傳送到客戶端 oos.writeobject map oos.flush 2.接收客戶端傳送的檔案序號 scanner sc new scanner s.get...

Linux遠端檔案傳輸

linux系統中,難免會遇到一些要將某檔案通過網路傳送給其他主機的情況,而恰好兩台主機 都是linux系統的時候,我們就可以直接使用scp命令來傳輸檔案到另一台主機了。scp命令用於在網路中安全的傳輸檔案,格式為 例項1 將本地檔案123.txt傳輸到遠端主機192.168.157.128的目錄 h...