User forums > Using Code::Blocks
Error using Winsock
(1/1)
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.
indianhits:
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;
}
--- End code ---
Biplab:
You need to change line 24 to -
--- Quote --- struct sockaddr_in client;
--- End quote ---
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. :)
Jenna:
As written by Biplab:
your post is unrelated to C::B and therefore violating our forum rules.
Topic locked !
Navigation
[0] Message Index
Go to full version