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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:06:29+00:00 2026-05-13T08:06:29+00:00

I am creating a simple Unix server written in C++ that simply waits for

  • 0

I am creating a simple Unix server written in C++ that simply waits for incoming connections, and then when a connection is established, it sends the requested data back to the client’s browser to be displayed. I have everything working except for the sending of the data to the client. This is how it should work:

  1. I start server and set port number (for this example i’ll use 8181).

  2. Client opens browser and connects to my server located at http://server.mysite.com:8181/test.txt and attempts to retrieve test.txt for viewing.

  3. Server accepts incoming connection. It checks to see if file exists. If file exists, it sends the file to the browser to be viewed.

My problem is that I’m not sure how to send the data back to the browser to be viewed. I’m trying to use Unix system calls to get this accomplished, as I believe that’s all that should be needed to get the data back.

Right now all I’m doing is read()ing in from the file into a char array buffer and then feeding that into the write(socketID, buffer, strlen(buffer)) command. When I test it, the server correctly finds the file and reads the data into the char array buffer, but the write() command is returning a -1 error value every time.

I would like to get it to where the browser connects and then is able to view the text.txt in the browser as well as browser-supported picture formats.

Is there some way to workaround the “reading in to a buffer then writing that buffer to a socket” part and just somehow send the file directly to the browser? Here is part of the code I’m working with now. I just commented in other parts of code.

//create and bind socket. call listen()...
conn_socket = accept(socketInt, (struct sockaddr *)&caddr, (socklen_t *) &caddrLength);

recvInt = recv(conn_socket, requestedFileBuffer, 1023, 0);
if(recvInt > 0){
    //checks to see that the requestd file exists and correctly 
    //reads that file into char array "buffer"
    int writeInt = write(recvInt, buffer, strlen(buffer));//This is not working
cout << "writeInt is: " << writeInt << endl;// returns -1
}//end if

Anyone have any suggestions? If you need me to post more code I’d be happy to do so, but I believe the problem lies in the above code.

Thanks for your time.

  • 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-13T08:06:29+00:00Added an answer on May 13, 2026 at 8:06 am

    When you say:

    int writeInt = write(recvInt, buffer, strlen(buffer));
    

    the first parameter of write() should be the socket to write to – you are passing it the count of characters returned by recv()

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

Sidebar

Related Questions

I'm creating a simple API that creates typed classes based on JSON data that
I'm creating a simple tag system that allows me to link a tag to
I am creating a simple application with Django. I realized that I am doing
I have a simple server that looks something like this: void *run_thread(void *arg) {
I'm creating simple application, some kind of portfolio. I've heard that it's better to
I'm creating a simple BizTalk 2010 process that will convert a 4010 835 file
I am creating a simple Unix shell in C. I use the fork() to
I am making simple ANSI C program, that simulates Unix shell. So I am
Creating a simple TCP server based on examples but still do not get how
I'm creating simple payment form that allows users to select from several different gateways

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.