Sign in to follow this  

simple socket problem

This topic is 4103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
closesocket() is what you want.

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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this