Newsgroup: it.comp.lang.c
Titolo: Errore compilazione programma winsock

Pur avendo linkato le librerie corrette il compilatore mi da errore
sul seguente codice:
#include <stdio.h>
#include <winsock.h>
#define PORT 6644
#define MESSAGE1 "Hello from client\0"
#define MESSAGE2 "Connection Closing\0"
#define MAX_STRING 256
void client(char *);
void server();
void error(char *);
int InitializeWinsock(WORD );
void Usage();
int main(int argc, char *argv[])
{
	if ( argc < 2 ) Usage();
	if (strcmp(argv[1], "client") == 0 )
	{
		// initialize wsock32.dll, requesting winsock 1.1
		if (argc!=3 ) Usage();
		if (!InitializeWinsock (MAKEWORD(1,1) ) ) return 1;
		client(argv[2]);
		WSACleanup();
	}
	else
		if (strcmp(argv[1], "server") == 0 ) 
		{
			if (!InitializeWinsock (MAKEWORD(1,1) ) ) return 1;
			server();
			WSACleanup();
		}
		else Usage();
	return 0;
}
void server ()
{
SOCKET fd, fd_new; 	// "file" descriptors for the network sockets
SOCKADDR_IN saddr_me;
char buf[MAX_STRING];
	if ((fd=socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
		error("Server: socket not connected ");
	saddr_me.sin_family = AF_INET;
	saddr_me.sin_addr.s_addr= htonl(INADDR_ANY);
	saddr_me.sin_port = htons(PORT);
	if (bind(fd, (LPSOCKADDR) &saddr_me, sizeof(saddr_me)) ==
SOCKET_ERROR)
		error("Server: bind failure ");
	if (listen(fd,1) == SOCKET_ERROR)
		error("Server: listen failure ");
	printf("\nServer started\n");
	if ( (fd_new=accept(fd, NULL, NULL)) == INVALID_SOCKET)
		error("Server: accept failure ");
	// In Windows i sockets non sono dei normali file descriptors, quindi
	//  non possono essere usate le normali primitive "read" e "write"
	if( recv(fd_new, buf, sizeof(buf), 0) == SOCKET_ERROR )
		error("Server: receiving failure ");
	printf("Server process read: '%s' \n", buf);
	if( send(fd_new, buf, strlen(buf)+1, 0) == SOCKET_ERROR )
		error("Server: sending failure ");
	if( recv(fd_new, buf, sizeof(buf), 0) == SOCKET_ERROR )
		error("Server: receiving failure ");
	printf("'%s' command received\n\n",buf);
	if (closesocket(fd_new) == SOCKET_ERROR)
		error("Server: closing socket ");
	if (closesocket(fd) == SOCKET_ERROR)
		error("Server: closing socket ");
}
void client(char * server_IP)
{
SOCKET fd; 			// "file" descriptor for the network socket
SOCKADDR_IN saddr;
char buf[MAX_STRING];
	if ((fd=socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET)
		error("Client: socket not connected ");
 
	saddr.sin_family = AF_INET;
	saddr.sin_addr.s_addr = inet_addr(server_IP); 
	saddr.sin_port = htons(PORT);
 
	// request connection to server socket (remote machine)
	if (connect(fd, (LPSOCKADDR) &saddr, sizeof(saddr)) == SOCKET_ERROR)
		error("Client: connect ");
	if( send(fd, MESSAGE1, strlen(MESSAGE1)+1, 0) == SOCKET_ERROR )
		error("Server: sending failure ");
 
	if( recv(fd, buf, sizeof(buf), 0) == SOCKET_ERROR )
		error("Client: receiving failure ");
	printf("Server echoed '%s' \n",buf);
	printf("Sending 'Connection Closing' command\n\n");
	if( send(fd, MESSAGE2, strlen(MESSAGE2)+1, 0) == SOCKET_ERROR )
		error("Server: sending failure ");
	if (closesocket(fd) == SOCKET_ERROR)
		error("Client: closing socket ");
}
/**********************************************
* Windows Sockets Initialization              *
**********************************************/
int InitializeWinsock(WORD wVersionRequested)
{
WSADATA wsaData;
int err;
	err = WSAStartup(wVersionRequested, &wsaData);
	// ritorna errore se, ad esempio, l'applicazione supporta al massimo
	// la versione 1.1 e la DLL supporta da 2.0 in su (le versioni non si
sovrappongono)
	if (err!=0) return 0; // Tell the user that we couldn't find a usable
winsock.dll
	// WSAStartup returns in wHighVersion (struct wsaData) the highest
version it supports
	//  and in wVersion the minimum of its high version and
wVersionRequested.
	//  wVersion is the The version of the Windows Sockets specification 
	//  that the Windows Sockets DLL expects the caller to use.
	// Se WSAStartup ritorna un risultato accettabile, l'applicazione
deve ancora
	//  verificare che il risultato sia compatibile con la sua richiesta.
Ad esempio,
	//  con wVersionRequested=1.1 e DLL version 1.0, wVersion=1.0. Se
l'applicazione
	//  vuole assolutamente usare la DLL 1.1, deve ancora verificare di
non trovarsi
	//  in questo caso
	// Tell the user that we couldn't find a usable winsock.dll.
	if (LOBYTE(wsaData.wVersion )!=1 || HIBYTE(wsaData.wVersion)!=1)
return 0;
	//Everything is fine: proceed
	return 1;
}
/**********************************************
* Error Handling Functions                    *
**********************************************/
void Usage()
{
	printf("\n\t Usage: tcp_sock server | client ip_server_address\n\n");
	exit(1);
}
// define to save space
#define PERR(X) fprintf(stderr, X); break
void error(char* string) {
int err;
 
	fprintf(stderr, "%s", string);
	err= WSAGetLastError();
 
	//in Windows, gli errori dovuti ai sockets sono mappati oltre 10000
   	switch (err)
	{
	case WSANOTINITIALISED: 
             PERR("A successful WSAStartup() must occur before using
this API.");
	case WSAENETDOWN:
             PERR("The Windows Sockets implementation has detected
that the network subsystem has failed.");
	case WSAEAFNOSUPPORT:
             PERR("The specified address family is not supported.");
	case WSAEINPROGRESS:
             PERR("A blocking Windows Sockets operation is in
progress.");
	case WSAEMFILE:
             PERR("No more file descriptors are available.");
	case WSAENOBUFS:
             PERR("No buffer space is available. The socket cannot be
created.");
	case WSAEPROTONOSUPPORT:
             PERR("The specified protocol is not supported.");
	case WSAEPROTOTYPE:
             PERR("The specified protocol is the wrong type for this
socket.");
	case WSAESOCKTNOSUPPORT:
             PERR("The specified socket type is not supported in this
address family.");
   	case WSAEADDRINUSE:
             PERR("The specified address is already in use. (See
setsockopt() SO_REUSEADDR option)");
    	case WSAEFAULT:
             PERR("The namelen argument is too small (less than the
size of a struct sockaddr).");
    	case WSAEINTR:
             PERR("The (blocking) call was canceled via
WSACancelBlockingCall()");
	case WSAEINVAL:
             PERR("The socket is already bound to an address.");
   	case WSAENOTSOCK:
             PERR("The descriptor is not a socket.");
    	case WSAEADDRNOTAVAIL:
             PERR("Address not availabile");
	case WSAEISCONN:
             PERR("The socket is already connected.");
	case WSAEOPNOTSUPP:
             PERR("The referenced socket is not of a type that
supports the listen() operation.");
	case WSAEWOULDBLOCK:
             PERR("The socket is marked as non-blocking and no
connections are present to be accepted.");
	case WSAECONNREFUSED:
             PERR("The attempt to connect was forcefully rejected.");
	case WSAEDESTADDRREQ:
             PERR("A destination address is required.");
	case WSAENETUNREACH:
             PERR("The network can't be reached from this host at this
time.");
	case WSAETIMEDOUT:
             PERR("Attempt to connect timed out without establishing a
connection");
       	case WSAECONNRESET:
             PERR("Connection reset");
 
	default:
             fprintf(stderr, "Error reported by WSAGetLastError= %d\n
Check winsock.h.\n\n", err); break;
	};
	exit(1);
	}
Gli errori sono:
g++.exe main.o  -o "Progetto1.exe" -L"C:/Dev-Cpp/lib"
-L"C:/WINDOWS/system32/wsock32.dll"
main.o(.text+0x8f):main.c: undefined reference to `WSACleanup@0'
main.o(.text+0xd4):main.c: undefined reference to `WSACleanup@0'
main.o(.text+0x1f3):main.c: undefined reference to `socket@12'
main.o(.text+0x21f):main.c: undefined reference to `htonl@4'
main.o(.text+0x232):main.c: undefined reference to `htons@4'
main.o(.text+0x24a):main.c: undefined reference to `bind@12'
main.o(.text+0x26f):main.c: undefined reference to `listen@8'
main.o(.text+0x2a6):main.c: undefined reference to `accept@12'
main.o(.text+0x2d8):main.c: undefined reference to `recv@16'
main.o(.text+0x329):main.c: undefined reference to `send@16'
main.o(.text+0x354):main.c: undefined reference to `recv@16'
main.o(.text+0x38b):main.c: undefined reference to `closesocket@4'
main.o(.text+0x3ae):main.c: undefined reference to `closesocket@4'
main.o(.text+0x4a7):main.c: undefined reference to `socket@12'
main.o(.text+0x4d4):main.c: undefined reference to `inet_addr@4'
main.o(.text+0x4e7):main.c: undefined reference to `htons@4'
main.o(.text+0x4ff):main.c: undefined reference to `connect@12'
main.o(.text+0x528):main.c: undefined reference to `send@16'
main.o(.text+0x553):main.c: undefined reference to `recv@16'
main.o(.text+0x5a0):main.c: undefined reference to `send@16'
main.o(.text+0x5c0):main.c: undefined reference to `closesocket@4'
main.o(.text+0x603):main.c: undefined reference to `WSAStartup@8'
main.o(.text+0xd3d):main.c: undefined reference to `WSAGetLastError@0'
make.exe: *** [Progetto1.exe] Error 1
Esecuzione terminata
Il compilatore e Dev C++ 4.9.8.0
Grazie in anticipo per le risposte.

 -L"C:/WINDOWS/system32/wsock32.dll"
No, non hai linkato le librerie giuste, se no qui vedrei scritto qualcosa
del tipo libwsock32.a o qualcosa del genere (prova a cercare in
C:/Dev-Cpp/lib . Occhio che ce ne sono un due o tre di files .a con nomi
simili. Dipende da quali Winsock usi, se i vecchi o i 2.0). Non credo tu
deva inserire un file .dll fra le cose da linkare.
--- bye

Grazie mille e scusa per l' OT. Proverň a controllare meglio le
librerie. Grazie ancora e ciao :).