IRC winsock DLL的问题 [英] Issues with IRC winsock DLL

查看:65
本文介绍了IRC winsock DLL的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用winsock代码编译DLL时,问题相当简单,它会产生大量错误。当它被编译为控制台应用程序并且为什么超出我的原因时,这不会发生。如果有人有建议,请给它。



以下一小部分

 i:\软件stuff \c ++ code\ircclientgui \ircclientbackend\code.h( 17 ):错误C2079 
' theClient'使用未定义的 struct ' sockaddr_in'
i:\ software stuff \c ++ code\ircclientgui\ircclientbackend\code.h( 19 ):错误C2079
' theWSA'使用undefined struct ' WSAData'
i:\软件stuff \c ++ code\ircclientgui \ircclientbackend\code.h( 36 ):错误C2568
' =':无法解决函数重载
i:\软件stuff \c ++ code\ircclientgui \ ircclientbackend \code.h( 36 ):注意:
可以' SOCKET套接字(int,int,int)'
i:\软件stuff \c ++ code\ircclientgui \ircclientbackend\code.h( 41 ):错误C2228
' .sin_addr'的左边必须有class / struct / union
i:\ software stuff \ c ++ code\ircclientgui \ircclientbackend\code.h( 41 ):注意:类型
' int'
i:\ software stuff \ c ++ code\ircclientgui \ircclientbackend\code.h( 41 ):错误C2228
' 。S_un'必须有class / struct / union
i:\软件stuff \c ++ code\ircclientgui \\ \\ rcclientbackend\code.h( 41 ):错误C2228
' < span class =code-string>。S_addr'必须有class / struct / union
i:\ software stuff \ c ++ code\ircclientgui \ircclientbackend\code.h( 42 ):错误C2228
' 的左侧。 sin_family'必须有class / struct / union
i:\ software stuff \ c ++ code\ircclientgui \ircclientbackend\code.h( 42 ):注意:类型
' int'
i:\\ \\软件stuff \c ++ code\ircclientgui \ircclientbackend\code.h( 43 ):错误C2228
' 。sin_port'必须有class / struct / union
i:\软件stuff \ c ++ code\ircclientgui \ircclientbackend\code.h( 43 ):注意:键入
' int'





有问题的代码

  #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(LPVOID lpParameter中的__)
{
char * in = new char [ 1002 < /跨度>];
SOCKET AcceptSocket =(SOCKET)lpParameter;
while true
{
recv(AcceptSocket,< span class =code-keyword> in
1000 0 );
}
// closesocket(AcceptSocket);
return 0 ;
}
void makeSocketLoad()
{
WSAStartup(MAKEWORD( 2 2 ),& theWSA);
theSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
}
// 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





完整代码,如果需要puu.sh/oqyle/ef30e7c575.zip



我尝试过:



根据关于此主题的其他主题移动包含。

解决方案

始终包含 winsock2.h windows.h 之前(或者只是删除 windows.h 包括 winsock2.h )。

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

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

#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.

解决方案

Always include winsock2.h before windows.h (or just remove including of windows.h because it is included by winsock2.h).


这篇关于IRC winsock DLL的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆