<?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[C++ Socket (winsock): send wird erst bei Beenden der Verbindung ausgeführt]]></title><description><![CDATA[<p>Hallo zusammen!</p>
<p>Ich arbeite derzeit gerade an einem Projekt, für das ich eine Socket-Connection zwischen Java (als Server) und C++ (als Client) benötige; mein Betriebssystem ist Windows Vista, ich arbeite mit Dev-Cpp.</p>
<p>Der Server in Java steht schon, mit einem Java-Client habe ich auch keine Probleme - es werden Strings auf beiden Seiten gesendet und empfangen (also schließe ich den Server als Fehlerquelle einmal aus).</p>
<p>Mein C++ Client funktioniert leider nicht so ganz, wie ich es möchte; er empfängt die Daten und gibt sie auf der Console aus und er sendet die Daten auch, allerdings wird das Senden erst dann getätigt, wenn das Socket geschlossen wird. (Geschlossen wird dann, wenn ich auf der Console &quot;exit&quot; eingebe.) Das Empfangen funktioniert, also das macht er auch, sobald ich es vom Server wegschicke.</p>
<p>Ich vermute, dass ich beim send() Aufruf irgend etwas falsch mache... hat jemand eine Idee?</p>
<p>Ich bin für jede Hilfe dankbar...</p>
<p>Liebe Grüße,<br />
miquagi</p>
<p>Der Code vom Client:</p>
<pre><code>[cpp]
#include &lt;windows.h&gt;
#include &lt;winsock2.h&gt;
#include &lt;conio.h&gt;

#include &lt;string&gt;
#include &lt;iostream&gt;
using namespace std;

SOCKET      Socket;
SOCKADDR_IN addr;

int main(){
   string      m_host = &quot;127.0.0.1&quot;;
   string      m_port = &quot;2000&quot;;

   WSADATA wsa;
   int rc = WSAStartup(MAKEWORD(2, 0), &amp;wsa);
   if(rc == SOCKET_ERROR){
      cout &lt;&lt; &quot;Error while starting server... \n&quot;;
   }

   Socket = socket(AF_INET, SOCK_STREAM, 0);
   if(Socket == INVALID_SOCKET){
      std::cout &lt;&lt; &quot;Error while building socket. \n&quot;;
   }

   addr.sin_addr.s_addr = inet_addr(m_host.c_str());
   addr.sin_port        = htons(atoi(m_port.c_str()));
   addr.sin_family      = AF_INET;

   if(connect(Socket, (SOCKADDR*)&amp;addr, sizeof(SOCKADDR)) == 0){
      cout &lt;&lt; &quot;Connection with server &quot; &lt;&lt; inet_ntoa(addr.sin_addr) 
           &lt;&lt; &quot; established.&quot; &lt;&lt; endl &lt;&lt; endl;
   } else {
      cout &lt;&lt; &quot;Impossible to connect to server &quot; &lt;&lt; inet_ntoa(addr.sin_addr) 
           &lt;&lt; &quot;:&quot; &lt;&lt; addr.sin_port
           &lt;&lt; &quot;. Please check host and port. \n&quot;;
      return 1;
   }    

   // Everything okay - start communicating with server
   char        buffersend[1024];
   char        bufferrecv[1024];
   int         bufpos = 0;
   int         bufIndex = 0;
   char        c;
   fd_set      fdSetRead;
   TIMEVAL     timeout;
   bool        mainloop = true;

   while(rc != SOCKET_ERROR &amp;&amp; mainloop){
      FD_ZERO(&amp;fdSetRead);
      FD_SET(Socket, &amp;fdSetRead);
      timeout.tv_sec  = 0;
      timeout.tv_usec = 0;

      //RECEIVE DATA        
      while((rc=select(0,&amp;fdSetRead,NULL,NULL,&amp;timeout)) != 0) { 
         u_long iNumBytesPresent;
         u_long iMode = 1;
         // switch socket to non-blocking
         rc = ioctlsocket(Socket, FIONBIO, &amp;iMode );
         //recv (returns if there is no data)
         rc = recv(Socket, bufferrecv,1023, 0);

         iNumBytesPresent = rc;
         // if first recv call returned immediately
         while(iNumBytesPresent &lt;= 0) {
            rc = ioctlsocket(Socket, FIONREAD ,&amp;iNumBytesPresent);
            if(iNumBytesPresent &gt; 0) {
               break;
            }
            // Read available number of bytes
            rc = recv(Socket, bufferrecv, iNumBytesPresent, 0);
      } 

      bufferrecv[rc] = '\0';
      cout &lt;&lt; &quot;Message from server: &quot; &lt;&lt; bufferrecv &lt;&lt; '\n';
   }

   //SEND DATA
   while(kbhit()){
      c = getch(); 
         if(c==13){ 
            if(strcmp(buffersend, &quot;exit&quot;) == 0){
               mainloop = false;
               break;
            }
            bufpos++;
            cout &lt;&lt; &quot;Sending '&quot; &lt;&lt; buffersend &lt;&lt; &quot;' to server.\n&quot;;
            rc = send(Socket,buffersend,bufpos, 0); 
            bufpos = 0; 
         } else {
            buffersend[bufpos++]=c; 
         }
      } 
      buffersend[bufpos] = '\0'; 
   }

   cout &lt;&lt; &quot;Shutting down client. \n&quot;;
   closesocket(Socket);
   WSACleanup();
   cout &lt;&lt; &quot;\nPROGRAM EXIT&quot;;    
}
[/cpp]
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/223324/c-socket-winsock-send-wird-erst-bei-beenden-der-verbindung-ausgeführt</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 09:09:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/223324.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 24 Sep 2008 17:46:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ Socket (winsock): send wird erst bei Beenden der Verbindung ausgeführt on Wed, 24 Sep 2008 17:46:47 GMT]]></title><description><![CDATA[<p>Hallo zusammen!</p>
<p>Ich arbeite derzeit gerade an einem Projekt, für das ich eine Socket-Connection zwischen Java (als Server) und C++ (als Client) benötige; mein Betriebssystem ist Windows Vista, ich arbeite mit Dev-Cpp.</p>
<p>Der Server in Java steht schon, mit einem Java-Client habe ich auch keine Probleme - es werden Strings auf beiden Seiten gesendet und empfangen (also schließe ich den Server als Fehlerquelle einmal aus).</p>
<p>Mein C++ Client funktioniert leider nicht so ganz, wie ich es möchte; er empfängt die Daten und gibt sie auf der Console aus und er sendet die Daten auch, allerdings wird das Senden erst dann getätigt, wenn das Socket geschlossen wird. (Geschlossen wird dann, wenn ich auf der Console &quot;exit&quot; eingebe.) Das Empfangen funktioniert, also das macht er auch, sobald ich es vom Server wegschicke.</p>
<p>Ich vermute, dass ich beim send() Aufruf irgend etwas falsch mache... hat jemand eine Idee?</p>
<p>Ich bin für jede Hilfe dankbar...</p>
<p>Liebe Grüße,<br />
miquagi</p>
<p>Der Code vom Client:</p>
<pre><code>[cpp]
#include &lt;windows.h&gt;
#include &lt;winsock2.h&gt;
#include &lt;conio.h&gt;

#include &lt;string&gt;
#include &lt;iostream&gt;
using namespace std;

SOCKET      Socket;
SOCKADDR_IN addr;

int main(){
   string      m_host = &quot;127.0.0.1&quot;;
   string      m_port = &quot;2000&quot;;

   WSADATA wsa;
   int rc = WSAStartup(MAKEWORD(2, 0), &amp;wsa);
   if(rc == SOCKET_ERROR){
      cout &lt;&lt; &quot;Error while starting server... \n&quot;;
   }

   Socket = socket(AF_INET, SOCK_STREAM, 0);
   if(Socket == INVALID_SOCKET){
      std::cout &lt;&lt; &quot;Error while building socket. \n&quot;;
   }

   addr.sin_addr.s_addr = inet_addr(m_host.c_str());
   addr.sin_port        = htons(atoi(m_port.c_str()));
   addr.sin_family      = AF_INET;

   if(connect(Socket, (SOCKADDR*)&amp;addr, sizeof(SOCKADDR)) == 0){
      cout &lt;&lt; &quot;Connection with server &quot; &lt;&lt; inet_ntoa(addr.sin_addr) 
           &lt;&lt; &quot; established.&quot; &lt;&lt; endl &lt;&lt; endl;
   } else {
      cout &lt;&lt; &quot;Impossible to connect to server &quot; &lt;&lt; inet_ntoa(addr.sin_addr) 
           &lt;&lt; &quot;:&quot; &lt;&lt; addr.sin_port
           &lt;&lt; &quot;. Please check host and port. \n&quot;;
      return 1;
   }    

   // Everything okay - start communicating with server
   char        buffersend[1024];
   char        bufferrecv[1024];
   int         bufpos = 0;
   int         bufIndex = 0;
   char        c;
   fd_set      fdSetRead;
   TIMEVAL     timeout;
   bool        mainloop = true;

   while(rc != SOCKET_ERROR &amp;&amp; mainloop){
      FD_ZERO(&amp;fdSetRead);
      FD_SET(Socket, &amp;fdSetRead);
      timeout.tv_sec  = 0;
      timeout.tv_usec = 0;

      //RECEIVE DATA        
      while((rc=select(0,&amp;fdSetRead,NULL,NULL,&amp;timeout)) != 0) { 
         u_long iNumBytesPresent;
         u_long iMode = 1;
         // switch socket to non-blocking
         rc = ioctlsocket(Socket, FIONBIO, &amp;iMode );
         //recv (returns if there is no data)
         rc = recv(Socket, bufferrecv,1023, 0);

         iNumBytesPresent = rc;
         // if first recv call returned immediately
         while(iNumBytesPresent &lt;= 0) {
            rc = ioctlsocket(Socket, FIONREAD ,&amp;iNumBytesPresent);
            if(iNumBytesPresent &gt; 0) {
               break;
            }
            // Read available number of bytes
            rc = recv(Socket, bufferrecv, iNumBytesPresent, 0);
      } 

      bufferrecv[rc] = '\0';
      cout &lt;&lt; &quot;Message from server: &quot; &lt;&lt; bufferrecv &lt;&lt; '\n';
   }

   //SEND DATA
   while(kbhit()){
      c = getch(); 
         if(c==13){ 
            if(strcmp(buffersend, &quot;exit&quot;) == 0){
               mainloop = false;
               break;
            }
            bufpos++;
            cout &lt;&lt; &quot;Sending '&quot; &lt;&lt; buffersend &lt;&lt; &quot;' to server.\n&quot;;
            rc = send(Socket,buffersend,bufpos, 0); 
            bufpos = 0; 
         } else {
            buffersend[bufpos++]=c; 
         }
      } 
      buffersend[bufpos] = '\0'; 
   }

   cout &lt;&lt; &quot;Shutting down client. \n&quot;;
   closesocket(Socket);
   WSACleanup();
   cout &lt;&lt; &quot;\nPROGRAM EXIT&quot;;    
}
[/cpp]
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1587498</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587498</guid><dc:creator><![CDATA[miquagi]]></dc:creator><pubDate>Wed, 24 Sep 2008 17:46:47 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Socket (winsock): send wird erst bei Beenden der Verbindung ausgeführt on Wed, 24 Sep 2008 19:31:23 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile-var-mode-is-viewprofile-and-u-is-14199.html" rel="nofollow">Phoemuex</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-15.html" rel="nofollow">C++</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-4.html" rel="nofollow">WinAPI</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39405.html" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1587538</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1587538</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Wed, 24 Sep 2008 19:31:23 GMT</pubDate></item></channel></rss>