Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9147005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:53:00+00:00 2026-06-17T10:53:00+00:00

I have written program reading a simple text from clients. The program works well

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:53:01+00:00Added an answer on June 17, 2026 at 10:53 am

    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.

    sprintf(buf, "220\r\nMy FTP SERVER\r\n\r\n");
    write(connected_fd, (void *) buf, strlen(buf));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program that uses qhttp to get a webpage. This works
I have a more complex program that involves using what's written in a text
I have written a program in Java to read in a text file of
I have a program which reads data from 2 text files and then save
I have a server written in Python that basically accepts incoming connection from clients
I have written a program that constantly reads from the Bluetooth via SPP and
I have written a test program to take in values from a csv file,
I have written a program for reading the contents of a file. import java.io.*;
I have written a small program, while reading a book about swing, that creates
I have written a C program that uses two threads for reading and writing.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.