socket以C语音实现linux服务端和windows客户端任意文件传输
时间: 2023-06-20 22:07:44 浏览: 193
socket TCP实现windows和linux之间文件传输.zip
5星 · 资源好评率100%
以下是一个简单的示例代码,用于在Linux上实现服务端,Windows上实现客户端的文件传输。需要注意的是,这个示例代码并不完善,实际使用中可能需要进行更多的错误处理和安全性考虑。
服务端代码(Linux):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
exit(1);
}
// 创建socket
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("socket");
exit(1);
}
// 绑定地址和端口
struct sockaddr_in server_addr = {0};
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(atoi(argv[1]));
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
exit(1);
}
// 监听端口
if (listen(server_fd, 10) == -1) {
perror("listen");
exit(1);
}
printf("Waiting for connection...\n");
// 接受连接
struct sockaddr_in client_addr = {0};
socklen_t client_len = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
if (client_fd == -1) {
perror("accept");
exit(1);
}
printf("Connected!\n");
// 接收文件名
char filename[256];
ssize_t recv_len = recv(client_fd, filename, sizeof(filename), 0);
if (recv_len <= 0) {
perror("recv");
exit(1);
}
// 打开文件
FILE *fp = fopen(filename, "wb");
if (fp == NULL) {
perror("fopen");
exit(1);
}
// 接收文件内容
char buf[1024];
while ((recv_len = recv(client_fd, buf, sizeof(buf), 0)) > 0) {
if (fwrite(buf, 1, recv_len, fp) != recv_len) {
perror("fwrite");
exit(1);
}
}
if (recv_len == -1) {
perror("recv");
exit(1);
}
printf("File received: %s\n", filename);
// 关闭文件和socket
fclose(fp);
close(client_fd);
close(server_fd);
return 0;
}
```
客户端代码(Windows):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <server_ip> <server_port> <filename>\n", argv[0]);
exit(1);
}
// 初始化Winsock库
WSADATA wsa_data;
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0) {
perror("WSAStartup");
exit(1);
}
// 创建socket
SOCKET client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client_socket == INVALID_SOCKET) {
perror("socket");
exit(1);
}
// 连接到服务端
struct sockaddr_in server_addr = {0};
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_addr.sin_port = htons(atoi(argv[2]));
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
perror("connect");
exit(1);
}
printf("Connected!\n");
// 发送文件名
if (send(client_socket, argv[3], strlen(argv[3]), 0) == SOCKET_ERROR) {
perror("send");
exit(1);
}
// 打开文件
FILE *fp = fopen(argv[3], "rb");
if (fp == NULL) {
perror("fopen");
exit(1);
}
// 发送文件内容
char buf[1024];
size_t read_len;
while ((read_len = fread(buf, 1, sizeof(buf), fp)) > 0) {
if (send(client_socket, buf, read_len, 0) == SOCKET_ERROR) {
perror("send");
exit(1);
}
}
printf("File sent: %s\n", argv[3]);
// 关闭文件和socket
fclose(fp);
closesocket(client_socket);
WSACleanup();
return 0;
}
```
阅读全文