Sockets I';我想读FTP命令
我写了一个程序,从客户那里读取一个简单的文本。 该程序在telnet发送消息时运行良好,但在ftp发送消息时运行不正常。 另外,程序误解了(?)套接字连接良好。(见图。) 事实上,ftp客户端试图连接我的服务器,随着时间的推移,ftp客户端无法连接。我想用我的服务器程序读取ftp消息。我该怎么办 服务器程序-Sockets I';我想读FTP命令,sockets,ftp,Sockets,Ftp,我写了一个程序,从客户那里读取一个简单的文本。 该程序在telnet发送消息时运行良好,但在ftp发送消息时运行不正常。 另外,程序误解了(?)套接字连接良好。(见图。) 事实上,ftp客户端试图连接我的服务器,随着时间的推移,ftp客户端无法连接。我想用我的服务器程序读取ftp消息。我该怎么办 服务器程序- #include <pthread.h> #include <sys/types.h> #include <sys/socket.h> #includ
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define MAXLINE 1024
#define PORTNUM 3600
void * thread_func(void *data)
{
int sockfd = *((int *)data);
int readn;
socklen_t addrlen;
char buf[MAXLINE];
struct sockaddr_in client_addr;
memset(buf, 0x00, MAXLINE);
addrlen = sizeof(client_addr);
getpeername(sockfd, (struct sockaddr *)&client_addr, &addrlen);
while((readn = read(sockfd, buf, MAXLINE)) > 0)
{
printf("Read Data %s(%d) : %s", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf);
memset(buf, 0x00, MAXLINE);
}
close(sockfd);
printf("worker thread end\n");
return 0;
}
int main(int argc, char** argv)
{
int listen_fd, client_fd;
socklen_t addrlen;
int readn;
char buf[MAXLINE];
pthread_t thread_id;
struct sockaddr_in server_addr, client_addr;
if( (listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return 1;
memset((void*)&server_addr, 0x00, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(PORTNUM);
if(bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
{
perror("bind error");
return 1;
}
if(listen(listen_fd, 5) == -1)
{
perror("listen error");
return 1;
}
while(1)
{
addrlen = sizeof(client_addr);
client_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &addrlen);
if(client_fd == -1)
{
printf("accept error\n");
// return -1;
}
else
{
printf("someone comes in.\n");
fflush(stdout);
pthread_create(&thread_id, NULL, thread_func, (void *)&client_fd);
pthread_detach(thread_id);
}
}
return 0;
}
#包括
#包括
#包括
#包括
#包含FTP协议有点复杂。。。您不能只等待客户端数据,但必须处理协议的多种情况
为了赶上第一阶段,您必须向客户端发送一个220命令
(FTP横幅,告诉我们的FTP服务器是什么,并声明我们准备好收回用户名)
因此,请尝试通过连接的套接字发送字符串“220-\r\nMy FTP SERVER\r\n”
,然后查看发生了什么
Add,就在read()
调用这些行之前
sprintf(buf, "220\r\nMy FTP SERVER\r\n\r\n");
write(connected_fd, (void *) buf, strlen(buf));
消息“220-#\r\n我的FTP服务器\r\n”已打印到我的终端井。但我想接收ftp客户端发送的消息。你能详细地告诉我多个案例吗??网站等等…哇,我明白了。我所要做的就是发送与标准ftp服务器发送到ftp客户端相同的代码消息。字符串已更新。。。我已经测试过了,它可以工作了。。。它将客户端移动到USER
阶段