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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:37:45+00:00 2026-05-23T08:37:45+00:00

I’m a novice/beginner programmer having problems getting some simple client/server C code working. My

  • 0

I’m a novice/beginner programmer having problems getting some simple client/server C code working. My end goal is to send a ‘stream’ of azimuth/elevation data from a server to a client, and then convert that data as it is received (it will just be a division, but I don’t really know how to do this either) into position data for a pan/tilt unit, and then output the converted data via serial to the pan/tilt head. (I’ll likely be back to ask about that later…)

Right now I’m just trying to figure out how to get the data sent and received. I grabbed code from this website. http://www.tenouk.com/Winsock/Winsock2example3.html. I had to move a few declarations around to get the code to compile.

I’m using Windows 7 and VS2010 professional on the client pc. There is no router in between the client and server (they’re directly connected via ethernet).

Using the debugger, I found that I’m getting hung up at this point.

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("12.266.66.255");
clientService.sin_port = htons(55555);

if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
    printf("Client: connect() - Failed to connect.\n");
    WSACleanup();
    return 0;
}

else
{
   printf("Client: connect() is OK.\n");
   printf("Client: Can start sending and receiving data...\n");
}

I always get the “failed to connect” message, and I’m not sure why. I am using the correct IP address of the host computer (I changed it before putting on here).

If this is a bad example to use, I’m open to starting over with another example. I’ve tried several of the ‘echo’ examples commonly found online, and I’m getting similar problems. I can give more info on my overall program goals as well if that would help. The rest of the client code is below. I’m using the server code (with declarations moved around) from the same link. Thanks.

int main()
{

int m_socket;

struct sockaddr_in clientService;

int bytesSent;

int bytesRecv = SOCKET_ERROR;
// Be careful with the array bound, provide some checking mechanism...
char sendbuf[200] = "This is a test string from client";
char recvbuf[200] = "";
// Initialize Winsock.

WSADATA wsaData;

int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
          printf("Client: Error at WSAStartup().\n");
else
          printf("Client: WSAStartup() is OK.\n");

// Create a socket
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET)
{
    printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError());
    WSACleanup();
    return 0;
}

else
   printf("Client: socket() is OK.\n");

// Connect to a server.
// Just test using the localhost, you can try other IP address
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("12.233.21.254");
clientService.sin_port = htons(55555);

if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
    printf("Client: connect() - Failed to connect.\n");
    WSACleanup();
    return 0;
}

else
{
   printf("Client: connect() is OK.\n");
   printf("Client: Can start sending and receiving data...\n");
}

// Send and receive data.
   // Receives some test string to server...

   while(bytesRecv == SOCKET_ERROR)
   {
       bytesRecv = recv(m_socket, recvbuf, 200, 0);

    if (bytesRecv == 0 || bytesRecv == WSAECONNRESET)
    {
         printf("Client: Connection Closed.\n");
        break;
    }

    if (bytesRecv < 0)
        return 0;

   else
   {
          printf("Client: recv() is OK.\n");
          printf("Client: Received data is: \"%s\"\n", recvbuf);
          printf("Client: Bytes received is: %ld.\n", bytesRecv);

   }

}

   // Sends some test data to server...
   bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0);

   if(bytesSent == SOCKET_ERROR)
          printf("Client: send() error %ld.\n", WSAGetLastError());

   else
   {
          printf("Client: send() is OK - Bytes sent: %ld\n", bytesSent);
          printf("Client: The test string sent: \"%s\"\n", sendbuf);
   }

WSACleanup();

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-23T08:37:46+00:00Added an answer on May 23, 2026 at 8:37 am

    Your original code (after I changed the IP and port, obviously) connected to my web-server just fine, but I did tweak it a bit (below).

    While it might be a bit much, CSocketServer contains a wealth of good ol’ WinSock code that’s been tried and held true.

    Anyway, this code connected to my local web server, sent a rudimentary request and received a response.

    int WSATest()
    {
        // Initialize Winsock.
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
        {
            printf("Client: Error at WSAStartup().\n");
            return 0;
        }
    
        // Create a socket
        SOCKET m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (m_socket == INVALID_SOCKET)
        {
            printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError());
            WSACleanup();
            return 0;
        }
        else {
            printf("Client: socket() is OK.\n");
        }
    
        struct sockaddr_in clientService;
        clientService.sin_family = AF_INET;
        clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
        clientService.sin_port = htons(9990);
    
        //Connect to the remote peer:
        if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
        {
            printf("Client: connect() - Failed to connect.\n");
            WSACleanup();
            return 0;
        }
        else
        {
            printf("Client: connect() is OK.\n");
            printf("Client: Can start sending and receiving data...\n");
        }
    
        //Very, very basic HTTP request.
        int iSendResult = send(m_socket, "GET / \n\n", 8, 0);
        if(iSendResult == SOCKET_ERROR)
        {
            printf("Failed to send data, error %d.\n", WSAGetLastError());
            return 0;
        }
    
        while(true)
        {
            char sRecvBuffer[200];
            int iRecvResult = recv(m_socket, sRecvBuffer, sizeof(sRecvBuffer) - 1, 0);
    
            if(iRecvResult <= SOCKET_ERROR)
            {
                printf("Failed to receive data, error %d.\n", WSAGetLastError());
                break;
            }
            else if(iRecvResult == 0)
            {
                //Graceful disconnect.
                break;
            }
            else {
                //Be sure to terminate the buffer.
                sRecvBuffer[iRecvResult] = '\0';
            }
    
            printf("Received: [%s] for [%d] bytes.\n", sRecvBuffer, iRecvResult);
        }
    
        WSACleanup();
    
        return 0;
    }
    
    int main()
    {
        WSATest();
        system("Pause");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

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.