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

  • Home
  • SEARCH
  • 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 7703525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:28:46+00:00 2026-05-31T23:28:46+00:00

I am learning socket programming for use in an upcoming project, and I have

  • 0

I am learning socket programming for use in an upcoming project, and I have researched the issue pretty extensively. Basically, all this program needs to is on a client computer (locally, i.e. my computer) needs to connect to a remote server and send a command (which it has done, I have gotten it to read back Apache server stats to me).

What is happening is this: I believe I have the socket set right, but the server receives random garbage buffers (one of which consisted of ” ‘>Z”). I have tried various socket settings, different bindings, etc.

I have in the process of starting it will initialize winsock, create a socket, bind the network, and then do a listen loop and while(1) recv data.

I have yet to get the server (on a remote computer, hosted at a datacenter) to output the message. This is my only goal for the time being. I appreciate everyone’s help in advance, and the code is before (this is the entire code, sorry for the length).

Client Code:

char *host = "127.0.0.1";
        SOCKET clientsock;
        struct sockaddr_in server_address;

        struct hostent *host_info;

        WSADATA WSAData;
        if(WSAStartup(MAKEWORD(2,2), &WSAData) != -1) {
            cout << "WINSOCK2 Initialized" << endl;         
            if((clientsock = socket(AF_INET, SOCK_STREAM, 0)) != SOCKET_ERROR) {
                cout << "Socket Created" << endl;

                char opt[2];
                opt[0] = 0;
                opt[1] = 1;

                //setsockopt(clientsock, SOL_SOCKET, SO_BROADCAST, opt, sizeof(opt));

                host_info = gethostbyname(host);                    

                server_address.sin_family = AF_INET;
                server_address.sin_addr = *((struct in_addr *)host_info->h_addr);                   
                server_address.sin_port = htons(80);

                if(connect(clientsock, (struct sockaddr *)&server_address, sizeof(struct sockaddr)) == 0) {
                    cout << "Connected to host" << endl;
                    char COMMAND[22] = "SVR --WINSOCK-VERIFY\0";
                    if(send(clientsock, COMMAND, sizeof(COMMAND), 0)) {
                        cout << "Command Sent" << endl;                         
                        closesocket(clientsock);
                    }
                    else {
                        cout << "ERROR - Could not send command. " << "Error: " << WSAGetLastError() << endl;
                        closesocket(clientsock);
                        WSACleanup();
                    }
                }
                else {
                    cout << "ERROR - Could not connect to host. " << "Error: " << WSAGetLastError() << endl;
                    closesocket(clientsock);
                    WSACleanup();
                }                   
            }
            else {
                cout << "ERROR - Could not create the socket. " << "Error: " << WSAGetLastError() << endl;                  
                WSACleanup();
            }               
        }
        else {
            cout << "ERROR - Could not initialize WINSOCK2. " << "Error: " << WSAGetLastError() << endl;                                
            WSACleanup();
        }

Server Code:

SOCKET serversock;
        char *server = "127.0.0.1";     
        //char *server = "50.31.1.180"; 
        struct sockaddr_in server_address;

        WSADATA WSAData;
        if(WSAStartup(MAKEWORD(2,2), &WSAData) != -1) {
            cout << "WINSOCK2 Initialized" << endl;

            if((serversock = socket(PF_INET, SOCK_DGRAM, PF_UNSPEC)) != SOCKET_ERROR) {
                cout << "Socket Created" << endl;

                unsigned long NB = 1;
                ioctlsocket(serversock, FIONBIO, &NB);

                server_address.sin_family = AF_INET;
                server_address.sin_addr = *((struct in_addr *)server);
                server_address.sin_port = htons(21578); 

                if(bind(serversock, (struct sockaddr*)&server_address, sizeof(struct sockaddr) == 0)) {
                    cout << "Network bound" << endl;                    

                    cout << "Listening..." << endl;
                    listen(serversock, 5);                      
                    while(1) {                  
                        int size = sizeof((struct sockaddr *)server);
                        SOCKET clientsock = accept(serversock, (struct sockaddr *)server, &size);
                        char INCOMMAND[20];
                        if(clientsock >= 0) { 
                            if(recv(clientsock, INCOMMAND, sizeof(INCOMMAND), 0)) {                             
                                int i = 0;  
                                if(INCOMMAND == "SVR --WINSOCK-VERIFY\0") {
                                    cout << "SVR receieved" << endl;
                                }
                                while(INCOMMAND[i] != '\0') {
                                    cout << INCOMMAND[i];                                       
                                    i++;                                                                            
                                }                                   
                                cout << endl;                                   
                            }
                            else {
                                cout << "ERROR - Could not receive command" << endl;
                                break;
                            }
                        }
                    }                       
                }
                else {
                    cout << "ERROR - Could not bind network. " << "Error: " << WSAGetLastError() << endl;
                    closesocket(serversock);
                    WSACleanup();
                }
            }
            else {
                cout << "ERROR - Could not create the socket. " << "Error: " << WSAGetLastError() << endl;                  
                WSACleanup();
            }
        }
        else {
            cout << "ERROR - Could not initialize WINSOCK2. " << "Error: " << WSAGetLastError() << endl;                                
            WSACleanup();
        }
  • 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-31T23:28:47+00:00Added an answer on May 31, 2026 at 11:28 pm

    Calls to send/recv may not send/receive the amount of bytes you indicate in their third argument, in fact, most of the time they will send/receive less bytes than you expect. You usually have to loop until the entire data has been sent/received. Also note that doing this:

    char buffer[100];
    recv(clientsock, buffer, sizeof(buffer), 0);
    cout << buffer;
    

    Will most surelly print garbage, since you don’t have a null terminator in your char array(whatch out for buffer overflows when appending it), and you’re not checking the return value of recv. It might be reading 1 byte only(or none if an error ocurred). You’re printing your buffer the same way in your server app.

    In this case, you are actually sending the null-terminator, but since you might read less bytes than you expect, this character might not be received by the other application, thus printing it will print garbage chars.

    Edit: You should have a look at the structure of a sockaddr struct. You can have a look at it here. In your code you are using this convertion:

    int size = sizeof((struct sockaddr *)"127.0.0.1");
    

    const char *, which is the type of “127.0.0.1”, cannot be casted to a sockaddr pointer, they’re incompatible. Here you should use getaddrinfo in order to resolve the IP address(note that you could use a domain name, and this function would resolve it). There are lots of tutorials online on how to use this function, just search for “getaddrinfo”.

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

Sidebar

Related Questions

I am learning socket programming in c, I wrote this simple program to accept
I am still learning socket programming (using Perl) but I have both options (
I am new to network programming, and have been learning this by writing small
I am learning socket programming under Linux,so I make a sample program to list
I'm learning socket programming and I have the following function: public void OnDataReceived(IAsyncResult asyn)
I have just started learning about socket programming and learned about winsock and achieved
I'm learning socket programming (in python) and I was wondering what the best/typical way
I am learning socket programming in c and trying to understand what exactly is
I was learning socket programming and tried to design a basic http client of
I'm learning socket programming in C & downloaded a simple tcp server source file.

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.