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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:26:15+00:00 2026-05-26T11:26:15+00:00

C++ when used in networking is completely cryptic to me. Would anyone mind helping

  • 0

C++ when used in networking is completely cryptic to me. Would anyone mind helping me break down the errors in this code? It was given to my class as an example of how not to make a UDP server.

I already notice a few issues like how the buffers are a fixed 256 bytes long, but for the most part I don’t understand it. I’m more of a PHP/Java/JavaScript programmer.

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

void error(char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, portno, clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     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 *) &cli_addr, sizeof(serv_addr));
     portno = atoi(argv[2]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
     serv_addr.sin_port = htons(portno);

     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(cli_addr)) < 0) 
              error("ERROR on binding");
     clilen = sizeof(cli_addr);

     while(100)
     {
        bzero(buffer,256);
        n = recvfrom(sockfd,buffer, 0, 255,
                 (struct sockaddr *) &serv_addr,&clilen);

        printf("A client from %s port %d connected\n", inet_aton(cli_addr.sin_addr), htons(cli_addr.sin_port));

        if (n < 0) error("Error reading from socket");
        printf("Here is the message: %s\n",buffer);
        n = sendto(sockfd,"I got your message",18,0,(struct sockaddr *) &serv_addr,sizeof(cli_addr));
        if (n < 0) error("Error writing to socket");
     }

     close(sockfd);
     return 0; 
}
  • 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-26T11:26:16+00:00Added an answer on May 26, 2026 at 11:26 am

    Have you tried it? Use nc/aka netcat to try sending UDP packets too it.

    Oh and here is another clue from socket()’s man page

    SOCK_STREAM     Provides sequenced, reliable, two-way, connection-based byte streams.   An  out-of-band data transmission mechanism may be supported
    SOCK_DGRAM      Supports datagrams (connectionless, unreliable messages of a fixed maximum length).
    

    An you are right about the buffer size although it should not overflow – I would use 2^16 aka 65536 as this is the largest UDP packet supported in IPv4

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

Sidebar

Related Questions

When used like this: import static com.showboy.Myclass; public class Anotherclass{} what's the difference between
Never used a cache like this before. The problem is that I want to
I understand the term used all over social networking sites is API, I did
In IPv6 networking, the IPV6_V6ONLY flag is used to ensure that a socket will
This is a biggie. I have a well-structured yet monolithic code base that has
This is what I have done so far: Created a class that has my
Can anyone give a concise set of real-world considerations that would drive the choice
Howdy. I am in a networking class and we are creating our own networking
Has anyone experienced with Common C++ Framework ? Can it be used as a
I would like to know whether Redbean ORM can be used for performance oriented

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.