Author Topic: Error using Winsock  (Read 9634 times)

Offline indianhits

  • Single posting newcomer
  • *
  • Posts: 6
Error using Winsock
« on: June 20, 2010, 03:52:33 am »
Hello, when i try to compile a winsock code i get following error

Here is the full error:
||=== client, Debug ===|
C:\Documents and Settings\HarishAdmin\My Documents\harish\main.c||In function 'main':|
C:\Documents and Settings\HarishAdmin\My Documents\harish\main.c|24|error: 'sockaddr_in' undeclared (first use in this function)|
C:\Documents and Settings\HarishAdmin\My Documents\harish\main.c|24|error: (Each undeclared identifier is reported only once|
C:\Documents and Settings\HarishAdmin\My Documents\harish\main.c|24|error: for each function it appears in.)|
C:\Documents and Settings\HarishAdmin\My Documents\harish\main.c|24|error: expected ';' before 'client'|
C:\Documents and Settings\HarishAdmin\My Documents\harish\main.c|25|error: 'client' undeclared (first use in this function)|
||=== Build finished: 5 errors, 2 warnings ===|


i linked the libws2_32.a and even the libwsock32.a
but still it keeps showing the problem
and yes the code is correct i check it in my MSVC
please help me.Thanks!
« Last Edit: June 20, 2010, 03:54:43 am by indianhits »

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Error using Winsock
« Reply #1 on: June 20, 2010, 04:35:47 am »
You are getting a compiler error; not a linker error. Please check that you have used correct header.

Also note that this issue is not related to Code::Blocks. Please search appropriate forum in internet.
Be a part of the solution, not a part of the problem.

Offline indianhits

  • Single posting newcomer
  • *
  • Posts: 6
Re: Error using Winsock
« Reply #2 on: June 20, 2010, 04:51:01 am »
Here is the full code it works in MSVC

Code
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>

int main()
{
        // Initialize Winsock
        WSADATA wsaData;

        if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
                printf("Error at WSAStartup()\n");

        // Create a SOCKET for connecting to server
        SOCKET ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (ConnectSocket == INVALID_SOCKET)
        {
                printf("Error at socket(): %ld\n", WSAGetLastError());
                WSACleanup();
                return 0;
        }

        // The sockaddr_in structure
        sockaddr_in client;
        client.sin_family = AF_INET;
        client.sin_addr.s_addr = inet_addr("192.168.176.1");
        client.sin_port = htons(80);

        // Connect to server.
        if (connect(ConnectSocket, (SOCKADDR*) &client, sizeof(client)) == SOCKET_ERROR)
        {
                printf("Failed to connect.\n" );
                WSACleanup();
                return 0;
        }

        printf("Connected to server.\n");

        char buffer[1024];
        char filename[512] = "/";
        char hostname[512] = "192.168.176.1";

        sprintf(buffer, "GET %s HTTP/1.1\r\n", filename);
        sprintf(buffer, "%sHost: %s\r\n", buffer, hostname);

        printf("%s\n", buffer);

        send(ConnectSocket, buffer, strlen(buffer), 0);

        int y;
        while(y = recv(ConnectSocket, buffer, 512, 0))
        {
                printf("%s", buffer);
        }

        WSACleanup();
        return 0;
}
« Last Edit: June 20, 2010, 04:52:46 am by indianhits »

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Error using Winsock
« Reply #3 on: June 20, 2010, 07:15:01 am »
You need to change line 24 to -
Quote
        struct sockaddr_in client;

Note the item in Bold-Red colour.

GCC being more standards compliant doesn't allow original code to be compiled.


Also note that as this is not an issue with Code::Blocks, similar future posts may get locked. Please search internet first. :)
Be a part of the solution, not a part of the problem.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: Error using Winsock
« Reply #4 on: June 20, 2010, 11:01:45 am »
As written by Biplab:
your post is unrelated to C::B and therefore violating our forum rules.

Topic locked !