I have written program reading a simple text from clients.
The program works well when telnet sends messages, but does not when ftp sends messages.
Also, the program misunderstands(?) socket is connected well. (See figure.)
In fact, ftp client was trying to connect my server and As time goes by, ftp client fali to connect. I’d like to read ftp message using my server program. What should I do??
Server program –
#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;
}
here is figures.
https://docs.google.com/document/d/1sKmk0NfTA9dL2svOPsAtaXt_bSpcT4s62yjHuKZCpIk/edit
FTP protocol is a bit more complex… you can’t just wait for client data but you have to handle the multiple cases of the protocol.
In order to just overtake the first phase, you have to send to the client a
220 command(the FTP banner, telling what’s our FTP server and declaring we’re ready to get back the username).So, try to send through the connected socket the string
"220-#\r\nMy FTP SERVER\r\n"and look what’s happening.Add, just before the
read()call these lines.