<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[socket server funktioniert als konsolenanwendung, aber nicht als dll]]></title><description><![CDATA[<p>hallo,</p>
<p>habe folgende konsolenanwendung programmiert :</p>
<pre><code class="language-cpp">/* 	Rot13 server example
 *  View with tabsize = 4
 *	Part of the Winsock networking tutorial by Thomas Bleeker
 *	Visit www.MadWizard.org
 */
#include &quot;stdafx.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

#define WIN32_MEAN_AND_LEAN
#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;

using namespace std;

class ROTException
{
public:
    ROTException() :
         m_pMessage(&quot;&quot;) {}
    virtual ~ROTException() {}
    ROTException(const char *pMessage) :
         m_pMessage(pMessage) {}
    const char * what() { return m_pMessage; }
private:
    const char *m_pMessage;
};

const int  REQ_WINSOCK_VER   = 2;	// Minimum winsock version required
const int  DEFAULT_PORT      = 4444;	
const int  TEMP_BUFFER_SIZE  = 128;

string GetHostDescription(const sockaddr_in &amp;sockAddr)
{
	ostringstream stream;
	stream &lt;&lt; inet_ntoa(sockAddr.sin_addr) &lt;&lt; &quot;:&quot; &lt;&lt; ntohs(sockAddr.sin_port);
	return stream.str();
}

void SetServerSockAddr(sockaddr_in *pSockAddr, int portNumber)
{
	// Set family, port and find IP
	pSockAddr-&gt;sin_family = AF_INET;
	pSockAddr-&gt;sin_port = htons(portNumber);
	pSockAddr-&gt;sin_addr.S_un.S_addr = INADDR_ANY;
}

void rot13(char *pBuffer, int size)
{
	for(int i=0;i&lt;size;i++)
	{
		char c = pBuffer[i];
		if ((c &gt;= 'a' &amp;&amp; c &lt; 'n') || (c &gt;= 'A' &amp;&amp; c &lt; 'N') )
			c += 13;
		else if ((c&gt;='n' &amp;&amp; c &lt;= 'z') || (c&gt;='N' &amp;&amp; c &lt;= 'Z'))
			c -= 13;
		else
			continue;
		pBuffer[i] = c;
	}
}

void HandleConnection(SOCKET hClientSocket, const sockaddr_in &amp;sockAddr)
{
	// Print description (IP:port) of connected client
	cout &lt;&lt; &quot;Connected with &quot; &lt;&lt; GetHostDescription(sockAddr) &lt;&lt; &quot;.\n&quot;;

	char tempBuffer[TEMP_BUFFER_SIZE];

	// Read data
	while(true)
	{
		int retval;
		retval = recv(hClientSocket, tempBuffer, sizeof(tempBuffer), 0);
		if (retval==0)
		{ 
			break; // Connection has been closed
		}
		else if (retval==SOCKET_ERROR)
		{
			throw ROTException(&quot;socket error while receiving.&quot;);
		}
		else
		{
			// retval is the number of bytes received.
			  // rot13 the data and send it back to the client */
			for(int i=0;i&lt;retval;i++)
		{
			cout &lt;&lt; tempBuffer[i];

			}
			/*rot13(tempBuffer, retval);

			System::Byte aBytes[];

			String *sString = System::Text::Encoding::ASCII-&gt;GetString(aBytes);

			cout &lt;&lt; rot13;*/

			//if (send(hClientSocket, tempBuffer, retval, 0)==SOCKET_ERROR)
				//throw ROTException(&quot;socket error while sending.&quot;);
		}
	}
	cout &lt;&lt; &quot;Connection closed.\n&quot;;
}

bool RunServer(int portNumber)
{
	SOCKET 		hSocket = INVALID_SOCKET,
				hClientSocket = INVALID_SOCKET;
	bool		bSuccess = true;
	sockaddr_in	sockAddr = {0};

	try
	{
		// Create socket
		cout &lt;&lt; &quot;Creating socket... &quot;;
		if ((hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
			throw ROTException(&quot;could not create socket.&quot;);
		cout &lt;&lt; &quot;created.\n&quot;;

		// Bind socket
		cout &lt;&lt; &quot;Binding socket... &quot;;
		SetServerSockAddr(&amp;sockAddr, portNumber);
		if (bind(hSocket, reinterpret_cast&lt;sockaddr*&gt;(&amp;sockAddr), sizeof(sockAddr))!=0)
			throw ROTException(&quot;could not bind socket.&quot;);
		cout &lt;&lt; &quot;bound.\n&quot;;

		// Put socket in listening mode
		cout &lt;&lt; &quot;Putting socket in listening mode... &quot;;
		if (listen(hSocket, SOMAXCONN)!=0)
			throw ROTException(&quot;could not put socket in listening mode.&quot;);
		cout &lt;&lt; &quot;done.\n&quot;;

		// Wait for connection
		cout &lt;&lt; &quot;Waiting for incoming connection... &quot;;

		sockaddr_in clientSockAddr;
		int			clientSockSize = sizeof(clientSockAddr);

		// Accept connection:
		hClientSocket = accept(hSocket,
						 reinterpret_cast&lt;sockaddr*&gt;(&amp;clientSockAddr),
						 &amp;clientSockSize);

		// Check if accept succeeded
		if (hClientSocket==INVALID_SOCKET)
			throw ROTException(&quot;accept function failed.&quot;);
		cout &lt;&lt; &quot;accepted.\n&quot;;

		// Wait for and accept a connection:
		HandleConnection(hClientSocket, clientSockAddr);

	}
	catch(ROTException e)
	{
		cerr &lt;&lt; &quot;\nError: &quot; &lt;&lt; e.what() &lt;&lt; endl;
		bSuccess = false; 
	}

	if (hSocket!=INVALID_SOCKET)
		closesocket(hSocket);

	if (hClientSocket!=INVALID_SOCKET)
		closesocket(hClientSocket);

	return bSuccess;
}	

int main(int argc, char* argv[])
{ 
	int iRet = 1;
	WSADATA wsaData;

	cout &lt;&lt; &quot;Initializing winsock... &quot;;

	if (WSAStartup(MAKEWORD(REQ_WINSOCK_VER,0), &amp;wsaData)==0)
	{
		// Check if major version is at least REQ_WINSOCK_VER
		if (LOBYTE(wsaData.wVersion) &gt;= REQ_WINSOCK_VER)
		{
			cout &lt;&lt; &quot;initialized.\n&quot;;

			int port = DEFAULT_PORT;
			if (argc &gt; 1)
				port = atoi(argv[1]);
			iRet = !RunServer(port);
		}
		else
		{
			cerr &lt;&lt; &quot;required version not supported!&quot;;
		}

		cout &lt;&lt; &quot;Cleaning up winsock... &quot;;

		// Cleanup winsock
		if (WSACleanup()!=0)
		{
			cerr &lt;&lt; &quot;cleanup failed!\n&quot;;
			iRet = 1;
		}   
		cout &lt;&lt; &quot;done.\n&quot;;
	}
	else
	{
		cerr &lt;&lt; &quot;startup failed!\n&quot;;
	}
	return iRet;
}
</code></pre>
<p>aber die dll version davon wo der empfangene string an die export funktion weitergegeben werden soll, klappt nicht :</p>
<pre><code class="language-cpp">// dllsocket.cpp : Definiert die exportierten Funktionen für die DLL-Anwendung.
//

#include &quot;stdafx.h&quot;
#include &quot;dllsocket.h&quot;
#include &lt;sstream&gt;
#include &lt;windows.h&gt;
#include &lt;winsock2.h&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

#define WIN32_MEAN_AND_LEAN
using namespace std;

static char* str;

class ROTException
{
public:
    ROTException() :
         m_pMessage(&quot;&quot;) {}
    virtual ~ROTException() {}
    ROTException(const char *pMessage) :
         m_pMessage(pMessage) {}
    const char * what() { return m_pMessage; }
private:
    const char *m_pMessage;
};

const int  REQ_WINSOCK_VER   = 2;	// Minimum winsock version required
const int  DEFAULT_PORT      = 4444;	
const int  TEMP_BUFFER_SIZE  = 128;

string GetHostDescription(const sockaddr_in &amp;sockAddr)
{
	ostringstream stream;
	stream &lt;&lt; inet_ntoa(sockAddr.sin_addr) &lt;&lt; &quot;:&quot; &lt;&lt; ntohs(sockAddr.sin_port);
	return stream.str();
}

void SetServerSockAddr(sockaddr_in *pSockAddr, int portNumber)
{
	// Set family, port and find IP
	pSockAddr-&gt;sin_family = AF_INET;
	pSockAddr-&gt;sin_port = htons(portNumber);
	pSockAddr-&gt;sin_addr.S_un.S_addr = INADDR_ANY;
}

void HandleConnection(SOCKET hClientSocket, const sockaddr_in &amp;sockAddr)
{
	// Print description (IP:port) of connected client
	cout &lt;&lt; &quot;Connected with &quot; &lt;&lt; GetHostDescription(sockAddr) &lt;&lt; &quot;.\n&quot;;

	char tempBuffer[TEMP_BUFFER_SIZE];

	// Read data
	while(true)
	{
		int retval;
		retval = recv(hClientSocket, tempBuffer, sizeof(tempBuffer), 0);
		if (retval==0)
		{ 
			break; // Connection has been closed
		}
		else if (retval==SOCKET_ERROR)
		{
			throw ROTException(&quot;socket error while receiving.&quot;);
		}
		else
		{
			// retval is the number of bytes received.
			  // rot13 the data and send it back to the client */
			str=&quot;&quot;;
			for(int i=0;i&lt;retval;i++)
			{
			str=str+tempBuffer[i];
			}
			/*rot13(tempBuffer, retval);

			System::Byte aBytes[];

			String *sString = System::Text::Encoding::ASCII-&gt;GetString(aBytes);

			cout &lt;&lt; rot13;*/

			//if (send(hClientSocket, tempBuffer, retval, 0)==SOCKET_ERROR)
				//throw ROTException(&quot;socket error while sending.&quot;);
		}
	}
	cout &lt;&lt; &quot;Connection closed.\n&quot;;
}

bool RunServer(int portNumber)
{
	SOCKET 		hSocket = INVALID_SOCKET,
				hClientSocket = INVALID_SOCKET;
	bool		bSuccess = true;
	sockaddr_in	sockAddr = {0};

	try
	{
		// Create socket
		cout &lt;&lt; &quot;Creating socket... &quot;;
		if ((hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
			throw ROTException(&quot;could not create socket.&quot;);
		cout &lt;&lt; &quot;created.\n&quot;;

		// Bind socket
		cout &lt;&lt; &quot;Binding socket... &quot;;
		SetServerSockAddr(&amp;sockAddr, portNumber);
		if (bind(hSocket, reinterpret_cast&lt;sockaddr*&gt;(&amp;sockAddr), sizeof(sockAddr))!=0)
			throw ROTException(&quot;could not bind socket.&quot;);
		cout &lt;&lt; &quot;bound.\n&quot;;

		// Put socket in listening mode
		cout &lt;&lt; &quot;Putting socket in listening mode... &quot;;
		if (listen(hSocket, SOMAXCONN)!=0)
			throw ROTException(&quot;could not put socket in listening mode.&quot;);
		cout &lt;&lt; &quot;done.\n&quot;;

		// Wait for connection
		cout &lt;&lt; &quot;Waiting for incoming connection... &quot;;

		sockaddr_in clientSockAddr;
		int			clientSockSize = sizeof(clientSockAddr);

		// Accept connection:
		hClientSocket = accept(hSocket,
						 reinterpret_cast&lt;sockaddr*&gt;(&amp;clientSockAddr),
						 &amp;clientSockSize);

		// Check if accept succeeded
		if (hClientSocket==INVALID_SOCKET)
			throw ROTException(&quot;accept function failed.&quot;);
		cout &lt;&lt; &quot;accepted.\n&quot;;

		// Wait for and accept a connection:
		HandleConnection(hClientSocket, clientSockAddr);

	}
	catch(ROTException e)
	{
		cerr &lt;&lt; &quot;\nError: &quot; &lt;&lt; e.what() &lt;&lt; endl;
		bSuccess = false; 
	}

	if (hSocket!=INVALID_SOCKET)
		closesocket(hSocket);

	if (hClientSocket!=INVALID_SOCKET)
		closesocket(hClientSocket);

	return bSuccess;
}	

// Dies ist das Beispiel einer exportierten Variable.
DLLSOCKET_API int ndllsocket=0;

// Dies ist das Beispiel einer exportierten Funktion.
DLLSOCKET_API char* fndllsocket(void)
{
	int iRet = 1;
	WSADATA wsaData;

	cout &lt;&lt; &quot;Initializing winsock... &quot;;

	if (WSAStartup(MAKEWORD(REQ_WINSOCK_VER,0), &amp;wsaData)==0)
	{
		// Check if major version is at least REQ_WINSOCK_VER
		if (LOBYTE(wsaData.wVersion) &gt;= REQ_WINSOCK_VER)
		{
			cout &lt;&lt; &quot;initialized.\n&quot;;

			int port = DEFAULT_PORT;

			iRet = !RunServer(port);
		}
		else
		{
			cerr &lt;&lt; &quot;required version not supported!&quot;;
		}

		cout &lt;&lt; &quot;Cleaning up winsock... &quot;;

		// Cleanup winsock
		 //int Desinfektionsloesung::berechneKonzentratanteil()
		if (WSACleanup()!=0)
		{
			cerr &lt;&lt; &quot;cleanup failed!\n&quot;;
			iRet = 1;
		}   
		cout &lt;&lt; &quot;done.\n&quot;;
	}
	else
	{
		cerr &lt;&lt; &quot;startup failed!\n&quot;;
	}
	return str;
}

// Dies ist der Konstruktor einer Klasse, die exportiert wurde.
// Siehe dllsocket.h für die Klassendefinition.
Cdllsocket::Cdllsocket()
{
	return;
}
</code></pre>
<p>ich bekomme folgende fehlermeldung :</p>
<pre><code class="language-cpp">1&gt;------ Erstellen gestartet: Projekt: dllsocket, Konfiguration: Debug Win32 ------
1&gt;  dllsocket.cpp
1&gt;     Bibliothek &quot;c:\users\user\documents\visual studio 2010\Projects\dllsocket\Debug\dllsocket.lib&quot; und Objekt &quot;c:\users\user\documents\visual studio 2010\Projects\dllsocket\Debug\dllsocket.exp&quot; werden erstellt.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__inet_ntoa@4&quot; in Funktion &quot;&quot;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; __cdecl GetHostDescription(struct sockaddr_in const &amp;)&quot; (?GetHostDescription@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABUsockaddr_in@@@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__ntohs@4&quot; in Funktion &quot;&quot;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; __cdecl GetHostDescription(struct sockaddr_in const &amp;)&quot; (?GetHostDescription@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABUsockaddr_in@@@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__htons@4&quot; in Funktion &quot;&quot;void __cdecl SetServerSockAddr(struct sockaddr_in *,int)&quot; (?SetServerSockAddr@@YAXPAUsockaddr_in@@H@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__recv@16&quot; in Funktion &quot;&quot;void __cdecl HandleConnection(unsigned int,struct sockaddr_in const &amp;)&quot; (?HandleConnection@@YAXIABUsockaddr_in@@@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__closesocket@4&quot; in Funktion &quot;__catch$?RunServer@@YA_NH@Z$0&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__accept@12&quot; in Funktion &quot;&quot;bool __cdecl RunServer(int)&quot; (?RunServer@@YA_NH@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__listen@8&quot; in Funktion &quot;&quot;bool __cdecl RunServer(int)&quot; (?RunServer@@YA_NH@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__bind@12&quot; in Funktion &quot;&quot;bool __cdecl RunServer(int)&quot; (?RunServer@@YA_NH@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__socket@12&quot; in Funktion &quot;&quot;bool __cdecl RunServer(int)&quot; (?RunServer@@YA_NH@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__WSACleanup@0&quot; in Funktion &quot;&quot;char * __cdecl fndllsocket(void)&quot; (?fndllsocket@@YAPADXZ)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__WSAStartup@8&quot; in Funktion &quot;&quot;char * __cdecl fndllsocket(void)&quot; (?fndllsocket@@YAPADXZ)&quot;.
1&gt;c:\users\user\documents\visual studio 2010\Projects\dllsocket\Debug\dllsocket.dll : fatal error LNK1120: 11 nicht aufgelöste externe Verweise.
========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/278698/socket-server-funktioniert-als-konsolenanwendung-aber-nicht-als-dll</link><generator>RSS for Node</generator><lastBuildDate>Tue, 25 Aug 2026 02:16:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/278698.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 Dec 2010 12:17:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to socket server funktioniert als konsolenanwendung, aber nicht als dll on Sat, 11 Dec 2010 12:17:34 GMT]]></title><description><![CDATA[<p>hallo,</p>
<p>habe folgende konsolenanwendung programmiert :</p>
<pre><code class="language-cpp">/* 	Rot13 server example
 *  View with tabsize = 4
 *	Part of the Winsock networking tutorial by Thomas Bleeker
 *	Visit www.MadWizard.org
 */
#include &quot;stdafx.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

#define WIN32_MEAN_AND_LEAN
#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;

using namespace std;

class ROTException
{
public:
    ROTException() :
         m_pMessage(&quot;&quot;) {}
    virtual ~ROTException() {}
    ROTException(const char *pMessage) :
         m_pMessage(pMessage) {}
    const char * what() { return m_pMessage; }
private:
    const char *m_pMessage;
};

const int  REQ_WINSOCK_VER   = 2;	// Minimum winsock version required
const int  DEFAULT_PORT      = 4444;	
const int  TEMP_BUFFER_SIZE  = 128;

string GetHostDescription(const sockaddr_in &amp;sockAddr)
{
	ostringstream stream;
	stream &lt;&lt; inet_ntoa(sockAddr.sin_addr) &lt;&lt; &quot;:&quot; &lt;&lt; ntohs(sockAddr.sin_port);
	return stream.str();
}

void SetServerSockAddr(sockaddr_in *pSockAddr, int portNumber)
{
	// Set family, port and find IP
	pSockAddr-&gt;sin_family = AF_INET;
	pSockAddr-&gt;sin_port = htons(portNumber);
	pSockAddr-&gt;sin_addr.S_un.S_addr = INADDR_ANY;
}

void rot13(char *pBuffer, int size)
{
	for(int i=0;i&lt;size;i++)
	{
		char c = pBuffer[i];
		if ((c &gt;= 'a' &amp;&amp; c &lt; 'n') || (c &gt;= 'A' &amp;&amp; c &lt; 'N') )
			c += 13;
		else if ((c&gt;='n' &amp;&amp; c &lt;= 'z') || (c&gt;='N' &amp;&amp; c &lt;= 'Z'))
			c -= 13;
		else
			continue;
		pBuffer[i] = c;
	}
}

void HandleConnection(SOCKET hClientSocket, const sockaddr_in &amp;sockAddr)
{
	// Print description (IP:port) of connected client
	cout &lt;&lt; &quot;Connected with &quot; &lt;&lt; GetHostDescription(sockAddr) &lt;&lt; &quot;.\n&quot;;

	char tempBuffer[TEMP_BUFFER_SIZE];

	// Read data
	while(true)
	{
		int retval;
		retval = recv(hClientSocket, tempBuffer, sizeof(tempBuffer), 0);
		if (retval==0)
		{ 
			break; // Connection has been closed
		}
		else if (retval==SOCKET_ERROR)
		{
			throw ROTException(&quot;socket error while receiving.&quot;);
		}
		else
		{
			// retval is the number of bytes received.
			  // rot13 the data and send it back to the client */
			for(int i=0;i&lt;retval;i++)
		{
			cout &lt;&lt; tempBuffer[i];

			}
			/*rot13(tempBuffer, retval);

			System::Byte aBytes[];

			String *sString = System::Text::Encoding::ASCII-&gt;GetString(aBytes);

			cout &lt;&lt; rot13;*/

			//if (send(hClientSocket, tempBuffer, retval, 0)==SOCKET_ERROR)
				//throw ROTException(&quot;socket error while sending.&quot;);
		}
	}
	cout &lt;&lt; &quot;Connection closed.\n&quot;;
}

bool RunServer(int portNumber)
{
	SOCKET 		hSocket = INVALID_SOCKET,
				hClientSocket = INVALID_SOCKET;
	bool		bSuccess = true;
	sockaddr_in	sockAddr = {0};

	try
	{
		// Create socket
		cout &lt;&lt; &quot;Creating socket... &quot;;
		if ((hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
			throw ROTException(&quot;could not create socket.&quot;);
		cout &lt;&lt; &quot;created.\n&quot;;

		// Bind socket
		cout &lt;&lt; &quot;Binding socket... &quot;;
		SetServerSockAddr(&amp;sockAddr, portNumber);
		if (bind(hSocket, reinterpret_cast&lt;sockaddr*&gt;(&amp;sockAddr), sizeof(sockAddr))!=0)
			throw ROTException(&quot;could not bind socket.&quot;);
		cout &lt;&lt; &quot;bound.\n&quot;;

		// Put socket in listening mode
		cout &lt;&lt; &quot;Putting socket in listening mode... &quot;;
		if (listen(hSocket, SOMAXCONN)!=0)
			throw ROTException(&quot;could not put socket in listening mode.&quot;);
		cout &lt;&lt; &quot;done.\n&quot;;

		// Wait for connection
		cout &lt;&lt; &quot;Waiting for incoming connection... &quot;;

		sockaddr_in clientSockAddr;
		int			clientSockSize = sizeof(clientSockAddr);

		// Accept connection:
		hClientSocket = accept(hSocket,
						 reinterpret_cast&lt;sockaddr*&gt;(&amp;clientSockAddr),
						 &amp;clientSockSize);

		// Check if accept succeeded
		if (hClientSocket==INVALID_SOCKET)
			throw ROTException(&quot;accept function failed.&quot;);
		cout &lt;&lt; &quot;accepted.\n&quot;;

		// Wait for and accept a connection:
		HandleConnection(hClientSocket, clientSockAddr);

	}
	catch(ROTException e)
	{
		cerr &lt;&lt; &quot;\nError: &quot; &lt;&lt; e.what() &lt;&lt; endl;
		bSuccess = false; 
	}

	if (hSocket!=INVALID_SOCKET)
		closesocket(hSocket);

	if (hClientSocket!=INVALID_SOCKET)
		closesocket(hClientSocket);

	return bSuccess;
}	

int main(int argc, char* argv[])
{ 
	int iRet = 1;
	WSADATA wsaData;

	cout &lt;&lt; &quot;Initializing winsock... &quot;;

	if (WSAStartup(MAKEWORD(REQ_WINSOCK_VER,0), &amp;wsaData)==0)
	{
		// Check if major version is at least REQ_WINSOCK_VER
		if (LOBYTE(wsaData.wVersion) &gt;= REQ_WINSOCK_VER)
		{
			cout &lt;&lt; &quot;initialized.\n&quot;;

			int port = DEFAULT_PORT;
			if (argc &gt; 1)
				port = atoi(argv[1]);
			iRet = !RunServer(port);
		}
		else
		{
			cerr &lt;&lt; &quot;required version not supported!&quot;;
		}

		cout &lt;&lt; &quot;Cleaning up winsock... &quot;;

		// Cleanup winsock
		if (WSACleanup()!=0)
		{
			cerr &lt;&lt; &quot;cleanup failed!\n&quot;;
			iRet = 1;
		}   
		cout &lt;&lt; &quot;done.\n&quot;;
	}
	else
	{
		cerr &lt;&lt; &quot;startup failed!\n&quot;;
	}
	return iRet;
}
</code></pre>
<p>aber die dll version davon wo der empfangene string an die export funktion weitergegeben werden soll, klappt nicht :</p>
<pre><code class="language-cpp">// dllsocket.cpp : Definiert die exportierten Funktionen für die DLL-Anwendung.
//

#include &quot;stdafx.h&quot;
#include &quot;dllsocket.h&quot;
#include &lt;sstream&gt;
#include &lt;windows.h&gt;
#include &lt;winsock2.h&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

#define WIN32_MEAN_AND_LEAN
using namespace std;

static char* str;

class ROTException
{
public:
    ROTException() :
         m_pMessage(&quot;&quot;) {}
    virtual ~ROTException() {}
    ROTException(const char *pMessage) :
         m_pMessage(pMessage) {}
    const char * what() { return m_pMessage; }
private:
    const char *m_pMessage;
};

const int  REQ_WINSOCK_VER   = 2;	// Minimum winsock version required
const int  DEFAULT_PORT      = 4444;	
const int  TEMP_BUFFER_SIZE  = 128;

string GetHostDescription(const sockaddr_in &amp;sockAddr)
{
	ostringstream stream;
	stream &lt;&lt; inet_ntoa(sockAddr.sin_addr) &lt;&lt; &quot;:&quot; &lt;&lt; ntohs(sockAddr.sin_port);
	return stream.str();
}

void SetServerSockAddr(sockaddr_in *pSockAddr, int portNumber)
{
	// Set family, port and find IP
	pSockAddr-&gt;sin_family = AF_INET;
	pSockAddr-&gt;sin_port = htons(portNumber);
	pSockAddr-&gt;sin_addr.S_un.S_addr = INADDR_ANY;
}

void HandleConnection(SOCKET hClientSocket, const sockaddr_in &amp;sockAddr)
{
	// Print description (IP:port) of connected client
	cout &lt;&lt; &quot;Connected with &quot; &lt;&lt; GetHostDescription(sockAddr) &lt;&lt; &quot;.\n&quot;;

	char tempBuffer[TEMP_BUFFER_SIZE];

	// Read data
	while(true)
	{
		int retval;
		retval = recv(hClientSocket, tempBuffer, sizeof(tempBuffer), 0);
		if (retval==0)
		{ 
			break; // Connection has been closed
		}
		else if (retval==SOCKET_ERROR)
		{
			throw ROTException(&quot;socket error while receiving.&quot;);
		}
		else
		{
			// retval is the number of bytes received.
			  // rot13 the data and send it back to the client */
			str=&quot;&quot;;
			for(int i=0;i&lt;retval;i++)
			{
			str=str+tempBuffer[i];
			}
			/*rot13(tempBuffer, retval);

			System::Byte aBytes[];

			String *sString = System::Text::Encoding::ASCII-&gt;GetString(aBytes);

			cout &lt;&lt; rot13;*/

			//if (send(hClientSocket, tempBuffer, retval, 0)==SOCKET_ERROR)
				//throw ROTException(&quot;socket error while sending.&quot;);
		}
	}
	cout &lt;&lt; &quot;Connection closed.\n&quot;;
}

bool RunServer(int portNumber)
{
	SOCKET 		hSocket = INVALID_SOCKET,
				hClientSocket = INVALID_SOCKET;
	bool		bSuccess = true;
	sockaddr_in	sockAddr = {0};

	try
	{
		// Create socket
		cout &lt;&lt; &quot;Creating socket... &quot;;
		if ((hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
			throw ROTException(&quot;could not create socket.&quot;);
		cout &lt;&lt; &quot;created.\n&quot;;

		// Bind socket
		cout &lt;&lt; &quot;Binding socket... &quot;;
		SetServerSockAddr(&amp;sockAddr, portNumber);
		if (bind(hSocket, reinterpret_cast&lt;sockaddr*&gt;(&amp;sockAddr), sizeof(sockAddr))!=0)
			throw ROTException(&quot;could not bind socket.&quot;);
		cout &lt;&lt; &quot;bound.\n&quot;;

		// Put socket in listening mode
		cout &lt;&lt; &quot;Putting socket in listening mode... &quot;;
		if (listen(hSocket, SOMAXCONN)!=0)
			throw ROTException(&quot;could not put socket in listening mode.&quot;);
		cout &lt;&lt; &quot;done.\n&quot;;

		// Wait for connection
		cout &lt;&lt; &quot;Waiting for incoming connection... &quot;;

		sockaddr_in clientSockAddr;
		int			clientSockSize = sizeof(clientSockAddr);

		// Accept connection:
		hClientSocket = accept(hSocket,
						 reinterpret_cast&lt;sockaddr*&gt;(&amp;clientSockAddr),
						 &amp;clientSockSize);

		// Check if accept succeeded
		if (hClientSocket==INVALID_SOCKET)
			throw ROTException(&quot;accept function failed.&quot;);
		cout &lt;&lt; &quot;accepted.\n&quot;;

		// Wait for and accept a connection:
		HandleConnection(hClientSocket, clientSockAddr);

	}
	catch(ROTException e)
	{
		cerr &lt;&lt; &quot;\nError: &quot; &lt;&lt; e.what() &lt;&lt; endl;
		bSuccess = false; 
	}

	if (hSocket!=INVALID_SOCKET)
		closesocket(hSocket);

	if (hClientSocket!=INVALID_SOCKET)
		closesocket(hClientSocket);

	return bSuccess;
}	

// Dies ist das Beispiel einer exportierten Variable.
DLLSOCKET_API int ndllsocket=0;

// Dies ist das Beispiel einer exportierten Funktion.
DLLSOCKET_API char* fndllsocket(void)
{
	int iRet = 1;
	WSADATA wsaData;

	cout &lt;&lt; &quot;Initializing winsock... &quot;;

	if (WSAStartup(MAKEWORD(REQ_WINSOCK_VER,0), &amp;wsaData)==0)
	{
		// Check if major version is at least REQ_WINSOCK_VER
		if (LOBYTE(wsaData.wVersion) &gt;= REQ_WINSOCK_VER)
		{
			cout &lt;&lt; &quot;initialized.\n&quot;;

			int port = DEFAULT_PORT;

			iRet = !RunServer(port);
		}
		else
		{
			cerr &lt;&lt; &quot;required version not supported!&quot;;
		}

		cout &lt;&lt; &quot;Cleaning up winsock... &quot;;

		// Cleanup winsock
		 //int Desinfektionsloesung::berechneKonzentratanteil()
		if (WSACleanup()!=0)
		{
			cerr &lt;&lt; &quot;cleanup failed!\n&quot;;
			iRet = 1;
		}   
		cout &lt;&lt; &quot;done.\n&quot;;
	}
	else
	{
		cerr &lt;&lt; &quot;startup failed!\n&quot;;
	}
	return str;
}

// Dies ist der Konstruktor einer Klasse, die exportiert wurde.
// Siehe dllsocket.h für die Klassendefinition.
Cdllsocket::Cdllsocket()
{
	return;
}
</code></pre>
<p>ich bekomme folgende fehlermeldung :</p>
<pre><code class="language-cpp">1&gt;------ Erstellen gestartet: Projekt: dllsocket, Konfiguration: Debug Win32 ------
1&gt;  dllsocket.cpp
1&gt;     Bibliothek &quot;c:\users\user\documents\visual studio 2010\Projects\dllsocket\Debug\dllsocket.lib&quot; und Objekt &quot;c:\users\user\documents\visual studio 2010\Projects\dllsocket\Debug\dllsocket.exp&quot; werden erstellt.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__inet_ntoa@4&quot; in Funktion &quot;&quot;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; __cdecl GetHostDescription(struct sockaddr_in const &amp;)&quot; (?GetHostDescription@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABUsockaddr_in@@@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__ntohs@4&quot; in Funktion &quot;&quot;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; __cdecl GetHostDescription(struct sockaddr_in const &amp;)&quot; (?GetHostDescription@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABUsockaddr_in@@@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__htons@4&quot; in Funktion &quot;&quot;void __cdecl SetServerSockAddr(struct sockaddr_in *,int)&quot; (?SetServerSockAddr@@YAXPAUsockaddr_in@@H@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__recv@16&quot; in Funktion &quot;&quot;void __cdecl HandleConnection(unsigned int,struct sockaddr_in const &amp;)&quot; (?HandleConnection@@YAXIABUsockaddr_in@@@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__closesocket@4&quot; in Funktion &quot;__catch$?RunServer@@YA_NH@Z$0&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__accept@12&quot; in Funktion &quot;&quot;bool __cdecl RunServer(int)&quot; (?RunServer@@YA_NH@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__listen@8&quot; in Funktion &quot;&quot;bool __cdecl RunServer(int)&quot; (?RunServer@@YA_NH@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__bind@12&quot; in Funktion &quot;&quot;bool __cdecl RunServer(int)&quot; (?RunServer@@YA_NH@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__socket@12&quot; in Funktion &quot;&quot;bool __cdecl RunServer(int)&quot; (?RunServer@@YA_NH@Z)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__WSACleanup@0&quot; in Funktion &quot;&quot;char * __cdecl fndllsocket(void)&quot; (?fndllsocket@@YAPADXZ)&quot;.
1&gt;dllsocket.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol &quot;__imp__WSAStartup@8&quot; in Funktion &quot;&quot;char * __cdecl fndllsocket(void)&quot; (?fndllsocket@@YAPADXZ)&quot;.
1&gt;c:\users\user\documents\visual studio 2010\Projects\dllsocket\Debug\dllsocket.dll : fatal error LNK1120: 11 nicht aufgelöste externe Verweise.
========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1992932</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992932</guid><dc:creator><![CDATA[bonzenbauer]]></dc:creator><pubDate>Sat, 11 Dec 2010 12:17:34 GMT</pubDate></item><item><title><![CDATA[Reply to socket server funktioniert als konsolenanwendung, aber nicht als dll on Sat, 11 Dec 2010 12:30:05 GMT]]></title><description><![CDATA[<p>Das sieht so aus, als ob Du die ws2_32.lib nicht mitlinken würdest.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992936</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992936</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Sat, 11 Dec 2010 12:30:05 GMT</pubDate></item><item><title><![CDATA[Reply to socket server funktioniert als konsolenanwendung, aber nicht als dll on Sat, 11 Dec 2010 13:53:28 GMT]]></title><description><![CDATA[<p>hab ich gemacht, danke für die hilfe,</p>
<p>jetzt habe ich ein neues problem, is glaube ich relativ simpel ...</p>
<p>habe ein char array tempBuffer</p>
<p>und das soll in char* __stdcall umgewandelt werden,</p>
<p>immer wenn ich die dll aufrufe bekomme ich die meldung</p>
<p>clared with a different calling convention</p>
<p>sowas wie</p>
<p>char* __stdcall st;</p>
<p>st=st+tempbuffer[0] und dann für tempbuffer[1] inner schleife</p>
<p>klappt nicht ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992963</guid><dc:creator><![CDATA[bonzenbauer]]></dc:creator><pubDate>Sat, 11 Dec 2010 13:53:28 GMT</pubDate></item><item><title><![CDATA[Reply to socket server funktioniert als konsolenanwendung, aber nicht als dll on Sat, 11 Dec 2010 14:41:08 GMT]]></title><description><![CDATA[<p>also in java sage ich folgendes :</p>
<p>String df=&quot;dssffffffffffffffffffffffffffdsd&quot;;<br />
byte[] theByteArray = df.getBytes();<br />
output.write(theByteArray);</p>
<p>heraus kommt :</p>
<p>100<br />
115<br />
115<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
102<br />
100<br />
115<br />
100</p>
<p>so das sind die ascii werte nehme ich an</p>
<p>und dies soll in der c++ dll wieder in einen string umgewandelt werden</p>
<p>und zwar in diese format :</p>
<p>wenn bei der c++ dll es einen tempBuffer char array gibt</p>
<pre><code class="language-cpp">char* __stdcall
</code></pre>
<p>wie geht das?</p>
<p>ich bekomme ständig error das entweder code nicht lesbar programm stürzt ab und so weiter</p>
<p>wenn ich einen einfachen testtring nehme :</p>
<p>char* __stdcall test= &quot;test&quot;;</p>
<p>klappt es aber std::string führt zum absturz des programms welches die dll-funktion aufurft</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992975</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992975</guid><dc:creator><![CDATA[bonzenbauer]]></dc:creator><pubDate>Sat, 11 Dec 2010 14:41:08 GMT</pubDate></item></channel></rss>