<?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[Threadproblem bei Server-Client-Programm]]></title><description><![CDATA[<p>Hallo Community,</p>
<p>Ich schreibe grade ein kleines Netzwerktestprogramm. Dabei sollen sich mehrere Clients gleichzeitig auf einem Server anmelden können und Nachrichten an den Server senden, der die Nachricht an alle Clients weitergibt (Das hab ich aber bisher noch nicht gemacht, ist aber mein Ziel).</p>
<p>Um zu realisieren, dass mehrere Clients sich anmelden können, benutze ich Threads aus der boost-Library. Zur Realisierung dieser Threads habe ich eine Klasse &quot;ServerConnection&quot; geschrieben, welche von den thread erbt und in der Endlosschleife des Servers ein ServerConnection-Objekt erzeugt.</p>
<p>Ich zeig euch erst einmal den Code dazu:</p>
<pre><code class="language-cpp">/* server_main.cpp */
/* Die main-Datei des Servers, welche aufgerufen wird */

#include &quot;ServerSocket.h&quot;
#include &quot;ClientSocket.h&quot;
#include &quot;ServerConnection.h&quot;
#include &quot;SocketException.h&quot;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

void ServerConnection::calc() {

	/*std::stringstream ss;
	std::string data;
	clientSocket &gt;&gt; data;
	ss &lt;&lt; &quot;Empfangen: &quot; &lt;&lt; data &lt;&lt; &quot;\n&quot;;

	clientSocket &lt;&lt; ss.str();
*/
	std::cout &lt;&lt; &quot;Ich bin die Methode calc()&quot; &lt;&lt; std::endl;
 }

int main ( int argc, char**argv )
{
	std::cout &lt;&lt; &quot;running....\n&quot;;

	try
	{
		// 1. Create the socket
		// 2. Bind Socket to Port 30000
		// 3. Set Socket to Listen
		ServerSocket server ( 30000 );

		while ( true )
		{
			ClientSocket clientSock;
			server.accept ( clientSock );
			/* Hier wird die Connection erstellt. Diese gibt die Funktion calc() an 
			 * den Thread weiter und den akzeptierten Socket */
			ServerConnection sc(clientSock, &amp;calc);
			sc.join();

		}
	}
	catch ( SocketException&amp; e )
	{
		std::cout &lt;&lt; &quot;Exception was caught:&quot; &lt;&lt; e.description() &lt;&lt; &quot;\nExiting.\n&quot;;
	}

	return 0;
}
</code></pre>
<p>Die Klasse ServerConnection:</p>
<pre><code class="language-cpp">#ifndef SERVERCONNECTION_H_
#define SERVERCONNECTION_H_

#include &lt;boost/thread/thread.hpp&gt;
#include &quot;ClientSocket.h&quot;

class ServerConnection : public boost::thread {

public:

	template&lt;typename Callable&gt;
	ServerConnection(const ClientSocket&amp; csocket, const Callable func)
: boost::thread(func), clientSocket(csocket)  { }

	virtual ~ServerConnection() { }

	void calc();
private:
	ClientSocket clientSocket;

};

#endif /* SERVERCONNECTION_H_ */
</code></pre>
<p>ClientSocket sieht so aus:</p>
<pre><code class="language-cpp">/*
 * ClientSocket.h
 *
 *  Created on: May 12, 2011
 *      Author: lumbeck
 */

#ifndef CLIENTSOCKET_H_
#define CLIENTSOCKET_H_

#include &quot;Socket.h&quot;

class ClientSocket : public Socket
{
 public:

  ClientSocket ( std::string host, int port );
  ClientSocket() { }
  virtual ~ClientSocket() {};

  const ClientSocket&amp; operator &lt;&lt; ( const std::string&amp; ) const;
  const ClientSocket&amp; operator &gt;&gt; ( std::string&amp; ) const;

};

#endif /* CLIENTSOCKET_H_ */
</code></pre>
<p>Und hier auch wieder die zugehörige cpp-Datei</p>
<pre><code class="language-cpp">// Implementation of the ClientSocket class

#include &quot;ClientSocket.h&quot;
#include &quot;SocketException.h&quot;

ClientSocket::ClientSocket ( std::string host, int port )
{
  if ( ! Socket::create() )
    {
throw SocketException ( &quot;Could not create client socket.&quot; );
    }

  if ( ! Socket::connect ( host, port ) )
    {
      throw SocketException ( &quot;Could not bind to port.&quot; );
    }
}

const ClientSocket&amp; ClientSocket::operator &lt;&lt; ( const std::string&amp; s ) const
{
  if ( ! Socket::send ( s ) )
    {
      throw SocketException ( &quot;Could not write to socket.&quot; );
    }
  return *this;
}

const ClientSocket&amp; ClientSocket::operator &gt;&gt; ( std::string&amp; s ) const
{
  if ( ! Socket::recv ( s ) )
    {
      throw SocketException ( &quot;Could not read from socket.&quot; );
    }
  return *this;
}
</code></pre>
<p>ServerSocket sieht so aus:</p>
<pre><code class="language-cpp">#ifndef SERVERSOCKET_H_
#define SERVERSOCKET_H_

#include &quot;Socket.h&quot;

class ServerSocket : public Socket
{
 public:

  ServerSocket ( int port );
  ServerSocket (){};
  virtual ~ServerSocket();

  const ServerSocket&amp; operator &lt;&lt; ( const std::string&amp; );
  const ServerSocket&amp; operator &gt;&gt; ( std::string&amp; );

 void accept ( Socket&amp; );

};

#endif /* SERVERSOCKET_H_ */
</code></pre>
<pre><code class="language-cpp">/*
 * ServerSocket.cpp
 */

// Implementation of the ServerSocket class

#include &quot;ServerSocket.h&quot;
#include &quot;SocketException.h&quot;
#include &lt;sstream&gt;

ServerSocket::ServerSocket ( int port )
{
  if ( ! Socket::create() )
    {
      throw SocketException ( &quot;Could not create server socket.&quot; );
    }

  if ( ! Socket::bind ( port ) )
    {
      throw SocketException ( &quot;Could not bind to port.&quot; );
    }

  if ( ! Socket::listen() )
    {
      throw SocketException ( &quot;Could not listen to socket.&quot; );
    }
}

ServerSocket::~ServerSocket()
{
}

const ServerSocket&amp; ServerSocket::operator &lt;&lt; ( const std::string&amp; s )
{
  if ( ! Socket::send ( s) )
    {
      throw SocketException ( &quot;Could not write to socket.&quot; );
    }
  return *this;
}

const ServerSocket&amp; ServerSocket::operator &gt;&gt; ( std::string&amp; s )
{
  if ( ! Socket::recv ( s ) )
    {
      throw SocketException ( &quot;Could not read from socket.&quot; );
    }
  return *this;
}

void ServerSocket::accept ( Socket&amp; sock )
{
  if ( ! Socket::accept ( sock ) )
    {
      throw SocketException ( &quot;Could not accept socket.&quot; );
    }
}
</code></pre>
<p>Ohne das Threading funktioniert es mit dem Echo-Server (also Client schickt Nachricht und bekommt sie sofort zurück).</p>
<p>Hier ist aber folgendes Problem beim Kompilieren des Servers:</p>
<pre><code>g++  -o server Socket.cpp network.cpp server_main.cpp ServerSocket.cpp ServerConnection.cpp
/tmp/ccD7KxdR.o: In function `main':
server_main.cpp:(.text+0x16e): undefined reference to `ServerConnection::ServerConnection&lt;void (ServerConnection::*)()&gt;(ClientSocket const&amp;,void (ServerConnection::*)())'
server_main.cpp:(.text+0x17e): undefined reference to `boost::thread::join()'
/tmp/ccD7KxdR.o: In function `ServerConnection::~ServerConnection()':
server_main.cpp:(.text._ZN16ServerConnectionD1Ev[ServerConnection::~ServerConnection()]+0x38): undefined reference to `boost::thread::~thread()'
server_main.cpp:(.text._ZN16ServerConnectionD1Ev[ServerConnection::~ServerConnection()]+0x63): undefined reference to `boost::thread::~thread()'
/tmp/ccD7KxdR.o: In function `ServerConnection::~ServerConnection()':
server_main.cpp:(.text._ZN16ServerConnectionD0Ev[ServerConnection::~ServerConnection()]+0x38): undefined reference to `boost::thread::~thread()'
server_main.cpp:(.text._ZN16ServerConnectionD0Ev[ServerConnection::~ServerConnection()]+0x63): undefined reference to `boost::thread::~thread()'
collect2: ld returned 1 exit status
make: *** [server] Error 1
</code></pre>
<p>Ich möchte die Methode calc() der ServerConnection an die Connection weitergeben. Warum klappt das nicht? Ich hoffe, mein Problem ist verständlich...</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/286845/threadproblem-bei-server-client-programm</link><generator>RSS for Node</generator><lastBuildDate>Thu, 20 Aug 2026 08:40:24 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/286845.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 17 May 2011 12:53:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 14:48:43 GMT]]></title><description><![CDATA[<p>Hallo Community,</p>
<p>Ich schreibe grade ein kleines Netzwerktestprogramm. Dabei sollen sich mehrere Clients gleichzeitig auf einem Server anmelden können und Nachrichten an den Server senden, der die Nachricht an alle Clients weitergibt (Das hab ich aber bisher noch nicht gemacht, ist aber mein Ziel).</p>
<p>Um zu realisieren, dass mehrere Clients sich anmelden können, benutze ich Threads aus der boost-Library. Zur Realisierung dieser Threads habe ich eine Klasse &quot;ServerConnection&quot; geschrieben, welche von den thread erbt und in der Endlosschleife des Servers ein ServerConnection-Objekt erzeugt.</p>
<p>Ich zeig euch erst einmal den Code dazu:</p>
<pre><code class="language-cpp">/* server_main.cpp */
/* Die main-Datei des Servers, welche aufgerufen wird */

#include &quot;ServerSocket.h&quot;
#include &quot;ClientSocket.h&quot;
#include &quot;ServerConnection.h&quot;
#include &quot;SocketException.h&quot;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

void ServerConnection::calc() {

	/*std::stringstream ss;
	std::string data;
	clientSocket &gt;&gt; data;
	ss &lt;&lt; &quot;Empfangen: &quot; &lt;&lt; data &lt;&lt; &quot;\n&quot;;

	clientSocket &lt;&lt; ss.str();
*/
	std::cout &lt;&lt; &quot;Ich bin die Methode calc()&quot; &lt;&lt; std::endl;
 }

int main ( int argc, char**argv )
{
	std::cout &lt;&lt; &quot;running....\n&quot;;

	try
	{
		// 1. Create the socket
		// 2. Bind Socket to Port 30000
		// 3. Set Socket to Listen
		ServerSocket server ( 30000 );

		while ( true )
		{
			ClientSocket clientSock;
			server.accept ( clientSock );
			/* Hier wird die Connection erstellt. Diese gibt die Funktion calc() an 
			 * den Thread weiter und den akzeptierten Socket */
			ServerConnection sc(clientSock, &amp;calc);
			sc.join();

		}
	}
	catch ( SocketException&amp; e )
	{
		std::cout &lt;&lt; &quot;Exception was caught:&quot; &lt;&lt; e.description() &lt;&lt; &quot;\nExiting.\n&quot;;
	}

	return 0;
}
</code></pre>
<p>Die Klasse ServerConnection:</p>
<pre><code class="language-cpp">#ifndef SERVERCONNECTION_H_
#define SERVERCONNECTION_H_

#include &lt;boost/thread/thread.hpp&gt;
#include &quot;ClientSocket.h&quot;

class ServerConnection : public boost::thread {

public:

	template&lt;typename Callable&gt;
	ServerConnection(const ClientSocket&amp; csocket, const Callable func)
: boost::thread(func), clientSocket(csocket)  { }

	virtual ~ServerConnection() { }

	void calc();
private:
	ClientSocket clientSocket;

};

#endif /* SERVERCONNECTION_H_ */
</code></pre>
<p>ClientSocket sieht so aus:</p>
<pre><code class="language-cpp">/*
 * ClientSocket.h
 *
 *  Created on: May 12, 2011
 *      Author: lumbeck
 */

#ifndef CLIENTSOCKET_H_
#define CLIENTSOCKET_H_

#include &quot;Socket.h&quot;

class ClientSocket : public Socket
{
 public:

  ClientSocket ( std::string host, int port );
  ClientSocket() { }
  virtual ~ClientSocket() {};

  const ClientSocket&amp; operator &lt;&lt; ( const std::string&amp; ) const;
  const ClientSocket&amp; operator &gt;&gt; ( std::string&amp; ) const;

};

#endif /* CLIENTSOCKET_H_ */
</code></pre>
<p>Und hier auch wieder die zugehörige cpp-Datei</p>
<pre><code class="language-cpp">// Implementation of the ClientSocket class

#include &quot;ClientSocket.h&quot;
#include &quot;SocketException.h&quot;

ClientSocket::ClientSocket ( std::string host, int port )
{
  if ( ! Socket::create() )
    {
throw SocketException ( &quot;Could not create client socket.&quot; );
    }

  if ( ! Socket::connect ( host, port ) )
    {
      throw SocketException ( &quot;Could not bind to port.&quot; );
    }
}

const ClientSocket&amp; ClientSocket::operator &lt;&lt; ( const std::string&amp; s ) const
{
  if ( ! Socket::send ( s ) )
    {
      throw SocketException ( &quot;Could not write to socket.&quot; );
    }
  return *this;
}

const ClientSocket&amp; ClientSocket::operator &gt;&gt; ( std::string&amp; s ) const
{
  if ( ! Socket::recv ( s ) )
    {
      throw SocketException ( &quot;Could not read from socket.&quot; );
    }
  return *this;
}
</code></pre>
<p>ServerSocket sieht so aus:</p>
<pre><code class="language-cpp">#ifndef SERVERSOCKET_H_
#define SERVERSOCKET_H_

#include &quot;Socket.h&quot;

class ServerSocket : public Socket
{
 public:

  ServerSocket ( int port );
  ServerSocket (){};
  virtual ~ServerSocket();

  const ServerSocket&amp; operator &lt;&lt; ( const std::string&amp; );
  const ServerSocket&amp; operator &gt;&gt; ( std::string&amp; );

 void accept ( Socket&amp; );

};

#endif /* SERVERSOCKET_H_ */
</code></pre>
<pre><code class="language-cpp">/*
 * ServerSocket.cpp
 */

// Implementation of the ServerSocket class

#include &quot;ServerSocket.h&quot;
#include &quot;SocketException.h&quot;
#include &lt;sstream&gt;

ServerSocket::ServerSocket ( int port )
{
  if ( ! Socket::create() )
    {
      throw SocketException ( &quot;Could not create server socket.&quot; );
    }

  if ( ! Socket::bind ( port ) )
    {
      throw SocketException ( &quot;Could not bind to port.&quot; );
    }

  if ( ! Socket::listen() )
    {
      throw SocketException ( &quot;Could not listen to socket.&quot; );
    }
}

ServerSocket::~ServerSocket()
{
}

const ServerSocket&amp; ServerSocket::operator &lt;&lt; ( const std::string&amp; s )
{
  if ( ! Socket::send ( s) )
    {
      throw SocketException ( &quot;Could not write to socket.&quot; );
    }
  return *this;
}

const ServerSocket&amp; ServerSocket::operator &gt;&gt; ( std::string&amp; s )
{
  if ( ! Socket::recv ( s ) )
    {
      throw SocketException ( &quot;Could not read from socket.&quot; );
    }
  return *this;
}

void ServerSocket::accept ( Socket&amp; sock )
{
  if ( ! Socket::accept ( sock ) )
    {
      throw SocketException ( &quot;Could not accept socket.&quot; );
    }
}
</code></pre>
<p>Ohne das Threading funktioniert es mit dem Echo-Server (also Client schickt Nachricht und bekommt sie sofort zurück).</p>
<p>Hier ist aber folgendes Problem beim Kompilieren des Servers:</p>
<pre><code>g++  -o server Socket.cpp network.cpp server_main.cpp ServerSocket.cpp ServerConnection.cpp
/tmp/ccD7KxdR.o: In function `main':
server_main.cpp:(.text+0x16e): undefined reference to `ServerConnection::ServerConnection&lt;void (ServerConnection::*)()&gt;(ClientSocket const&amp;,void (ServerConnection::*)())'
server_main.cpp:(.text+0x17e): undefined reference to `boost::thread::join()'
/tmp/ccD7KxdR.o: In function `ServerConnection::~ServerConnection()':
server_main.cpp:(.text._ZN16ServerConnectionD1Ev[ServerConnection::~ServerConnection()]+0x38): undefined reference to `boost::thread::~thread()'
server_main.cpp:(.text._ZN16ServerConnectionD1Ev[ServerConnection::~ServerConnection()]+0x63): undefined reference to `boost::thread::~thread()'
/tmp/ccD7KxdR.o: In function `ServerConnection::~ServerConnection()':
server_main.cpp:(.text._ZN16ServerConnectionD0Ev[ServerConnection::~ServerConnection()]+0x38): undefined reference to `boost::thread::~thread()'
server_main.cpp:(.text._ZN16ServerConnectionD0Ev[ServerConnection::~ServerConnection()]+0x63): undefined reference to `boost::thread::~thread()'
collect2: ld returned 1 exit status
make: *** [server] Error 1
</code></pre>
<p>Ich möchte die Methode calc() der ServerConnection an die Connection weitergeben. Warum klappt das nicht? Ich hoffe, mein Problem ist verständlich...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064640</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064640</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Tue, 17 May 2011 14:48:43 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 13:06:18 GMT]]></title><description><![CDATA[<p>Ohne den Code zu lesen: Du hast nicht gegen die Booost Threads Bibliothek gelinkt. Da sollte noch so etwas wie -lboost_thread an den Compileraufruf dran.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064651</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064651</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 17 May 2011 13:06:18 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 13:16:47 GMT]]></title><description><![CDATA[<p>Ahrg,<br />
hatte vorher ein kleines Testprogramm geschrieben und in der make-Datei nur die Library beim Testprogramm angegeben.</p>
<p>Jetzt kommt noch folgender Fehler:</p>
<pre><code>g++ -lboost_thread -o server Socket.cpp network.cpp server_main.cpp ServerSocket.cpp ServerConnection.cpp  -L/usr/lib64/
/tmp/ccD4gzLZ.o: In function `main':
server_main.cpp:(.text+0x16e): undefined reference to `ServerConnection::ServerConnection&lt;void (ServerConnection::*)()&gt;(ClientSocket const&amp;,void (ServerConnection::*)())'
collect2: ld returned 1 exit status
make: *** [server] Error 1
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2064661</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064661</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Tue, 17 May 2011 13:16:47 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 13:25:21 GMT]]></title><description><![CDATA[<p>Templatedefinitionen musst (beziehungsweise solltest, denn das ist die einfachste Methode) du in den zugehörigen Header packen, sonst werden sie bei Bedarf nicht instanziert. Steht hier in den FAQ glaube ich an fünfter Stelle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064666</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064666</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 17 May 2011 13:25:21 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 13:34:53 GMT]]></title><description><![CDATA[<p>Das heisst, der Kontruktor sollte in der Header von ServerConnection schon definiert werden?</p>
<p>Oder wie hab ich das vorzustellen?</p>
<p>Habe die Templatedefinition aus der thread-Klasse von boost, deshalb weiss ich es nicht besser, habe bisher nicht mit templates aktiv gearbeitet.</p>
<p>(ich schau mal im FAQ)</p>
<p>Ah. Also muss ich entweder direkt im Header definieren oder eine .impl-Datei schreiben, wo die Templates definiert werden...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064674</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064674</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Tue, 17 May 2011 13:34:53 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 13:32:11 GMT]]></title><description><![CDATA[<p>Fabulus schrieb:</p>
<blockquote>
<p>Das heisst, der Kontruktor sollte in der Header von ServerConnection schon definiert werden?</p>
</blockquote>
<p>Ja.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064679</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064679</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 17 May 2011 13:32:11 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 13:34:13 GMT]]></title><description><![CDATA[<p>Okay, habe ich gemacht. Das klappt ja soweit schonmal, jetzt kommt aber ein nächstes Kompilierproblem bzgl der threads:</p>
<pre><code>g++ -lboost_thread -o server Socket.cpp network.cpp server_main.cpp ServerSocket.cpp   -L/usr/lib64/
/usr/include/boost/thread/detail/thread.hpp: In member function 'void boost::detail::thread_data&lt;F&gt;::run() [with F = void (ServerConnection::*)()]':
server_main.cpp:55:   instantiated from here
/usr/include/boost/thread/detail/thread.hpp:56: error: must use '.*' or '-&gt;*' to call pointer-to-member function in '((boost::detail::thread_data&lt;void (ServerConnection::*)()&gt;*)this)-&gt;boost::detail::thread_data&lt;void (ServerConnection::*)()&gt;::f (...)'
make: *** [server] Error 1
</code></pre>
<p>Das versteh ich ja noch weniger <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064685</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064685</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Tue, 17 May 2011 13:34:13 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 13:38:53 GMT]]></title><description><![CDATA[<p>Zeig mal die server_main.cpp, wie sie jetzt ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064689</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064689</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 17 May 2011 13:38:53 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 13:46:36 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">/* server_main.cpp */
/* Die main-Datei des Servers, welche aufgerufen wird */

#include &quot;ServerSocket.h&quot;
#include &quot;ClientSocket.h&quot;
#include &quot;ServerConnection.h&quot;
#include &quot;SocketException.h&quot;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;

void ServerConnection::calc() {

	/*std::stringstream ss;
	std::string data;
	clientSocket &gt;&gt; data;
	ss &lt;&lt; &quot;Empfangen: &quot; &lt;&lt; data &lt;&lt; &quot;\n&quot;;

	clientSocket &lt;&lt; ss.str();
*/
	std::cout &lt;&lt; &quot;Ich bin die Methode calc()&quot; &lt;&lt; std::endl;
 }

int main ( int argc, char**argv )
{
	std::cout &lt;&lt; &quot;running....\n&quot;;

	try
	{
		// 1. Create the socket
		// 2. Bind Socket to Port
		// 3. Set Socket to Listen
		ServerSocket server ( 30000 );

		while ( true )
		{

			ClientSocket clientSock;
			server.accept ( clientSock );

			/* Hier wird die Connection erstellt. Diese gibt die Funktion calc() an
			 * den Thread weiter und den akzeptierten Socket */
			ServerConnection sc(clientSock, &amp;ServerConnection::calc);
			sc.join();

		}
	}
	catch ( SocketException&amp; e )
	{
		std::cout &lt;&lt; &quot;Exception was caught:&quot; &lt;&lt; e.description() &lt;&lt; &quot;\nExiting.\n&quot;;
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2064693</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064693</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Tue, 17 May 2011 13:46:36 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 14:29:49 GMT]]></title><description><![CDATA[<p>Verstehe. Du übergibst ihm mit &amp;calc einen Pointer auf eine Memberfunktion. Die kann der Thread natürlich nicht ohne weiteres aufrufen, da er schließlich noch ein konkretes Objekt braucht.</p>
<p>Das musst du entweder mit boost::bind daran binden oder das insgesamt anders lösen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064726</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064726</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 17 May 2011 14:29:49 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 14:42:54 GMT]]></title><description><![CDATA[<p>Ah, weil calc() ja keine statische Methode ist, müsste ich quasi die Methode eines Objektes vom Typ ServerConnection angeben?<br />
Das heißt, wenn ich diese Methode statisch mache, würde das vorerst funktionieren.</p>
<p>Wie funktioniert das denn mit bind? <a href="http://www.boost.org/doc/libs/1_46_1/doc/html/index.html" rel="nofollow">Auf der Boost-Documentation-Seite</a> finde ich da keine Hilfe zu.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064734</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064734</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Tue, 17 May 2011 14:42:54 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 14:43:14 GMT]]></title><description><![CDATA[<p>Das er hier die Definition einer Template-Memberfunktion in die cpp gepackt hat spielt hier keine Rolle?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064735</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064735</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Tue, 17 May 2011 14:43:14 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 14:47:40 GMT]]></title><description><![CDATA[<p>Habe es schon geändert, gibt jetzt nur noch den Konstruktor in ServerConnection.h, die .cpp-Datei gibt es nicht mehr <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064739</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Tue, 17 May 2011 14:47:40 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 15:13:30 GMT]]></title><description><![CDATA[<p>Fabulus schrieb:</p>
<blockquote>
<p>Wie funktioniert das denn mit bind?</p>
</blockquote>
<p>So:<br />
<a href="http://www.boost.org/doc/libs/1_46_1/libs/bind/bind.html#with_member_pointers" rel="nofollow">http://www.boost.org/doc/libs/1_46_1/libs/bind/bind.html#with_member_pointers</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064752</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064752</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 17 May 2011 15:13:30 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 16:01:33 GMT]]></title><description><![CDATA[<p>Blicke ich noch nicht so durch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Mit dem Beispiel aus der Doku wird doch nur die Funktion aufgerufen (So wie es daneben im Kommentar steht)<br />
Aber ich habe doch gar keine Argumente, die an calc übergeben werden müssen.<br />
Und ich habe auch kein Objekt, welches ich angeben könnte (im Beispiel ist es das x).</p>
<p>Ich weiß nicht, wie ich den bind-Befehl mit meinem Code kombinieren soll.<br />
Soll ich beim Erstellen der ServerConnection-Instanz das bind() in den Argumentaufruf setzen wie hier:</p>
<pre><code class="language-cpp">ServerConnection sc(clientSock, bind(&amp;ServerConnection::calc, /* Was soll hier jetzt hin? */));
			sc.join();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2064779</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064779</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Tue, 17 May 2011 16:01:33 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Tue, 17 May 2011 16:47:55 GMT]]></title><description><![CDATA[<p>Fabulus schrieb:</p>
<blockquote>
<p>Aber ich habe doch gar keine Argumente, die an calc übergeben werden müssen.<br />
Und ich habe auch kein Objekt, welches ich angeben könnte (im Beispiel ist es das x).</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> Wieso machst du es dann überhaupt so kompliziert, wenn du das gar nicht möchtest? Überdenke dein Design!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2064805</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2064805</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 17 May 2011 16:47:55 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Thu, 19 May 2011 05:56:03 GMT]]></title><description><![CDATA[<p>Mein Plan ist folgender gewesen:</p>
<p>Der Server ist die ganze Zeit online und akzeptiert in einer while-true-Schleife alle eingehenden Verbindungen. Damit aber gleichzeitig mehrere Clients sich anmelden können, soll eine neue ServerConnection erstellt werden, welche quasi ein Thread ist, in dem dann die Berechnung bzw. die Stringumformungen gemacht werden.<br />
Als Argumente für die Serverconnection gebe ich den ClientSocket an und die Methode, die im Thread ausgeführt werden soll.</p>
<p>Ist das Prinzip verständlich?</p>
<p>Es soll quasi bei einer neuen Client-Verbindung ein neuer Thread gestartet werden.</p>
<p>Edit:<br />
Wenn ich eine globale Methode erstelle, klappt dies soweit, habe dann aber keinen Zugriff mehr auf den clientSocket, den ich bei der ServerConnection erstelle, deshalb würde ich lieber ne Methode von ServerConnection übergeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2065606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2065606</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Thu, 19 May 2011 05:56:03 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Thu, 19 May 2011 06:41:02 GMT]]></title><description><![CDATA[<p>Wenn du die calc-Methode von ServerConnection verwenden willst, dann brauchst du sie doch eigentlich nicht im Konstruktor von ServerConnection zu übergeben. Es würde doch reichen sie in der Initialisierungsliste des Konstruktors an thread zu übergeben. Irgendwie so</p>
<pre><code class="language-cpp">ServerConnection(const ClientSocket&amp; csocket)
: boost::thread(boost::bind(&amp;ServerConnection::calc, this)), clientSocket(csocket)  { }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2065621</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2065621</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Thu, 19 May 2011 06:41:02 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Thu, 19 May 2011 14:14:18 GMT]]></title><description><![CDATA[<p>Ah, wunderbar, das funktioniert! Hätte ich auch selber drauf kommen können. Dann spar ich mir auch das Template <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Danke sehr <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Wenn es noch was bzgl. dieses Themas gibt, melde ich mich wieder.</p>
<p>EDIT:</p>
<p>Ich möchte jetzt zusätzlich noch eine Clientconnection erstellen. Eine Instanz dieser wird zu Beginn des Clients erstellt und der Thread gestartet. Das einzige, was die Methode, die der Thread aufruft, macht, ist in einer Endlosschleife Nachrichten entgegenzunehmen und auszugeben.</p>
<p>Das ganze wollte ich jetzt folgendermaßen lösen:</p>
<pre><code class="language-cpp">// client_main.cpp
#include &quot;ClientSocket.h&quot;
#include &quot;SocketException.h&quot;
#include &quot;ClientConnection.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;

int main ( int argc, char** argv )
{
	try
	{

		ClientSocket client_socket ( &quot;ikp696&quot;, 30000 );

		std::string reply;
		std::string entry;
		std::string exitmsg=&quot;exit&quot;;

		ClientConnection cc(client_socket);

		cc.join();

                std::cout &lt;&lt; &quot;Test&quot;;

		while(true) {

			std::cout &lt;&lt; &quot;Enter message: &quot;;
			getline(std::cin, entry);

			try
			{
				// Sende Nachricht an den Sever
				client_socket &lt;&lt; entry;

				if(entry == exitmsg) {
					client_socket.close();
					break;
				}
			}
			catch ( SocketException&amp; ) {
				std::cout &lt;&lt; &quot;Socket Exception!&quot;;
			}
		}

	}
	catch ( SocketException&amp; e )
	{
		std::cout &lt;&lt; &quot;Exception was caught:&quot; &lt;&lt; e.description() &lt;&lt; &quot;\n&quot;;
	}

	return 0;
}
</code></pre>
<pre><code class="language-cpp">#ifndef CLIENTCONNECTION_H_
#define CLIENTCONNECTION_H_

#include &lt;boost/thread/thread.hpp&gt;
#include &lt;boost/bind.hpp&gt;
#include &quot;ClientSocket.h&quot;

class ClientConnection : public boost::thread {

public:

	ClientConnection(const ClientSocket&amp; csocket)
	: boost::thread(boost::bind(&amp;ClientConnection::Run, this)), clientSocket(csocket)  { }

	virtual ~ClientConnection() { }

	void Run();

private:
	ClientSocket clientSocket;

};

#endif /* CLIENTCONNECTION_H_ */
</code></pre>
<pre><code class="language-cpp">//ClientConnection.cpp
#include &quot;ClientConnection.h&quot;
#include &lt;string&gt;

void ClientConnection::Run() {

		// Empfange Daten vom Server und speichere sie in reply
		std::string reply=&quot;&quot;;
		clientSocket &gt;&gt; reply;

		std::cout &lt;&lt; reply &lt;&lt; &quot;\&quot;\n&quot;;;
}
</code></pre>
<p>Jetzt hab ich das Problem, dass in der Client_main-Datei die Nachricht &quot;Test&quot; nicht ausgegeben wird, weil anscheinend der Thread blockiert oder so.</p>
<p>Sinn und Zweck der ganzen Sache ist, dass ich gleichzeitig Nachrichten senden und empfangen kann. In Java habe ich es genau so hinbekommen, nur in C++ noch nicht. Kann da jemand noch mal schauen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2065839</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2065839</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Thu, 19 May 2011 14:14:18 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Thu, 19 May 2011 18:39:54 GMT]]></title><description><![CDATA[<p>Indem du auf einem Thread die join - Methode aufrufst, wartest du auf Beendigung des Threads. Wenn der Thread nicht beendet wird, passiert also auch nichts.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2066019</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2066019</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Thu, 19 May 2011 18:39:54 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Thu, 19 May 2011 19:31:02 GMT]]></title><description><![CDATA[<p>Und wie ist es möglich, dass ein Thread wirklich _parallel_ läuft`?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2066049</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2066049</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Thu, 19 May 2011 19:31:02 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Thu, 19 May 2011 20:03:53 GMT]]></title><description><![CDATA[<p>Dein Thread läuft doch parallel, du darfst halt nur nicht auf seine Beendigung warten - oder eben an anderer Stelle.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2066059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2066059</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Thu, 19 May 2011 20:03:53 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Fri, 20 May 2011 05:20:53 GMT]]></title><description><![CDATA[<p>Gibt es denn Methoden, die den Thread starten und nicht warten, bis dieser zu Ende gelaufen ist, sondern dass Programm weiterläuft und der Thread im Hintergrund auch?</p>
<p>Irgendwie verstehe ich den Sinn dahinter nicht. Ein Thread ist doch gerade dazu da, dass mehrere Dinge gleichzeitig laufen. Dann ist es doch Schwachsinn, dass erst gewartet werden muss, bis der Thread durchgelaufen ist und dann erst weiter gemacht wird <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2066135</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2066135</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Fri, 20 May 2011 05:20:53 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Fri, 20 May 2011 07:03:52 GMT]]></title><description><![CDATA[<p>Ich glaube wir beide reden aneinander vorbei. Lass doch einfach das join weg <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2066158</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2066158</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Fri, 20 May 2011 07:03:52 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Fri, 20 May 2011 07:17:32 GMT]]></title><description><![CDATA[<p>Achso, das bedeutet, ein Thread läuft automatisch, ohne erst den thread wie in Java zu starten?</p>
<p>Habe dennoch ein weiteres Problem:<br />
Ich kann jetzt eine Nachricht eingeben, aber keine weitere.<br />
Die Ausgabe ist folgende (bei der Ausführung des Clients)</p>
<pre><code>Enter message: Testnachricht
Enter message: We received this response from the server:
&quot;Empfangen: Testnachricht&quot;
terminate called after throwing an instance of 'SocketException'
Aborted
</code></pre>
<p>Dabei schließe ich doch die Verbindung nicht, oder?</p>
<p>EDIT: Ich habe herausgefunden, das Problem liegt an der Schleife im Thread der Clientconnection. Es wird wieder versucht, etwas zu empfangen, aber dies funktioniert nicht. Dabei soll doch nur empfangen werden, wenn etwas geschickt wurde..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2066163</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2066163</guid><dc:creator><![CDATA[Fabulus]]></dc:creator><pubDate>Fri, 20 May 2011 07:17:32 GMT</pubDate></item><item><title><![CDATA[Reply to Threadproblem bei Server-Client-Programm on Fri, 20 May 2011 09:12:55 GMT]]></title><description><![CDATA[<p>Ein Thread wird im Kontruktor gestartet, ja. RAII eben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2066225</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2066225</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Fri, 20 May 2011 09:12:55 GMT</pubDate></item></channel></rss>