//伺服器**
/////本檔案是伺服器的**
#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_lengthbzero(buffer,buffer_size);
}printf("recieve file from server[%s] finished\n", argv[1]);
//關閉 檔案
fclose(stream);
//關閉socket
close(client_socket);
return 0;
}
socket檔案傳輸
最近入職培訓中需要寫乙個linux下的c s網路檔案傳輸工具,在實現的過程中,遇到了一些坑,在這裡 做個總結 由於udp伺服器不需要accept 因此也沒有link id 連線的客戶端id 在伺服器檔案接收或下發檔案時需要知道當下與之通訊的客戶端的識別符號,由於udp是無連線的,並且sockfd s...
Socket檔案傳輸
服務端 輸入檔案完整路徑傳送給客戶端 import struct import json import os tcp server socket ip port 127.0.0.1 8080 buffsize 1024 埠的重複利用 tcp server.bind ip port tcp serve...
socket通訊檔案傳輸
主機傳送方 fileinfo ezonefile new fileinfo 檔案路徑 開啟檔案流 filestream ezonestream ezonefile.openread 包的大小 int packetsize 1024 包的數量 int packetcount int ezonestre...