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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:52:14+00:00 2026-05-24T00:52:14+00:00

In my socket code I have the following structure: Server: #include <stdio.h> #include <stdlib.h>

  • 0

In my socket code I have the following structure:

Server:

         #include <stdio.h>
         #include <stdlib.h>
         #include <string.h>
         #include <sys/types.h>
         #include <sys/socket.h>
         #include <netinet/in.h>
         #define MAXPROFILES  2

        int main(int argc, char *argv[])
     {
        int sockfd, newsockfd, portno, clilen;
        struct sockaddr_in serv_addr, cli_addr;
        unsigned char buf[1024];
        char* pointer = buf;

       struct profile_t
     {
       unsigned char length;
       unsigned char type;
       unsigned char *data;
     };

       typedef struct profile_datagram_t
     {
       unsigned char src[4];
       unsigned char dst[4];
       unsigned char ver;
       unsigned char n;
       struct profile_t profiles[MAXPROFILES];
     } header;


       header outObj;

       int j =0;
       int i =0;


       if (argc < 2) {
       fprintf(stderr,"usage: %s port_number1",argv[0]);
       exit(1);
     }
       sockfd = socket(AF_INET, SOCK_STREAM, 0);
       if (sockfd < 0)
       error("ERROR DETECTED !!! Problem in 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 = htonl(INADDR_ANY);
       serv_addr.sin_port = htons(portno);

        if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
          error("ERROR DETECTED !!! There was a problem in binding");

        listen(sockfd, 10);
        clilen = sizeof(cli_addr);



        printf("Server listening on port number %d...\n", serv_addr.sin_port);

        newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

        if (newsockfd < 0)
        error("ERROR DETECTED !!! the connection request was not accepted");

        int rc = read(newsockfd,&pointer,100);
        if(rc < 0){
        printf("error");
      }
        else {
        printf("success %d",rc);
      }
        for(int i=0;i<29;i++){
        printf(pointer);
        printf(buf);
      }





 return 0;

}

Client:

            int main(int argc, char *argv[])

         {
            int sockfd, portno, n;
            struct sockaddr_in serv_addr;
            struct hostent *server;
            unsigned char buf[1024];
            unsigned int srcAddress = 2193598184;
            unsigned int destAddress = 2193598182;

           struct profile_t
       {
            unsigned char length;
            unsigned char type;
            unsigned char *data;
       };

            typedef struct profile_datagram_t
       {
            unsigned char src[4];
            unsigned char dst[4];
            unsigned char ver;
            unsigned char n;
            struct profile_t profiles[MAXPROFILES];
        } header;


           header outObj;

           int j =0;
           int i =0;
      // for loop for doing the malloc so that we can allocate memory to all profiles
           for(i=0;i<MAXPROFILES;i++){
           outObj.profiles[i].data = malloc(5);
        }


          for(i=0;i<4;i++){
          outObj.src[i] = (srcAddress >> (i*8)) & 0xFF;
          outObj.dst[i] = (destAddress >> (i*8)) & 0xFF;
        }
          outObj.ver = 1;
          outObj.n = 2;

          memcpy(buf,&outObj.src,4);
          memcpy(buf+4,&outObj.dst,4);
          memcpy(buf+8,&outObj.ver,1);
          memcpy(buf+9,&outObj.n,2);


          outObj.profiles[0].length = 5;
          outObj.profiles[0].type = 1;
          outObj.profiles[1].length = 5;
          outObj.profiles[1].type = 2;

          for(i=0;i<MAXPROFILES;i++){
          for(j=0;j<5;j++){
          outObj.profiles[i].data[j] = j+1;
        }
      }



            int k = 11;


           for(i=0;i<MAXPROFILES;i++){
            memcpy(buf+k,&outObj.profiles[0].length,1);
            memcpy(buf+k+1,&outObj.profiles[0].type,1);
            memcpy(buf+k+2,outObj.profiles[0].data,5);
            k +=7;
       }


           if (argc < 3) {
           fprintf(stderr,"usage: %s hostname port\n", argv[0]);
           exit(0);
       }
           portno = atoi(argv[2]); //Convert ASCII to integer
           sockfd = socket(AF_INET, SOCK_STREAM, 0); // socket file descriptor


           if (sockfd < 0)
           error("ERROR DETECTED !!! Problem in opening socket\n");

           server = gethostbyname(argv[1]);
           if (server == NULL) {
           fprintf(stderr,"ERROR DETECTED !!!, no such server found \n");
           exit(0);
       }

           bzero((char *) &serv_addr, sizeof(serv_addr)); //clear the memory for server  address

           serv_addr.sin_family = AF_INET;
           bcopy((char *)server->h_addr,
                 (char *)&serv_addr.sin_addr.s_addr,
                   server->h_length);

           serv_addr.sin_port = htons(portno);

           printf("Client 1 trying to connect with server host %s on port %d\n", argv[1], portno); 


             if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
             error("ERROR in connection");

             printf("SUCCESS !!! Connection established \n");


             if (write(sockfd, buf, k) < 0)
          {
             error("Write error has occured ");
          }


            return 0;
           }

Now from the client side I do a memcpy to my unsigned char buf and send the data. On the recv side how to access the values inside my “buf”. I tried the “*” operator, but i get a warning

arning: passing argument 1 of âprintfâ makes pointer from integer without a cast

I want to basically access the values inside buffer, as I know the size i should be able to repopulate my packet back. I know that this is a “C” question and not exactly “socket related”. Any help would be really helpful.

  • 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-24T00:52:15+00:00Added an answer on May 24, 2026 at 12:52 am

    On the server side, you just have to reverse what you’ve done on the client side:

    You’re also not gaining anything by using char *pointer = buf, just read directly into the buffer. When you loop around for your MAXPROFILES, just check your current position in buffer against the returned size to make sure you’re not reading data you didn’t receive.

    For example:

       int size = read(newsockfd, buffer, sizeof(buffer));
    
       int src, dst;
       char ver;
       short n;
    
       memcpy(&src, buf + 0, 4);
       memcpy(&dst, buf + 4, 4);
       memcpy(&ver, buf + 8, 1);
       memcpy(&n,   buf + 9, 2);
    

    …and so on…

    You’re encoding your structures/data on the client side, so you have to decode them on the server side. And once you’ve done so, then you can print them or do whatever it is you like:

      printf("src: %d  dst: %d  ver: %c  n: %d\n", src, dst, ver, n);
    

    Also, to simplify your encode/decode, use the same method (e.g. memcpy instead of your byte-by-byte with masks method). For example, on the client side:

      k = 0;
      memcpy(buf + k, &src, sizeof(src));  k += sizeof(src);
      memcpy(buf + k, &dst, sizeof(dst));  k += sizeof(dst);
      ...
    

    This also frees you a bit from hardcoded numbers as I was doing above. Just make sure your src, dst, and all the rest have the same type on both the client and server.

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

Sidebar

Related Questions

I have created a socket using the following lines of code and i get
I have the following code: Socket clientSocket = null; try { clientSocket = serverSocket.accept();
I have the following code in C#: Console.WriteLine(Connecting to server...); TcpClient client = new
I have the following code: http://pastebin.com/U9qYSmET When I try to telnet into my server
Suppose, I have a connected socket after writing this code.. if ((sd = accept(socket_d,
I have the following code regarding asynchronous sockets programming: public void ServerBeginAceept(ushort ServerPort) {
i have the following code below (i have stuck in xxx to not publish
I have the following piece of code for getting the hostname and IP address,
The following code waits for data over UDP. I have a test function that
I have written a very simple socket server. It listens in post 63254. First

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.