socket以C语音实现linux服务端和windows客户端文件传输
时间: 2023-06-23 15:10:14 浏览: 170
要实现文件传输,需要在服务端和客户端之间建立一个TCP连接,可以使用socket来实现。下面是一个基本的示例代码:
服务端代码(Linux):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAXLINE 1024
int main() {
int sockfd, connfd;
struct sockaddr_in servaddr, cliaddr;
char buff[MAXLINE];
int n;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
if (bind(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) {
perror("socket bind failed");
exit(EXIT_FAILURE);
}
if (listen(sockfd, 5) != 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
while (1) {
printf("waiting for client connection...\n");
socklen_t clilen = sizeof(cliaddr);
connfd = accept(sockfd, (struct sockaddr*)&cliaddr, &clilen);
if (connfd < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
printf("client connected: %s:%d\n", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
FILE *fp = fopen("test.txt", "wb");
if (fp == NULL) {
perror("file creation failed");
exit(EXIT_FAILURE);
}
while ((n = read(connfd, buff, MAXLINE)) > 0) {
fwrite(buff, sizeof(char), n, fp);
}
if (n < 0) {
perror("error reading from socket");
exit(EXIT_FAILURE);
}
fclose(fp);
printf("file received successfully\n");
close(connfd);
}
close(sockfd);
return 0;
}
```
客户端代码(Windows):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define PORT 8888
#define MAXLINE 1024
int main() {
WSADATA wsaData;
int sockfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
int n;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
perror("WSAStartup failed");
exit(EXIT_FAILURE);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
if (connect(sockfd, (SOCKADDR*)&servaddr, sizeof(servaddr)) < 0) {
perror("connection failed");
exit(EXIT_FAILURE);
}
FILE *fp = fopen("test.txt", "rb");
if (fp == NULL) {
perror("file open failed");
exit(EXIT_FAILURE);
}
while ((n = fread(buff, sizeof(char), MAXLINE, fp)) > 0) {
if (send(sockfd, buff, n, 0) < 0) {
perror("error sending file");
exit(EXIT_FAILURE);
}
}
fclose(fp);
printf("file sent successfully\n");
closesocket(sockfd);
WSACleanup();
return 0;
}
```
这两份代码分别在Linux和Windows系统上编译运行即可实现文件传输。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)