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 6141967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:19:23+00:00 2026-05-23T18:19:23+00:00

trying to write primitive test. Program must startup tcp-server, receive connection and redirect received

  • 0

trying to write primitive test. Program must startup tcp-server, receive connection and redirect received data to forked program. Here is the code:

#include "TcpServer.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <termios.h>
#include <stropts.h>
#include <sys/ioctl.h>

//test
#include <pty.h>

int main() {

    if (setsid() < 1) {
        perror("cannot setsid()");
        return 1;
    }

    TcpServer tcp_server;
    int client;
    char buf[1024];

    int master, slave;
    char * slave_name;


    bzero(buf, sizeof(buf));

    tcp_server.setPort(1025);
    int server = tcp_server.startup();
    client = accept(server, NULL, NULL);

    fprintf(stderr, "forking...\n");
    pid_t pid = forkpty(&master, NULL, NULL, NULL);


    if (pid == 0) {
        setsid();


        fprintf(stderr, "slave pty is opened\n");

        fprintf(stdout, "hello, client!\n");

        char * cmd[] = {"upper.py", NULL};

        fprintf(stderr, "slave: start to login\n");
        int res = execvp("upper.py", cmd);
        if (res < 0) {
            fprintf(stderr, "trouble while running exec - '%s'\n", strerror(errno));
            return 1;
        }
        fprintf(stderr, "slave: quit\n");
        return 0;
    } else if (pid < 0) {
        perror("cannot fork off process");
        return 1;
    }

    fd_set in;
    int max_fd, n;

    struct termios ot, t;
    tcgetattr(master, &ot);
    t = ot;
    t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT );
    t.c_iflag |= IGNBRK;
    t.c_cc[VMIN] = 1;
    t.c_cc[VTIME] = 0;
    tcsetattr(master, TCSANOW, &t);

    while (1) {
        FD_ZERO(&in);

        max_fd = (master > max_fd) ? master : max_fd;
        FD_SET(master, &in);

        max_fd = (client > max_fd) ? client : max_fd;
        FD_SET(client, &in);

        fprintf(stderr, "select starts\n");
        if (select(max_fd + 1, &in, NULL, NULL, NULL) < 0) {
            fprintf(stderr, "select returned incorrectly\n");
            if (errno == EINTR) 
                continue;

            perror("error while select");
            return 1;
        }

        if (FD_ISSET(client, &in)) {
            fprintf(stderr, "client is set\n");
            n = recv(client, buf, sizeof(buf), 0);

            if (n < 0) {
                fprintf(stderr, "error while receiving data from client '%s'\n", strerror(errno));
                return 1;
            }

            if (n <= sizeof(buf)) {
                buf[n] = 0;
            }

            fprintf(stderr, "received from client: '%s'\n", buf);

            n = write(master, buf, n);
            tcflush(master, TCIOFLUSH);
        }

        if (FD_ISSET(master, &in)) {
            fprintf(stderr, "master is set ");

            n = read(master, buf, sizeof(buf));

            if (n < 0) {
                fprintf(stderr, ": %s - ", strerror(errno));
                return 1;
            }
            if (n <= sizeof(buf)) 
                buf[n] = 0;
            fprintf(stderr, "sending data - '%s'\n", buf);
            n = send(client, buf, n, 0);
        }

    }

    return 0;
}

program’s output:

[milo@milo telnetd]$ sudo ./server 
forking...
select starts
master is set sending data - 'slave pty is opened
hello, client!
slave: start to login
'
select starts
master is set sending data - 'input string: '
select starts
client is set
received from client: 'hello'
select starts

The trouble in short: server sucessfully receives data from tcp-client, writes it to master pty, but slave-end don’t receive it. I’ve seen a lot of examples, but cannot see error. Please suggest me…

UPD I’ve tried to int len = read(STDIN_FILENO, buf, sizeof(buf)); instead of execvp and it works fine! I think I have to send some kind of controlling symbol like enter… Any thoughts?

  • 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-05-23T18:19:24+00:00Added an answer on May 23, 2026 at 6:19 pm

    Yahoo! I’ve solved this problem! The mistake was (as i said earlier in UPD) in absence of \n symbol in input! otherside-program waits input until endline symbol is came!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying write a test for a method that returns a java.sql.Connection to connect
I'm trying write a java program to send live microphone data over UDP, then
I'm trying to write Windows app to get data from From Fox Pro DB,
Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void
I am trying to write a very basic export to blender (from primitive shapes)
I'm trying write a C program that sends an UDP packet to a given
Trying to figure out how to write a custom matcher for a primitive value.
I'm trying write a routine in SQL Server that, when run, would traverse specified
Trying to write a simple VCL program for educating purposes (dynamicly created forms, controls
I have been trying to figure out how to write a simple program to

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.