Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » C programming socket WSADATA problem
C programming socket WSADATA problem [message #1716722] Mon, 07 December 2015 13:12 Go to next message
Eclipse UserFriend
Hello everyone.
I'm starting programming socket in C. I am using Eclipse and I have installed MinGW compiler. When I compile a simple starter program I have this error:

Field 's_addr' could not be resolved
Symbol 'AF_INET' could not be resolved
Symbol 'IPPROTO_TCP' could not be resolved
Symbol 'PF_INET' could not be resolved
Symbol 'SOCK_STREAM' could not be resolved
Type 'WSADATA' could not be resolved

I have included in the code and also added wsock32 to the project library. It seems not recognizing the WSADATA structure I think. What's the problem? Thanks.
Re: C programming socket WSADATA problem [message #1716731 is a reply to message #1716722] Mon, 07 December 2015 15:05 Go to previous messageGo to next message
Eclipse UserFriend
Hmmm ... another "I did something -- not saying what -- and it didn't work. Why?" post.
Instead of making us guess, it would be helpful if you were more specific about what you've done and what you are seeing. We aren't mind readers.

For instance, what is producing those messages? The compiler? CDT itself? How was the project created? Did you modify it? How is the file being built? By makefile? If so, what is its source? Did you make it or import it from somewhere or is Eclipse creating it?

XXX could no be resolved could be a compiler error message or or an error emitted by the CDT Indexer. Whichever, it isn't seeing a definition for WSADATA. Without more information it isn't possible to say why -- particularly without knowing what is missing it.



Re: C programming socket WSADATA problem [message #1716874 is a reply to message #1716731] Tue, 08 December 2015 12:46 Go to previous messageGo to next message
Eclipse UserFriend
It Seems resolved adding #include <winsock2.h>

Using Eclipse with MinGW compiler . This simple starter program creates a client/server communication, server side.

#include <stdio.h>
#include <stdlib.h> // for atoi()
#include <winsock2.h>
#include <winsock.h>

#define PROTOPORT 5193 //default protocol port number
#define QLEN 6 // size of request queue

void ErrorHandler (char *errorMessage) {
printf (errorMessage);
}

void ClearWinSock() {
WSACleanup ();
}


int main (int argc, char *argv[]) {
int port;
if (argc>1) {
    port = atoi (argv[1]); //if argument specified convert argument to binary
}
else
	port = PROTOPORT; //use dafault port number
if (port < 0) {
	printf ("bad port number %s \n", argv[1]);
return 0;
}


WSADATA wsaData;
int iResult = WSAStartup (MAKEWORD(2,2), &wsaData);

if (iResult != 0) {
	ErrorHandler ("Error at WSAStartup()\n");
	return 0;
}


// CREAZIONE DELLA SOCKET
int MySocket;
MySocket = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (MySocket < 0) {
	ErrorHandler ("socket creation failed.\n");
	ClearWinSock();
	return 0;
}

// ASSEGNAZIONE DI UN INDIRIZZO ALLA SOCKET
struct sockaddr_in sad;
memset (&sad, 0, sizeof (sad)); //ensures that extra bytes contain 0
sad.sin_family = AF_INET;
sad.sin_addr.s_addr = inet_addr ("127.0.0.1");
sad.sin_port = htons (port); //host to network short


//Assegnazione porta e ip alla socket e verifica presenza di eventuali errori
if (bind (MySocket, (struct sockaddr*) & sad, sizeof (sad)) < 0) {
	ErrorHandler ("bind() failed.\n");
	closesocket (MySocket);
	ClearWinSock ();
	return 0;
}


// SETTAGGIO DELLA SOCKET ALL'ASCOLTO
if (listen (MySocket, QLEN) < 0) {
	ErrorHandler ("listen() failed.\n");
	closesocket (MySocket);
	ClearWinSock ();
	return 0;
}

// ACCETTARE UNA NUOVA CONNESSIONE
struct sockaddr_in cad; //structure for the client address
int clientSocket; // socket descriptor for the client
int clientLen; //the size of the client address
printf ("Waiting for a client to connect...");
while (1) {
	clientLen = sizeof (cad); //set the size of the client address
	if ((clientSocket = accept (MySocket, (struct sockaddr*) &cad, &clientLen)) < 0) {
		ErrorHandler ("accept() failed.\n");

		// CHIUSURA DELLA CONNESSIONE
		closesocket (MySocket);
		ClearWinSock ();
		return 0;
	}
	printf ("Handling client %s\n", inet_ntoa (cad.sin_addr));
}
}
Re: C programming socket WSADATA problem [message #1716886 is a reply to message #1716722] Tue, 08 December 2015 14:22 Go to previous messageGo to next message
Eclipse UserFriend
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 07-12-15 19:25, Marco R. wrote:
> I have included in the code and also added wsock32 to the project
> library. It seems not recognizing the WSADATA structure I think.
> What's the problem? Thanks.


http://lmgtfy.com/?q=WSADATA

First hit, library to use is Ws2_32




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlZnLd4ACgkQ0VJKUkHcX78P/gCfbkr+FzhPGVKxD/6w2Hb8RxGo
KnQAniFGztnrj/HilsR18vhFSCy4+zsy
=l1pe
-----END PGP SIGNATURE-----
Re: C programming socket WSADATA problem [message #1717018 is a reply to message #1716886] Wed, 09 December 2015 10:36 Go to previous message
Eclipse UserFriend
Thanks, you are all so pleasant friends.

[Updated on: Wed, 09 December 2015 10:39] by Moderator

Previous Topic:Building CDT plugin
Next Topic:Double backslash in paths
Goto Forum:
  


Current Time: Sat Sep 27 02:45:49 EDT 2025

Powered by FUDForum. Page generated in 0.06895 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top