那么你来改写这段代码,使其可以正常运行
时间: 2024-03-16 16:44:03 浏览: 18
以下是一个可能的修改方案,使用了多线程来处理客户端的输入:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#define MAXLINE 1024
void *handle_connection(void *arg);
void list_dir(int connfd);
void download(int connfd, char *filename);
int main(int argc, char *argv[])
{
int listenfd, connfd, port;
struct sockaddr_in servaddr, cliaddr;
socklen_t clilen;
pthread_t tid;
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(EXIT_FAILURE);
}
port = atoi(argv[1]);
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket error");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
perror("bind error");
exit(EXIT_FAILURE);
}
if (listen(listenfd, 5) == -1) {
perror("listen error");
exit(EXIT_FAILURE);
}
printf("服务器已启动,正在监听端口 %d...\n", port);
while (1) {
clilen = sizeof(cliaddr);
if ((connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen)) == -1) {
perror("accept error");
continue;
}
printf("客户端 %s:%d 已连接\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
if (pthread_create(&tid, NULL, handle_connection, (void *)&connfd) != 0) {
perror("pthread_create error");
exit(EXIT_FAILURE);
}
pthread_detach(tid);
}
close(listenfd);
return 0;
}
void *handle_connection(void *arg)
{
int connfd = *(int *)arg;
char buffer[MAXLINE], str[MAXLINE];
char *pos;
while (1) {
memset(buffer, 0, sizeof(buffer));
if (recv(connfd, buffer, sizeof(buffer), 0) < 0) {
perror("recv error");
break;
}
if (strcmp(buffer, "exit") == 0) {
printf("客户端 %d 已断开连接\n", connfd);
break;
} else if (strcmp(buffer, "ls") == 0) {
list_dir(connfd);
} else if (strncmp(buffer, "download", 8) == 0) {
memset(str, 0, sizeof(str));
if (recv(connfd, str, sizeof(str), 0) < 0) {
perror("recv error");
break;
}
pos = strchr(str, ' ');
if (pos != NULL) {
printf("空格后的字符串为:%s\n", pos + 1);
download(connfd, pos+1);
} else {
printf("请输入正确的文件格式\n");
}
} else {
printf("未知命令:%s\n", buffer);
if (send(connfd, buffer, strlen(buffer), 0) < 0) {
perror("send error");
break;
}
}
}
close(connfd);
pthread_exit(NULL);
}
void list_dir(int connfd)
{
FILE *fp;
char buffer[MAXLINE];
fp = popen("ls -l", "r");
if (fp == NULL) {
perror("popen error");
return;
}
while (fgets(buffer, MAXLINE, fp) != NULL) {
if (send(connfd, buffer, strlen(buffer), 0) < 0) {
perror("send error");
break;
}
}
pclose(fp);
}
void download(int connfd, char *filename)
{
FILE *fp;
char buffer[MAXLINE];
int n;
if ((fp = fopen(filename, "r")) == NULL) {
perror("fopen error");
return;
}
while ((n = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
if (send(connfd, buffer, n, 0) < 0) {
perror("send error");
break;
}
}
fclose(fp);
}
```
这个修改后的代码使用了 `pthread_create()` 函数创建了一个新线程来处理每个客户端的连接,这样可以避免 `recv()` 函数的阻塞等待。在新线程中,我们使用 `recv()` 函数来接收客户端发来的命令和文件名,并根据不同的命令调用不同的函数来处理。在 `download()` 函数中,我们使用了标准的文件操作函数 `fopen()` 和 `fread()` 来读取文件内容,并使用 `send()` 函数将内容发送给客户端。