Hey all,

I am currently learning about socket programming in my Networking class. Our teacher gave us an example of a server class for it, and while I was trying to build it I came across the error of it not being able to identify socklen_t. I am guessing that for some reason it is not contained in winsock.h or winsock2.h, so I was wondering if there might be a quick fix to this. Never had to add any libraries to my machine, so not really sure how to do that.

Also, if you guys could look at this and let me know if everything looks to be in at least the right place that would be awesome :) Totally new to any client-server programming, so I am relatively lost.

#include <winsock.h>
#include <winsock2.h>
#include <ws2tcpip.h>

int main()
	int socketId = socket(AF_INET, SOCK_DGRAM, 0);

	sockaddr_in serverAddr, clientAddr;
	sockaddr &serverAddrCast = (sockaddr &) serverAddr;
	sockaddr &clientAddrCast = (sockaddr &) clientAddr;

	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(serverPort);
	serverAddr.sin_addr.s_addr = INADDR_ANY;

	bind(socketId, &serverAddrCast, sizeof(addr));

	int size = sizeof(clientAddr);
	recvfrom(socketId, buffer, bufferSize, 0, clientAddrCast, &size);

	sendto(socketId, buffer, bufferSize, 0, clientAddrCast, size);


int sendto(int sid, const void *bufferPtr, size_t bufferLength, int flag, struct sockaddr *addrPtr, socklen_t addrLength)

int recvfrom(int sid, void *bufferPtr, int bufferlength, int flag, sockaddr *addrPtr, int *addrLengthPtr)

Error C2061: syntax error: identifier 'socklen_t'

Recommended Answers

All 3 Replies

See this thread. Looks like you have to define socklen_t yourself.

I seen something like this in linux socket programming. Try using a socklen_t pointer. The worst that can happen it won't work.

See this thread. Looks like you have to define socklen_t yourself.

i want to know how to use lan in c++.coding & header files of this project

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.