simple socket problem

Started by
7 comments, last by taby 17 years, 7 months ago
I am new to networking programming. I cant get this simple server code to compile with my vc++ 6 compiler.

#include <iostream>
#include <winsock.h>

using namespace std;
const unsigned short int port = 5432;
const int max_pending = 10;
const int max_len = 256;

int main() 
{
	WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD( 1, 1 );
	WSAStartup( wVersionRequested, &wsaData );

	sockaddr_in address; //address
	sockaddr_in client_address; //client address
	char message[max_len];
	int s;
	int new_s;
	int len;
	//build address
	memset(&address, 0, sizeof(address));
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = htonl(INADDR_ANY);
	address.sin_port = htons(port);
	//setup passive open
	if ((s=socket(PF_INET, SOCK_STREAM, 0)) < 0) {
		cout<<"error in socket";
		return 0;
	}
	//bind socket to address
	if (bind(s, (sockaddr *)&address, sizeof(address)) < 0) {
		cout<<"error in bind";
		return 0;
	}
	if (listen(s, max_pending) < 0) {
		cout<<"error in listen";
		return 0;
	}
	//wait for connection, then receive message
	socklen_t size;
	while (1) {
		if ((new_s = accept(s, (sockaddr *)&client_address, &size)) < 0) {
			cout<<"error in accept";
			return 0;
		}
		while (len = recv(new_s, message, sizeof(message), 0)) {
			cout<<message<<"\n";
		}
		close(new_s);
	}
}

the compiler gives me these errors: error C2065: 'socklen_t' : undeclared identifier error C2146: syntax error : missing ';' before identifier 'size' error C2065: 'size' : undeclared identifier error C2065: 'close' : undeclared identifier I believed socklen_t is a typedef of int. And after adding typedef int socklen_t; did fix the first errors. However, I cant seem to fix the last error with the close() function. I dont know what I did wrong. All I did was just copy and paste the code from an example. Thanks for your time!
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
Advertisement
Did you include ws2_32.lib into your VSC6 project?
_______________________Afr0Games
I have now added it to the linker, but I still get the same errors. Thanks!
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
You should use WinSock 2, and never-ever Winsock 1 (unless you're supporting legacy Win16 code hehe):

#include <winsock2.h>

Link in Ws2_32.lib as suggested above.

Does this help?
Thanks for your reply, but it still doesnt work.
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
Maybe someone can show me a simple example of a socket? Thanks!
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
closesocket() is what you want.

Also, about your socklen_t size;, you should assign sizeof(client_address) to it.
Thanks for your help! I finally get to compile it, however when I build it. I get these errors:

Linking...
server.obj : error LNK2001: unresolved external symbol __imp__closesocket@4
server.obj : error LNK2001: unresolved external symbol __imp__recv@16
server.obj : error LNK2001: unresolved external symbol __imp__accept@12
server.obj : error LNK2001: unresolved external symbol __imp__listen@8
server.obj : error LNK2001: unresolved external symbol __imp__bind@12
server.obj : error LNK2001: unresolved external symbol __imp__socket@12
server.obj : error LNK2001: unresolved external symbol __imp__htons@4
server.obj : error LNK2001: unresolved external symbol __imp__htonl@4
server.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8
Debug/socket.exe : fatal error LNK1120: 9 unresolved externals
Error executing link.exe.

socket.exe - 10 error(s), 0 warning(s)
www.dannylum.com/games_projects.html - My Game Projectswww.dannylum.com/D2DProject - My Open Source 2D Map Editor
Quote:Original post by Anonymous Poster
closesocket() is what you want.

Also, about your socklen_t size;, you should assign sizeof(client_address) to it.


How glaringly obvious. In Winsock one uses clocksocket() and not close(). My bad. :)

Here is some source code for an entire TCP Winsock library:
My lib

This topic is closed to new replies.

Advertisement