News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

Error using Winsock

Started by indianhits, June 20, 2010, 03:52:33 AM

Previous topic - Next topic

indianhits

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!

Biplab

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.

indianhits

#2
Here is the full code it works in MSVC


#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;
}

Biplab

You need to change line 24 to -
Quotestruct 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.

Jenna

As written by Biplab:
your post is unrelated to C::B and therefore violating our forum rules.

Topic locked !