写完了一个简单的聊天程序,我想如果我们要发送一个文件给对方的话,应该也可以用这个解决。其实我感觉TCP服务器端依次调用socket()、bind()、listen()之后,就会监听指定的socket地址了。TCP客户端依次调用socket()、connect()之后就向TCP服务器发送了一个连接请求。TCP服务器监听到这个请求之后,就会调用accept()函数去接收请求,这样连接就建立好了。之后就可以开始网络I/O操作了,即类同于普通文件的读写I/O操作。如果我们要实现文本信息传输的话只需要用send()和recv()函数就可以实现了。如果要实现文件传输的话只需从文件中读取出来在用send()发送,然后recv()接受的话再存到文件里就OK了。 下面是实现的是客户端向服务端发送一个文件名,然后服务端接受到这个名字之后将该文件传送给客户端:
- #include<netinet/in.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
-
- #define PORT 5552
- #define LISTEN 20
- #define BUFFER_SIZE 1024
- #define MAX_SIZE 512
-
- int main(int argc, char **argv)
- {
- // 设置一个socket地址结构server_addr,代表服务器internet的地址和端口
- struct sockaddr_in server_addr;
- bzero(&server_addr, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- server_addr.sin_addr.s_addr = htons(INADDR_ANY);
- server_addr.sin_port = htons(PORT);
-
- // 创建用于internet的流协议(TCP)socket,用server_socket代表服务器向客户端提供服务的接口
- int server_socket = socket(PF_INET, SOCK_STREAM, 0);
- if (server_socket < 0)
- {
- printf("Create Socket Failed!\n");
- exit(1);
- }
-
- // 把socket和socket地址结构绑定
- if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)))
- {
- printf("Server Bind Port: %d Failed!\n",PORT);
- exit(1);
- }
-
- // server_socket用于监听
- if (listen(server_socket,LISTEN))
- {
- printf("Server Listen Failed!\n");
- exit(1);
- }
-
- // 服务器端一直运行用以持续为客户端提供服务
- while(1)
- {
- struct sockaddr_in client_addr;
- socklen_t length = sizeof(client_addr);
- // accpet返回一个新的socket,这个socket用来与此次连接到server的client进行通信
- int new_server_socket = accept(server_socket, (struct sockaddr*)&client_addr, &length);
- if (new_server_socket < 0)
- {
- printf("Server Accept Failed!\n");
- break;
- }
-
- char buffer[BUFFER_SIZE];
- memset(buffer,0,sizeof(buffer));
- length = recv(new_server_socket, buffer, BUFFER_SIZE, 0);
- if (length < 0)
- {
- printf("Server Recieve Data Failed!\n");
- break;
- }
-
- char file_name[MAX_SIZE + 1];
- memset(file_name,0,sizeof(file_name));
- strncpy(file_name, buffer,
- strlen(buffer) > MAX_SIZE ? MAX_SIZE : strlen(buffer));
-
- FILE *fp = fopen(file_name, "r");
- if (fp == NULL)
- {
- printf("File:\t%s Not Found!\n", file_name);
- }
- else
- {
- memset(buffer,0, BUFFER_SIZE);
- int file_block_length = 0;
- while( (file_block_length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
- {
- printf("file_block_length = %d\n", file_block_length);
-
- // 发送buffer中的字符串到new_server_socket,实际上就是发送给客户端
- if (send(new_server_socket, buffer, file_block_length, 0) < 0)
- {
- printf("Send File:\t%s Failed!\n", file_name);
- break;
- }
-
- memset(buffer,0,sizeof(buffer));
- }
- fclose(fp);
- printf("File:\t%s Transfer Finished!\n", file_name);
- }
-
- close(new_server_socket);
- }
-
- close(server_socket);
-
- return 0;
- }
- #include<netinet/in.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
-
- #define PORT 5552
- #define BUFFER_SIZE 1024
- #define MAX_SIZE 512
-
- int main(int argc, char **argv)
- {
- if (argc != 2)
- {
- printf("IP error\n");
- exit(1);
- }
- // 创建用于internet的流协议(TCP)类型socket,用client_socket代表客户端socket
- int client_socket = socket(AF_INET, SOCK_STREAM, 0);
- if (client_socket < 0)
- {
- printf("Create Socket Failed!\n");
- exit(1);
- }
- // 设置一个socket地址结构server_addr,代表服务器的internet地址和端口
- struct sockaddr_in server_addr;
- memset(&server_addr,0, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
-
- // 服务器的IP地址来自程序的参数
- if (inet_aton(argv[1], &server_addr.sin_addr) == 0)
- {
- printf("Server IP Address Error!\n");
- exit(1);
- }
-
- server_addr.sin_port = htons(PORT);
- socklen_t server_addr_length = sizeof(server_addr);
-
- // 向服务器发起连接请求,连接成功后client_socket代表客户端和服务器端的一个socket连接
- if (connect(client_socket, (struct sockaddr*)&server_addr, server_addr_length) < 0)
- {
- printf("Can Not Connect To %s!\n", argv[1]);
- exit(1);
- }
-
- char file_name[MAX_SIZE + 1];
- memset(file_name,0, sizeof(file_name));
- printf("Please Input T he File Name On Server.\t");
- scanf("%s", file_name);
-
- char buffer[BUFFER_SIZE];
- memset(buffer,0, sizeof(buffer));
- strncpy(buffer, file_name, strlen(file_name) > BUFFER_SIZE ? BUFFER_SIZE : strlen(file_name));
- // 向服务器发送buffer中的数据,此时buffer中存放的是客户端需要接收的文件的名字
- send(client_socket, buffer, BUFFER_SIZE, 0);
-
- FILE *fp = fopen(file_name, "w");
- if (fp == NULL)
- {
- printf("File:\t%s Can Not Open To Write!\n", file_name);
- exit(1);
- }
-
- // 从服务器端接收数据到buffer中
- memset(buffer,0, sizeof(buffer));
- int length = 0;
- while(length = recv(client_socket, buffer, BUFFER_SIZE, 0))
- {
- if (length < 0)
- {
- printf("Recieve Data From Server %s Failed!\n", argv[1]);
- break;
- }
-
- int write_length = fwrite(buffer, sizeof(char), length, fp);
- if (write_length < length)
- {
- printf("File:\t%s Write Failed!\n", file_name);
- break;
- }
- memset(buffer,0, BUFFER_SIZE);
- }
-
- printf("Recieve File:\t %s From Server[%s] Finished!\n", file_name, argv[1]);
-
- // 传输完毕,关闭socket
- fclose(fp);
- close(client_socket);
- return 0;
-
- }
运行之后的截图:- lwp@lwp-linux:~/Desktop$ ./cli 192.168.200.180
- Please Input T he File Name On Server. data
- Recieve File: liwenping From Server[192.168.200.180] Finished!
- lwp@lwp-linux:~/Desktop$ ./ser
- File: data Transfer
这样我们就将服务端的data文件传输到了客户端。
阅读(1070) | 评论(0) | 转发(0) |