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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:37:31+00:00 2026-05-24T15:37:31+00:00

I have the following code (I’m working from code at http://www.linuxhowtos.org/C_C++/socket.htm ) which I’m

  • 0

I have the following code (I’m working from code at http://www.linuxhowtos.org/C_C++/socket.htm) which I’m trying to turn into a proxy server:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void dostuff(int); /* function prototype */
void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     //setup proxy:
     int sockfd, newsockfd, portno, pid;
     socklen_t clilen;
     struct sockaddr_in serv_addr, cli_addr;

     if (argc < 2) {
         fprintf(stderr,"***ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
        error("***ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0)
              error("***ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     while (1) {
         newsockfd = accept(sockfd,
               (struct sockaddr *) &cli_addr, &clilen);
         if (newsockfd < 0)
             error("***ERROR on accept");
         pid = fork();
         if (pid < 0)
             error("***ERROR on fork");
             if (pid == 0)  {
             close(sockfd);
             dostuff(newsockfd);
             exit(0);
         }
         else close(newsockfd);
     } /* end of while */
     close(sockfd);
     return 0; /* we never get here */
}

/******** DOSTUFF() *********************
 There is a separate instance of this function 
 for each connection.  It handles all communication
 once a connnection has been established.
 *****************************************/
void dostuff (int sock)
{
   int n;
   char buffer[256];

   bzero(buffer,256);
   n = read(sock,buffer,255);
   if (n < 0){
       error("***ERROR reading from socket");
   }
   //printf("Here is the message: %s\n",buffer);




   /*
   ***Forward message to port 80 and read response here
   */





   n = write(sock,"I got your message",18);
   if (n < 0) error("***ERROR writing to socket");
}

In the function “dostuff” I want to write ‘buffer’ to port 80, read the response and write this response back over port 20000 (argv[1]).

At the moment, when I set my browser’s proxy to 172.16.1.218:20000, all I get is “I got your message”. I want to change this to the response from the webpage!

Any pointers in the right direction greatly appreciated.

Here’s what I’ve tried sofar (replace multi-line comment “Forward message to port 80 and read response here” with this code):

   int sockfdi, portnoi, ni;
   struct sockaddr_in serv_addri;
   struct hostent *serveri;
   portnoi =80;

   sockfdi = socket(AF_INET, SOCK_STREAM, 0);
   if (sockfdi < 0){
        error("***ERROR opening socket");
   }
   serveri = gethostbyname("172.16.1.218");
   if (serveri == NULL){
       fprintf(stderr,"***ERROR, no such host\n");
       exit(0);
   }

   bzero((char *) &serv_addri, sizeof(serv_addri));
   serv_addri.sin_family = AF_INET;
   bcopy((char *)serveri->h_addr, (char *)&serv_addri.sin_addr.s_addr, serveri->h_length);
   serv_addri.sin_port = htons(portnoi);
   if (connect(sockfdi,(struct sockaddr *) &serv_addri,sizeof(serv_addri)) < 0){
       error("***ERROR connecting");
   }
   printf("Please enter the message: ");
   bzero(buffer,256);

But every time I try to connect via my webbrowser, the server echos: “***ERROR connecting: Connection refused”

Many thanks in advance,

  • 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-24T15:37:31+00:00Added an answer on May 24, 2026 at 3:37 pm

    This is a non-trivial task you set out to do. Currently, you’re missing three things, an easy one and two difficult ones:

    1. You have to open a network connection to the server you want to forward the call to (rather easy, see socket() and connect()).

    2. You’ll then have a duplex connection, that is two concurrent streams of data, one going from the client to the forwarded server and one from the forwarded server to the client. In order to cope with this concurrency, you either need two threads with blocking I/O or some sort of non-blocking I/O (see select() or AIO).

    3. If you forward an HTTP request without changes to another server, you’ll likely end up with invalid server names and IP addresses in the request. The request will then be rejected. So you’ll need to parse the HTTP header, do some replacements and forward the modified HTTP request.

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

Sidebar

Related Questions

I'm trying to use opengl in C#. I have following code which fails with
i have following code in which, i am fetching the data from the sqlite
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
I have the following code: SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING
I have following code import sys from ctypes import * from ctypes.util import find_library
I have following code: SELECT q21, q21coding AS Description FROM `tresults_acme` WHERE q21 IS
I have following code: <Window xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=145 Width=156> <Window.Resources> <DataTemplate x:Key=tabTemplate> <ScrollViewer>
I have following code which works fine in iPhone application but in iPad I
I'm trying to use Entity Framework to query database and I have following code

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.