Click here to Skip to main content
15,885,953 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The issue is rather simple when I try to compile a DLL with winsock code it produces a huge number of errors. This does not happen when it is compiled as a console app and reason as to why are beyond me. If anyone has advice please give it.

A small fraction of them below
C++
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(17): error C2079
: 'theClient' uses undefined struct 'sockaddr_in'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(19): error C2079
: 'theWSA' uses undefined struct 'WSAData'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(36): error C2568
: '=': unable to resolve function overload
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(36): note: could
 be 'SOCKET socket(int,int,int)'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(41): error C2228
: left of '.sin_addr' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(41): note: type
is 'int'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(41): error C2228
: left of '.S_un' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(41): error C2228
: left of '.S_addr' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(42): error C2228
: left of '.sin_family' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(42): note: type
is 'int'
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(43): error C2228
: left of '.sin_port' must have class/struct/union
i:\software stuff\c++ code\ircclientgui\ircclientbackend\code.h(43): note: type
is 'int'


The code in question
C++
#ifndef _code_h_
#define _code_h_
#include <Windows.h>
#include <Winsock2.h>
#include <WS2tcpip.h>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
#include <thread>
#include <chrono>
#include <future>


namespace classClient
{
	struct sockaddr_in theClient;
	char *buffer = new char[1002];
	WSADATA theWSA;
	SOCKET theSocket,theOtherSocket;	

	DWORD WINAPI ThreadProc(__in  LPVOID lpParameter)
	{
		char *in = new char[1002];
		SOCKET AcceptSocket=(SOCKET) lpParameter;
		while(true)
		{
			recv(AcceptSocket,in,1000,0);
		}
		//closesocket(AcceptSocket);
		return 0;
	}
	void makeSocketLoad()
	{
		WSAStartup(MAKEWORD(2,2),&theWSA);
		theSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	}
	// port of 8888
	void sockConnect(std::string ip,int port)
	{	
		theClient.sin_addr.s_addr = inet_addr(ip.c_str());
		theClient.sin_family = AF_INET;
		theClient.sin_port = htons(port);
		//bind(theSocket,(sockaddr*) &theClient,sizeof(theClient));
		if(connect(theSocket,(SOCKADDR*)(&theClient),sizeof(theClient))!=0)
		{
			WSACleanup();
			system("PAUSE");
		}
	}
	void sendData(std::string data)
	{
		data[data.length()+1] = '\0';
		strcpy(buffer,data.c_str());
		send(theSocket,buffer,1000,0);	
	}
	void getData2()
	{
		DWORD dwThread;
		HANDLE hThread = CreateThread(NULL,0,ThreadProc,(LPVOID)theSocket,0,&dwThread);
		CloseHandle(hThread);
	}
	void getData()
	{
		auto go = std::async (getData2);
		go.get();
	}
	void killWinsock()
	{
		closesocket(theSocket);
		WSACleanup();
	}
}
#endif


The full code if needed puu.sh/oqyle/ef30e7c575.zip

What I have tried:

Moving the includes around in accordance with other threads made on this subject.
Posted
Updated 21-Apr-16 5:28am

1 solution

Always include winsock2.h before windows.h (or just remove including of windows.h because it is included by winsock2.h).
 
Share this answer
 
Comments
Member 10657083 22-Apr-16 4:07am    
Thank you the error wasn't actually in the code but another header file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900