Odd Winsock Error

When compiling the below code I get the following errors:

1>c:\users\craig\documents\visual studio 2008\projects\winsock\winsock\main.cpp(29) : error C2228: left of '.sin_family' must have class/struct/union
1> type is 'sockaddr_in *'
1> did you intend to use '->' instead?
1>c:\users\craig\documents\visual studio 2008\projects\winsock\winsock\main.cpp(30) : error C2228: left of '.sin_addr' must have class/struct/union
1> type is 'sockaddr_in *'
1> did you intend to use '->' instead?
1>c:\users\craig\documents\visual studio 2008\projects\winsock\winsock\main.cpp(30) : error C2228: left of '.S_un' must have class/struct/union
1>c:\users\craig\documents\visual studio 2008\projects\winsock\winsock\main.cpp(30) : error C2228: left of '.S_addr' must have class/struct/union
1>c:\users\craig\documents\visual studio 2008\projects\winsock\winsock\main.cpp(31) : error C2228: left of '.sin_port' must have class/struct/union
1> type is 'sockaddr_in *'
1> did you intend to use '->' instead?


Any help is greatly appreciated! Thanks in advance.

EDIT: I'm using MSVC++ Express 2008, with Windows Vista.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>


#pragma comment(lib, "w32_2.lib")

int main()
{
WSADATA WsaData;

WSAStartup(0x202, &WsaData);

SOCKET ServerSocket;
SOCKET ClientSocket;
struct sockaddr_in* ServerInfo;
int iRetVal = 0;


ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ServerSocket == INVALID_SOCKET)
{
printf("error: unable to create the listening socket...\n");
}
else
{
ServerInfo.sin_family = AF_INET;
ServerInfo.sin_addr.s_addr = INADDR_ANY;
ServerInfo.sin_port = htons(8080);
iRetVal = bind(ServerSocket, (struct sockaddr*) &ServerInfo, sizeof(struct sockaddr));
if (iRetVal == SOCKET_ERROR)
{
printf("error: unable to bind listening socket...\n");
}
else
{
iRetVal = listen(ServerSocket, 10);
if (iRetVal == SOCKET_ERROR)
{
printf("error: unable to listen on listening socket...\n");
}
else
{
char* pszSendData = "Hello world!";
while (true)
{
ClientSocket = accept(ServerSocket, NULL, NULL);
printf("Incoming connection accepted!\n");
send(ClientSocket, pszSendData, strlen(pszSendData), 0);
closesocket(ClientSocket);
}
}
}
}
}
Last edited on
You have defined ServerInfo as a POINTER in line 9 but using the . (dot) access in line 28,29 and 30.
You should either:
1. remove the pointer bit from line 9 and have struct sockaddr_in ServerInfo;

2. leave line 9 as it is and use -> instead of the . lines 28, 29 and 30.

Changing line 9 will be easiest as this will also automatically cure the problem in line 31.
Topic archived. No new replies allowed.