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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:50:50+00:00 2026-05-16T06:50:50+00:00

Hey guys, here is my code. int main() { char buffer[BUFSIZE]; // define our

  • 0

Hey guys, here is my code.

int main() { 

    char buffer[BUFSIZE]; 

    // define our address structure, stores our port
    // and our ip address, and the socket type, etc.. 
    struct sockaddr_in addrinfo; 
    addrinfo.sin_family = AF_INET; 
    addrinfo.sin_port = htons(PORT); 
    addrinfo.sin_addr.s_addr = INADDR_ANY; 


    // create our socket. 
    int sock; 
    if ( (sock = socket(addrinfo.sin_family, SOCK_STREAM, 0))  < 0) { 
        cout << "Error in creating the socket."; 
    } 

    // bind our socket to the actual adress we want 
    if (bind(sock, (struct sockaddr*)&addrinfo, sizeof(addrinfo)) != 0) { 
        cout << "Error in binding."; 
    } 

    // open the socket up for listening
    if (listen(sock, 5) != 0) { 
        cout << "Error in opening listener."; 
    } 
    cout << "Waiting for connections...." << endl; 

    char *msg = "Success! You are connected.\r\n"; 

    // continuously accept new connections.. but no multithreading.. yet
    while(1) { 

        struct sockaddr_in client_addr;
        socklen_t sin_size = sizeof(client_addr); 

        if(int client = accept(sock, (struct sockaddr*)&client_addr, &sin_size)) { 
            cout << "Recived new connection from " << inet_ntoa(client_addr.sin_addr) << endl; 
            send(client, msg, strlen(msg), 0); 
            while(1) { 
                send(client, buffer, recv(client, buffer, BUFSIZE, 0), 0);

                cout << buffer << endl; 
                strcpy(buffer, ""); 
            } 

        } else { 
            cout << "Error in accepting new connection." << endl; 
        } 

    } 

    close(sock); 
    return 0; 
} 

Now, I’m very new to sockets, Im just sort of trying to get a feel for them but I do have some experience with sockets in PHP. I’m using telnet via putty on my linux machine to test this, I don’t know if thats causing any issues but the server is outputting some strange characters and I don’t know why. I think it has something to do with the buffer, but I’m not really sure. I can send things like “hi” to the server via telnet and it outputs them just fine and sends them back to me but when I send things like “hoobla” it starts the funky character stuff. Any suggestions would be helpful!

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-16T06:50:51+00:00Added an answer on May 16, 2026 at 6:50 am

    You’re getting rubbish printed out because recv does not null-terminate your buffer.

    The important section in the below code is:

    int num = recv(client,buffer,BUFSIZE,0);
    if (num < 1) break;
    
    send(client, ">> ", 3, 0);     // <<-- Nice to have.
    send(client, buffer, num, 0);
    
    buffer[num] = '\0';            // <<-- Really important bit!
    
    if (buffer[num-1] == '\n')     // <<-- Nice to have.
        buffer[num-1] = '\0';      // <<-- Nice to have.
    
    cout << buffer << endl;
    

    which will properly terminate your buffer before trying to print it, as well as remove the trailing newline if present (and allow the client to distinguish between input and echoed lines).

    This one (a complete program) works a little better:

    using namespace std;
    #include <iostream>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    
    #define BUFSIZE 1000
    #define PORT 1234
    
    int main() {
        char buffer[BUFSIZE];
    
        // define our address structure, stores our port
        // and our ip address, and the socket type, etc..
        struct sockaddr_in addrinfo;
        addrinfo.sin_family = AF_INET;
        addrinfo.sin_port = htons(PORT);
        addrinfo.sin_addr.s_addr = INADDR_ANY;
    
        // create our socket.
        int sock;
        if ( (sock = socket(addrinfo.sin_family, SOCK_STREAM, 0))  < 0) {
            cout << "Error in creating the socket.";
            return -1;
        }
    
        // bind our socket to the actual adress we want
        if (bind(sock, (struct sockaddr*)&addrinfo, sizeof(addrinfo)) != 0) {
            cout << "Error in binding.";
            return -1;
        }
    
        // open the socket up for listening
        if (listen(sock, 5) != 0) {
            cout << "Error in opening listener.";
            return -1;
        }
    
        char *msg = "Success! You are connected.\r\n";
    
        // continuously accept new connections.. but no multithreading.. yet
        while(1) {
            cout << "Waiting for connections...." << endl;
    
            struct sockaddr_in client_addr;
            socklen_t sin_size = sizeof(client_addr);
    
            if(int client =
                accept(sock, (struct sockaddr*)&client_addr, &sin_size))
            {
                cout << "Recieved new connection from "
                    << inet_ntoa(client_addr.sin_addr) << endl;
                send(client, msg, strlen(msg), 0);
                while(1) {
                    int num = recv(client,buffer,BUFSIZE,0);
                    if (num < 1) break;
                    send(client, ">> ", 3, 0);
                    send(client, buffer, num, 0);
    
                    buffer[num] = '\0';
                    if (buffer[num-1] == '\n')
                        buffer[num-1] = '\0';
                    cout << buffer << endl;
                    strcpy(buffer, "");
                }
            } else {
                cout << "Error in accepting new connection." << endl;
            }
        }
        close(sock);
        return 0;
    }
    

    On the client side:

    $ telnet 127.0.0.1 1234
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    Success! You are connected.
    hello
    >> hello
    my name is pax
    >> my name is pax
    and you?
    >> and you?
    <CTRL-D>
    Connection closed by foreign host.
    

    and, on the server side:

    $ ./testprog
    Waiting for connections....
    Recived new connection from 127.0.0.1
    hello
    my name is pax
    and you?
    Waiting for connections....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey Guys, here is my code for this, the only help i get from
Hey guys here is my code: <script>$('td#view-details').click(function() { var fg = $(this).closest('td').siblings(':first-child').text(); $('#'+fg+'confirm').click(function ()
...hey guys, here is the view code in question... <td> <%= f.radio_button :score, '4'
Hey guys a bit stuck here. Ill first start with showing you the code.
Hey guys got a quick question here, how do i set up TweetStream after
Hey guys I have a little issue here. I have a panel where I
Hey guys i had an idea yesterday. Can you help me with this. Here
The photo (please view): Example Hey guys, I'm new here.. I'm also new to
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
hey there guys and girls i have this code that saves json as a

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.