Linux网络编程之使用TCP传输文件
2019-07-13 09:24发布
生成海报
原创作品,转载时请务必以超链接形式标明文章原始出处:http://blog.csdn.net/gqb666/article/details/8976263,作者:gqb666
最近在写Linux网络方面的demo,用TCP实现了一个简单的传输文件程序,适用于网卡设备及TCP/IP协议栈及网络环境测试时使用。用法容易,代码简洁,已经测试成功。其功能是使用TCP协议从client端向server端传输文件,用法如下:
编译:
client:gcc -o client client.c
server:gcc -o server server.c
运行:
client端:./client <端口号> <上传文件名>
server端:./server <端口号> <保存为文件名>
其中,server端先运行,client端与server端的端口号必须一致并且不能与已知端口冲突(例如8888即可)
下面将代码贴上:(使用UDP传输文件请参阅博文Linux网络编程之使用UDP传输文件)
server端代码:server.c
[cpp] view plain copy print?- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- #define MAXLINE 1024
-
- void usage(char *command)
- {
- printf(”usage :%s portnum filename
”, command);
- exit(0);
- }
- int main(int argc,char **argv)
- {
- struct sockaddr_in serv_addr;
- struct sockaddr_in clie_addr;
- char buf[MAXLINE];
- int sock_id;
- int link_id;
- int recv_len;
- int write_leng;
- int clie_addr_len;
- FILE *fp;
-
- if (argc != 3) {
- usage(argv[0]);
- }
- if ((fp = fopen(argv[2], “w”)) == NULL) {
- perror(”Open file failed
”);
- exit(0);
- }
- if ((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- perror(”Create socket failed
”);
- exit(0);
- }
-
- memset(&serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(atoi(argv[1]));
- serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
-
- if (bind(sock_id, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 ) {
- perror(”Bind socket failed
”);
- exit(0);
- }
-
- if (-1 == listen(sock_id, 10)) {
- perror(”Listen socket failed
”);
- exit(0);
- }
-
- while (1) {
- clie_addr_len = sizeof(clie_addr);
- link_id = accept(sock_id, (struct sockaddr *)&clie_addr, &clie_addr_len);
- if (-1 == link_id) {
- perror(”Accept socket failed
”);
- exit(0);
- }
- bzero(buf, MAXLINE);
- while (recv_len = recv(link_id, buf, MAXLINE, 0)) {
-
- if(recv_len < 0) {
- printf(”Recieve Data From Server Failed!
”);
- break;
- }
- printf(”#”);
- write_leng = fwrite(buf, sizeof(char), recv_len, fp);
- if (write_leng < recv_len) {
- printf(”Write file failed
”);
- break;
- }
- bzero(buf,MAXLINE);
- }
- printf(”
Finish Recieve
”);
- fclose(fp);
- close(link_id);
- }
- close(sock_id);
- return 0;
- }
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 1024
void usage(char *command)
{
printf("usage :%s portnum filename
", command);
exit(0);
}
int main(int argc,char **argv)
{
struct sockaddr_in serv_addr;
struct sockaddr_in clie_addr;
char buf[MAXLINE];
int sock_id;
int link_id;
int recv_len;
int write_leng;
int clie_addr_len;
FILE *fp;
if (argc != 3) {
usage(argv[0]);
}
if ((fp = fopen(argv[2], "w")) == NULL) {
perror("Open file failed
");
exit(0);
}
if ((sock_id = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Create socket failed
");
exit(0);
}
/*fill the server sockaddr_in struct commented by guoqingbo*/
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[1]));
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock_id, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0 ) {
perror("Bind socket failed
");
exit(0);
}
if (-1 == listen(sock_id, 10)) {
perror("Listen socket failed
");
exit(0);
}
/* server part commented by guoqingbo*/
while (1) {
clie_addr_len = sizeof(clie_addr);
link_id = accept(sock_id, (struct sockaddr *)&clie_addr, &clie_addr_len);
if (-1 == link_id) {
perror("Accept socket failed
");
exit(0);
}
bzero(buf, MAXLINE);
while (recv_len = recv(link_id, buf, MAXLINE, 0)) {
/* receiver data part commented by guoqingbo*/
if(recv_len < 0) {
printf("Recieve Data From Server Failed!
");
break;
}
printf("#");
write_leng = fwrite(buf, sizeof(char), recv_len, fp);
if (write_leng < recv_len) {
printf("Write file failed
");
break;
}
bzero(buf,MAXLINE);
}
printf("
Finish Recieve
");
fclose(fp);
close(link_id);
}
close(sock_id);
return 0;
}
client端代码:client.c
[html] view plain copy print?- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
-
- #define MAXLINE 1024
-
- void usage(char *command)
- {
- printf(“usage :%s ipaddr portnum filename
”, command);
- exit(0);
- }
- int main(int argc,char **argv)
- {
- struct sockaddr_in serv_addr;
- char buf[MAXLINE];
- int sock_id;
- int read_len;
- int send_len;
- FILE *fp;
- int i_ret;
-
- if (argc != 4) {
- usage(argv[0]);
- }
-
- /* open the file to be transported commented by guoqingbo*/
- if ((fp = fopen(argv[3],”r”)) == NULL) {
- perror(“Open file failed
”);
- exit(0);
- }
-
- /* create the socket commented by guoqingbo*/
- if ((sock_id = socket(AF_INET,SOCK_STREAM,0)) < 0) {
- perror(“Create socket failed
”);
- exit(0);
- }
-
- memset(&serv_addr, 0, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(atoi(argv[2]));
- inet_pton(AF_INET, argv[1], &serv_addr.sin_addr);
-
- /* connect the server commented by guoqingbo*/
- i_ret = connect(sock_id, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
- if (-1 == i_ret) {
- printf(“Connect socket failed
”);
- return -1;
- }
-
- /* transported the file commented by guoqingbo*/
- bzero(buf, MAXLINE);
- while ((read_len = fread(buf, sizeof(char), MAXLINE, fp)) >0 ) {
- send_len = send(sock_id, buf, read_len, 0);
- if ( send_len < 0 ) {
- perror(“Send file failed
”);
- exit(0);
- }
- bzero(buf, MAXLINE);
- }
-
- fclose(fp);
- close(sock_id);
- printf(“Send Finish
”);
- return 0;
- }
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 1024
void usage(char *command)
{
printf("usage :%s ipaddr portnum filename
", command);
exit(0);
}
int main(int argc,char **argv)
{
struct sockaddr_in serv_addr;
char buf[MAXLINE];
int sock_id;
int read_len;
int send_len;
FILE *fp;
int i_ret;
if (argc != 4) {
usage(argv[0]);
}
/* open the file to be transported commented by guoqingbo*/
if ((fp = fopen(argv[3],"r")) == NULL) {
perror("Open file failed
");
exit(0);
}
/* create the socket commented by guoqingbo*/
if ((sock_id = socket(AF_INET,SOCK_STREAM,0)) < 0) {
perror("Create socket failed
");
exit(0);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[2]));
inet_pton(AF_INET, argv[1], &serv_addr.sin_addr);
/* connect the server commented by guoqingbo*/
i_ret = connect(sock_id, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr));
if (-1 == i_ret) {
printf("Connect socket failed
");
return -1;
}
/* transported the file commented by guoqingbo*/
bzero(buf, MAXLINE);
while ((read_len = fread(buf, sizeof(char), MAXLINE, fp)) >0 ) {
send_len = send(sock_id, buf, read_len, 0);
if ( send_len < 0 ) {
perror("Send file failed
");
exit(0);
}
bzero(buf, MAXLINE);
}
fclose(fp);
close(sock_id);
printf("Send Finish
");
return 0;
}
下转博文:Linux网络编程之使用UDP传输文件
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮