linux下的TCP IP socket 檔案傳輸

2021-04-27 19:26:09 字數 3833 閱讀 8816

//伺服器**

/////本檔案是伺服器的**

#include // for sockaddr_in

#include // for socket

#include // for socket

#include // for printf

#include // for exit

#include // for bzero

#include //for time_t and time

#define hello_world_server_port 7754

#define length_of_listen_queue 20

#define buffer_size 1024

int main(int argc, char **argv)

//把socket和socket位址結構聯絡起來

if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)))

//server_socket用於監聽

if ( listen(server_socket, length_of_listen_queue) )

while (1) //伺服器端要一直執行

char buffer[buffer_size];

bzero(buffer, buffer_size);

strcpy(buffer,"hello,world! 從伺服器來!");

strcat(buffer,"/n"); //c語言字串連線

send(new_server_socket,buffer,buffer_size,0);

bzero(buffer,buffer_size);

//接收客戶端傳送來的資訊到buffer中

length = recv(new_server_socket,buffer,buffer_size,0);

if (length < 0)

printf("/n%s",buffer);

if((stream = fopen("data1","r"))==null)

else

printf("the file 'filename' was opened! /n");

bzero(buffer,buffer_size);

int lengsize = 0;

while((lengsize = fread(buffer,1,1024,stream)) > 0)

bzero(buffer, buffer_size); }

if(fclose(stream))

printf("the file 'data' was not closed! /n");

exit(1);       

//關閉與客戶端的連線

close(new_server_socket);         }

//關閉監聽用的socket

close(server_socket);

return 0; }

//客戶端**

/////本檔案是客戶機的**

#include // for sockaddr_in

#include // for socket

#include // for socket

#include // for printf

#include // for exit

#include // for bzero

#include //for time_t and time

#include

#define hello_world_server_port    7754

#define buffer_size 1024

int main(int argc, char **argv)

//time_t now;

file *stream;

//設定乙個socket位址結構client_addr,代表客戶機internet位址, 埠

struct sockaddr_in client_addr;

bzero(&client_addr,sizeof(client_addr)); //把一段記憶體區的內容全部設定為0

client_addr.sin_family = af_inet;    //internet協議族

client_addr.sin_addr.s_addr = htons(inaddr_any);//inaddr_any表示自動獲取本機位址

client_addr.sin_port = htons(0);    //0表示讓系統自動分配乙個空閒埠

//建立用於internet的流協議(tcp)socket,用client_socket代表客戶機socket

int client_socket = socket(af_inet,sock_stream,0);

if( client_socket < 0)

//把客戶機的socket和客戶機的socket位址結構聯絡起來

if( bind(client_socket,(struct sockaddr*)&client_addr,sizeof(client_addr)))

//設定乙個socket位址結構server_addr,代表伺服器的internet位址, 埠

struct sockaddr_in server_addr;

bzero(&server_addr,sizeof(server_addr));

server_addr.sin_family = af_inet;

if(inet_aton(argv[1],&server_addr.sin_addr) == 0) //伺服器的ip位址來自程式的引數

server_addr.sin_port = htons(hello_world_server_port);

socklen_t server_addr_length = sizeof(server_addr);

//向伺服器發起連線,連線成功後client_socket代表了客戶機和伺服器的乙個socket連線

if(connect(client_socket,(struct sockaddr*)&server_addr, server_addr_length) < 0)

char buffer[buffer_size];

bzero(buffer,buffer_size);

//從伺服器接收資料到buffer中

int length = recv(client_socket,buffer,buffer_size,0);

if(length < 0)

printf("/n%s/n",buffer);

bzero(buffer,buffer_size);

bzero(buffer,buffer_size);

strcpy(buffer,"hello, world! from client/n");

//向伺服器傳送buffer中的資料

send(client_socket,buffer,buffer_size,0);

if((stream = fopen("data","w+t"))==null)

else

bzero(buffer,buffer_size);

length = 0;

while( length = recv(client_socket,buffer,buffer_size,0))

int write_length = fwrite(buffer,sizeof(char),length,stream);

if (write_length

Linux下ZendOptimizer的安裝與配置

內容 在裝的好的red hat linux 9 apache 2.0.55 mysql 5.1.6 php 5.1.2後,接下來就是安裝linux下的php加速器zendoptimizer 2.6.2了。自己編譯 php 後,zend optimizer 2.6.2 需要自己設定一下,請參照以下步驟...

Linux下的命令

管理員 普通使用者 pwd 顯示當前檔案全路徑 date 顯示系統當前日期和時間 who 顯示當前已登入的所有使用者名稱 cal 顯示日曆 uname r m,i,v 顯示當前系統作業系統資訊 wc l 行數,w字數,c位元組數 統計字數 clear 清屏 檔案操作touch 新建檔案mkdir 新...

linux 下 奇怪的

最近在做乙個從hp ux移行到linux 上面的專案,遇到乙個命令 hp ux date y m d h m s read sys date sys time dev null 2 1 但是這個命令在linux 下,就達不到預期的結果 之後問了高手,才得到解決辦法 linux read sys da...