网络文件传输程序源代码

时间:2021-06-08 14:06:38
【文件属性】:

文件名称:网络文件传输程序源代码

文件大小:3KB

文件格式:RAR

更新时间:2021-06-08 14:06:38

C socket

一个简单高效的网络文件传输程序,适合嵌入式系统使用。 14 鎸囧畾浣跨敤鐨勫崗璁皣:AF_INET锛圛Pv4锛?AF_INET6锛圛Pv6锛?AF_LOCAL锛圲NIX鍗忚锛?AF_ROUTE锛堣矾鐢卞鎺ュ瓧锛?AF_KEY锛堢閽ュ鎺ュ瓧锛? 15 type: 16 鎸囧畾浣跨敤鐨勫鎺ュ瓧鐨勭被鍨?SOCK_STREAM锛堝瓧鑺傛祦濂楁帴瀛楋級 SOCK_DGRAM 17 protocol: 18 濡傛灉濂楁帴瀛楃被鍨嬩笉鏄師濮嬪鎺ュ瓧锛岄偅涔堣繖涓弬鏁板氨涓? 19 2銆乮nt bind(int sockfd, struct sockaddr *myaddr, int addrlen) 20 sockfd: 21 socket鍑芥暟杩斿洖鐨勫鎺ュ瓧鎻忚堪绗? 22 myaddr: 23 鏄寚鍚戞湰鍦癐P鍦板潃鐨勭粨鏋勪綋鎸囬拡 24 myaddrlen: 25 缁撴瀯闀垮害 26 struct sockaddr{ 27 unsigned short sa_family; //閫氫俊鍗忚绫诲瀷鏃廇F_xx 28 char sa_data[14]; //14瀛楄妭鍗忚鍦板潃锛屽寘鍚socket鐨処P鍦板潃鍜岀鍙e彿 29 }; 30 struct sockaddr_in{ 31 short int sin_family; //閫氫俊鍗忚绫诲瀷鏃? 32 unsigned short int sin_port; //绔彛鍙? 33 struct in_addr sin_addr; //IP鍦板潃 34 unsigned char si_zero[8]; //濉厖0浠ヤ繚鎸佷笌sockaddr缁撴瀯鐨勯暱搴︾浉鍚? 35 }; 36 3銆乮nt connect(int sockfd,const struct sockaddr *serv_addr,socklen_t addrlen) 37 sockfd: 38 socket鍑芥暟杩斿洖濂楁帴瀛楁弿杩扮 39 serv_addr: 40 鏈嶅姟鍣↖P鍦板潃缁撴瀯鎸囬拡 41 addrlen: 42 缁撴瀯浣撴寚閽堢殑闀垮害 43 4銆乮nt listen(int sockfd, int backlog) 44 sockfd: 45 socket鍑芥暟缁戝畾bind鍚庡鎺ュ瓧鎻忚堪绗? 46 backlog: 47 璁剧疆鍙繛鎺ュ鎴风鐨勬渶澶ц繛鎺ヤ釜鏁帮紝褰撴湁澶氫釜瀹㈡埛绔悜鏈嶅姟鍣ㄨ姹傛椂锛屾敹鍒版鍊肩殑褰卞搷銆傞粯璁ゅ€?0 48 5銆乮nt accept(int sockfd,struct sockaddr *cliaddr,socklen_t *addrlen) 49 sockfd: 50 socket鍑芥暟缁忚繃listen鍚庡鎺ュ瓧鎻忚堪绗? 51 cliaddr: 52 瀹㈡埛绔鎺ュ瓧鎺ュ彛鍦板潃缁撴瀯 53 addrlen: 54 瀹㈡埛绔湴鍧€缁撴瀯闀垮害 55 6銆乮nt send(int sockfd, const void *msg,int len,int flags) 56 7銆乮nt recv(int sockfd, void *buf,int len,unsigned int flags) 57 sockfd: 58 socket鍑芥暟鐨勫鎺ュ瓧鎻忚堪绗? 59 msg: 60 鍙戦€佹暟鎹殑鎸囬拡 61 buf: 62 瀛樻斁鎺ユ敹鏁版嵁鐨勭紦鍐插尯 63 len: 64 鏁版嵁鐨勯暱搴︼紝鎶奻lags璁剧疆涓? 65 *************************************************************************************************************************/ int main(int argc, char *argv[]) { int fd,new_fd,struct_len,numbytes,total_read=0; struct sockaddr_in server_addr; struct sockaddr_in client_addr; char buff[BUFSIZ]; char fname[255]; //鐢ㄤ簬瀛樻斁鏂囦欢鍚? FILE * fp; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8000); server_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(server_addr.sin_zero), 8); struct_len = sizeof(struct sockaddr_in); fd = socket(AF_INET, SOCK_STREAM, 0); while(bind(fd, (struct sockaddr *)&server;_addr, struct_len) == -1); printf("Bind Success!\n"); while(listen(fd, 10) == -1); printf("Listening....\n");


【文件预览】:
sendfile.c
recvfile.c

网友评论