<?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++ Winsock fehler bei send() thread ?]]></title><description><![CDATA[<p>Ich habe einen chat client und server programmiert.Mittels threads solln beide in der lage sein daten zu empfangen und die eingabe des benutzers den anderen schicken.</p>
<p>SERVER:</p>
<pre><code>//Server
#include &lt;iostream&gt;
#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;

#define PORT 12345

using namespace std;

long rc;
char buffers[50];
string input;
SOCKET sendsock;

void schicken(SOCKET sock)
{
    while(1)
    {
        std::getline(std::cin, input);;
        strcpy(buffers,input.c_str());
        rc = send(sock,buffers,input.length(),0);
        if (rc == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[ERROR]Send fehler.&quot;&lt;&lt; WSAGetLastError() &lt;&lt; endl;
        } else
        {
            cout &lt;&lt; &quot;[INFO]You send: &quot; &lt;&lt; input;
    }
    }
}

void startwinsock()
{
    WSADATA wsa;
    rc = WSAStartup(MAKEWORD(2,0),&amp;wsa);
    if(rc != 0)
    {
        cout &lt;&lt; &quot;[ERROR]Winsock könnte nicht gestarted werden.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else {

        cout &lt;&lt; &quot;[INFO]Winsock würde gestarted.&quot; &lt;&lt; endl;
    }
}

int main()
{
    cout &lt;&lt; &quot;[INFO]Server wird gestarted.&quot; &lt;&lt; endl;
    startwinsock();
    SOCKADDR_IN addr;
    SOCKET connectedsocket;
    sendsock = socket(AF_INET,SOCK_STREAM,0);

    SOCKADDR_IN clientinfo;
    int clientinfolen = sizeof(clientinfo);

    SOCKET acceptsocket = socket(AF_INET,SOCK_STREAM,0);
    if(acceptsocket == INVALID_SOCKET)
    {
        cout &lt;&lt; &quot;[ERROR]Socket könnte nicht erstellt werden.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else{

        cout &lt;&lt; &quot;[INFO]Socket würde erstellt.&quot; &lt;&lt; endl;
    }

    memset(&amp;addr,0,sizeof(SOCKADDR_IN));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(PORT);
    addr.sin_addr.s_addr=ADDR_ANY;

    rc=bind(acceptsocket,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR_IN));

    if(rc == SOCKET_ERROR)
    {
        cout &lt;&lt; &quot;[ERROR]Bind fehler.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else{
        cout &lt;&lt; &quot;[INFO]Server würde an port [&quot; &lt;&lt; PORT &lt;&lt; &quot;] gebunden.&quot; &lt;&lt; endl;
    }

    rc=listen(acceptsocket,1);
    if(rc == SOCKET_ERROR)
    {
        cout &lt;&lt; &quot;[ERROR]Listen error.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else
    {
        cout &lt;&lt; &quot;[INFO]Server im listen modus.&quot; &lt;&lt; endl;
        cout &lt;&lt; &quot;[Server]--&gt;Waiting for connections:&quot; &lt;&lt; endl &lt;&lt; endl;
    }

    connectedsocket = accept(acceptsocket,(SOCKADDR*)&amp;clientinfo,&amp;clientinfolen);
    if(connectedsocket == INVALID_SOCKET)
    {
        cout &lt;&lt; &quot;[ERROR]Accept error.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else
    {
        cout &lt;&lt; &quot;[JOIN][&quot; &lt;&lt; inet_ntoa(clientinfo.sin_addr) &lt;&lt; &quot;][&quot; &lt;&lt; ntohs(clientinfo.sin_port) &lt;&lt; &quot;]&quot;&lt;&lt; endl;
    }

    //SOCKET a = socket(AF_INET,SOCK_STREAM,0);

    char buffer[50];

    //...

    //CreateThread(0,0,(LPTHREAD_START_ROUTINE)dllfunction,0,0,0);
    CreateThread(0,0,(LPTHREAD_START_ROUTINE)schicken,&amp;connectedsocket,0,0);
    //enviar(connectedsocket);

    while(1)
    {
        rc = recv(connectedsocket,buffer,50,0);
        if(rc == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[LEAVE][&quot; &lt;&lt; inet_ntoa(clientinfo.sin_addr) &lt;&lt; &quot;][&quot; &lt;&lt; ntohs(clientinfo.sin_port) &lt;&lt; &quot;]&quot; &lt;&lt; endl;
            break;
        } else
        {
            buffer[rc]='\0';
            cout &lt;&lt; &quot;From client [&quot; &lt;&lt; inet_ntoa(clientinfo.sin_addr) &lt;&lt; &quot;]: &quot;&lt;&lt; buffer &lt;&lt; endl;

        }
    }

    return 0;
}
</code></pre>
<p>CLIENT:</p>
<pre><code>//CLIENT

#include &lt;iostream&gt;
#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;
#include &lt;string&gt;
#include &quot;Client.hpp&quot;

#define PORT 12345
#define IP &quot;127.0.0.1&quot;

char sendbuffer[50];
char recvbuffer[50];

using namespace std;

long rc;

void empfangen(SOCKET sock)
{
    while(1)
    {
        Sleep(10);
        if(recv(sock,recvbuffer,50,0) == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[ERROR]-&gt;Verbindung unterbrochen.&quot;&lt;&lt; WSAGetLastError() &lt;&lt; endl;
            break;
        } else
        {
            cout &lt;&lt; &quot;From server: &quot; &lt;&lt; recvbuffer &lt;&lt; endl;
        }
    }
}

void startwinsock()
{
    WSADATA wsa;
    rc = WSAStartup(MAKEWORD(2,0),&amp;wsa);
    if(rc != 0)
    {
        cout &lt;&lt; &quot;[ERROR]Winsock könnte nicht gestarted werden.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else {

        cout &lt;&lt; &quot;[INFO]Winsock würde gestarted.&quot; &lt;&lt; endl;
    }
}

int main()
{
    cout &lt;&lt; &quot;[INFO]Client wird gestarted.&quot; &lt;&lt; endl;
    startwinsock();

    SOCKET s = socket(AF_INET,SOCK_STREAM,0);

    if(s == INVALID_SOCKET)
    {
        cout &lt;&lt; &quot;[ERROR]Socket könnte nicht erstellt werden.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else{

        cout &lt;&lt; &quot;[INFO]Socket würde erstellt.&quot; &lt;&lt; endl;
    }

     SOCKADDR_IN addr;

     memset(&amp;addr,0,sizeof(SOCKADDR_IN));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(PORT);
    addr.sin_addr.s_addr=inet_addr(IP);

    while(1)
    {
        Sleep(1000);
        if(connect(s,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR)) == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[ERROR]--&gt;CONNECT GESCHEITERT.&quot; &lt;&lt; endl;
        } else
        {
            cout &lt;&lt; &quot;[INFO]Verbunden mit server.&quot; &lt;&lt; endl;
            break;
        }
    }

    //CreateThread(0,0,(LPTHREAD_START_ROUTINE)dllfunction,0,0,0);
    CreateThread(0,0,(LPTHREAD_START_ROUTINE)empfangen,&amp;s,0,0);
    string input;

    while(1)
    {
        Sleep(10);
        std::getline(std::cin, input);;
        strcpy(sendbuffer,input.c_str());
        rc = send(s,sendbuffer,input.length(),0);
        if (rc == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[ERROR]Send fehler.&quot; &lt;&lt; endl;
        } else
        {
            cout &lt;&lt; &quot;[INFO]You send: &quot; &lt;&lt; input &lt;&lt; endl;
    }
    }
    return 0;
}
</code></pre>
<p>Messages vom client zum server schicken klappt nur kann der server keine schicken da kommt &quot;[ERROR]Send fehler.10038&quot;</p>
<p>Bei msdn steht:</p>
<p>Socket operation on nonsocket.<br />
Ein Vorgang bezog sich auf ein Objekt, das kein Socket ist. Entweder hat der Sockethandleparameter nicht auf einen gültigen Socket verwiesen, oder für select war ein Member einer fd_set-Struktur nicht gültig.</p>
<p>Also mein socket ist doch ein richtiger socket oder?? <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/topic/337004/c-winsock-fehler-bei-send-thread</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 15:14:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/337004.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 29 Feb 2016 19:34:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to c++ Winsock fehler bei send() thread ? on Mon, 29 Feb 2016 19:34:33 GMT]]></title><description><![CDATA[<p>Ich habe einen chat client und server programmiert.Mittels threads solln beide in der lage sein daten zu empfangen und die eingabe des benutzers den anderen schicken.</p>
<p>SERVER:</p>
<pre><code>//Server
#include &lt;iostream&gt;
#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;

#define PORT 12345

using namespace std;

long rc;
char buffers[50];
string input;
SOCKET sendsock;

void schicken(SOCKET sock)
{
    while(1)
    {
        std::getline(std::cin, input);;
        strcpy(buffers,input.c_str());
        rc = send(sock,buffers,input.length(),0);
        if (rc == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[ERROR]Send fehler.&quot;&lt;&lt; WSAGetLastError() &lt;&lt; endl;
        } else
        {
            cout &lt;&lt; &quot;[INFO]You send: &quot; &lt;&lt; input;
    }
    }
}

void startwinsock()
{
    WSADATA wsa;
    rc = WSAStartup(MAKEWORD(2,0),&amp;wsa);
    if(rc != 0)
    {
        cout &lt;&lt; &quot;[ERROR]Winsock könnte nicht gestarted werden.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else {

        cout &lt;&lt; &quot;[INFO]Winsock würde gestarted.&quot; &lt;&lt; endl;
    }
}

int main()
{
    cout &lt;&lt; &quot;[INFO]Server wird gestarted.&quot; &lt;&lt; endl;
    startwinsock();
    SOCKADDR_IN addr;
    SOCKET connectedsocket;
    sendsock = socket(AF_INET,SOCK_STREAM,0);

    SOCKADDR_IN clientinfo;
    int clientinfolen = sizeof(clientinfo);

    SOCKET acceptsocket = socket(AF_INET,SOCK_STREAM,0);
    if(acceptsocket == INVALID_SOCKET)
    {
        cout &lt;&lt; &quot;[ERROR]Socket könnte nicht erstellt werden.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else{

        cout &lt;&lt; &quot;[INFO]Socket würde erstellt.&quot; &lt;&lt; endl;
    }

    memset(&amp;addr,0,sizeof(SOCKADDR_IN));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(PORT);
    addr.sin_addr.s_addr=ADDR_ANY;

    rc=bind(acceptsocket,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR_IN));

    if(rc == SOCKET_ERROR)
    {
        cout &lt;&lt; &quot;[ERROR]Bind fehler.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else{
        cout &lt;&lt; &quot;[INFO]Server würde an port [&quot; &lt;&lt; PORT &lt;&lt; &quot;] gebunden.&quot; &lt;&lt; endl;
    }

    rc=listen(acceptsocket,1);
    if(rc == SOCKET_ERROR)
    {
        cout &lt;&lt; &quot;[ERROR]Listen error.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else
    {
        cout &lt;&lt; &quot;[INFO]Server im listen modus.&quot; &lt;&lt; endl;
        cout &lt;&lt; &quot;[Server]--&gt;Waiting for connections:&quot; &lt;&lt; endl &lt;&lt; endl;
    }

    connectedsocket = accept(acceptsocket,(SOCKADDR*)&amp;clientinfo,&amp;clientinfolen);
    if(connectedsocket == INVALID_SOCKET)
    {
        cout &lt;&lt; &quot;[ERROR]Accept error.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else
    {
        cout &lt;&lt; &quot;[JOIN][&quot; &lt;&lt; inet_ntoa(clientinfo.sin_addr) &lt;&lt; &quot;][&quot; &lt;&lt; ntohs(clientinfo.sin_port) &lt;&lt; &quot;]&quot;&lt;&lt; endl;
    }

    //SOCKET a = socket(AF_INET,SOCK_STREAM,0);

    char buffer[50];

    //...

    //CreateThread(0,0,(LPTHREAD_START_ROUTINE)dllfunction,0,0,0);
    CreateThread(0,0,(LPTHREAD_START_ROUTINE)schicken,&amp;connectedsocket,0,0);
    //enviar(connectedsocket);

    while(1)
    {
        rc = recv(connectedsocket,buffer,50,0);
        if(rc == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[LEAVE][&quot; &lt;&lt; inet_ntoa(clientinfo.sin_addr) &lt;&lt; &quot;][&quot; &lt;&lt; ntohs(clientinfo.sin_port) &lt;&lt; &quot;]&quot; &lt;&lt; endl;
            break;
        } else
        {
            buffer[rc]='\0';
            cout &lt;&lt; &quot;From client [&quot; &lt;&lt; inet_ntoa(clientinfo.sin_addr) &lt;&lt; &quot;]: &quot;&lt;&lt; buffer &lt;&lt; endl;

        }
    }

    return 0;
}
</code></pre>
<p>CLIENT:</p>
<pre><code>//CLIENT

#include &lt;iostream&gt;
#include &lt;winsock2.h&gt;
#include &lt;windows.h&gt;
#include &lt;string&gt;
#include &quot;Client.hpp&quot;

#define PORT 12345
#define IP &quot;127.0.0.1&quot;

char sendbuffer[50];
char recvbuffer[50];

using namespace std;

long rc;

void empfangen(SOCKET sock)
{
    while(1)
    {
        Sleep(10);
        if(recv(sock,recvbuffer,50,0) == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[ERROR]-&gt;Verbindung unterbrochen.&quot;&lt;&lt; WSAGetLastError() &lt;&lt; endl;
            break;
        } else
        {
            cout &lt;&lt; &quot;From server: &quot; &lt;&lt; recvbuffer &lt;&lt; endl;
        }
    }
}

void startwinsock()
{
    WSADATA wsa;
    rc = WSAStartup(MAKEWORD(2,0),&amp;wsa);
    if(rc != 0)
    {
        cout &lt;&lt; &quot;[ERROR]Winsock könnte nicht gestarted werden.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else {

        cout &lt;&lt; &quot;[INFO]Winsock würde gestarted.&quot; &lt;&lt; endl;
    }
}

int main()
{
    cout &lt;&lt; &quot;[INFO]Client wird gestarted.&quot; &lt;&lt; endl;
    startwinsock();

    SOCKET s = socket(AF_INET,SOCK_STREAM,0);

    if(s == INVALID_SOCKET)
    {
        cout &lt;&lt; &quot;[ERROR]Socket könnte nicht erstellt werden.&quot; &lt;&lt; endl;
        system(&quot;PAUSE&quot;);
    } else{

        cout &lt;&lt; &quot;[INFO]Socket würde erstellt.&quot; &lt;&lt; endl;
    }

     SOCKADDR_IN addr;

     memset(&amp;addr,0,sizeof(SOCKADDR_IN));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(PORT);
    addr.sin_addr.s_addr=inet_addr(IP);

    while(1)
    {
        Sleep(1000);
        if(connect(s,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR)) == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[ERROR]--&gt;CONNECT GESCHEITERT.&quot; &lt;&lt; endl;
        } else
        {
            cout &lt;&lt; &quot;[INFO]Verbunden mit server.&quot; &lt;&lt; endl;
            break;
        }
    }

    //CreateThread(0,0,(LPTHREAD_START_ROUTINE)dllfunction,0,0,0);
    CreateThread(0,0,(LPTHREAD_START_ROUTINE)empfangen,&amp;s,0,0);
    string input;

    while(1)
    {
        Sleep(10);
        std::getline(std::cin, input);;
        strcpy(sendbuffer,input.c_str());
        rc = send(s,sendbuffer,input.length(),0);
        if (rc == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[ERROR]Send fehler.&quot; &lt;&lt; endl;
        } else
        {
            cout &lt;&lt; &quot;[INFO]You send: &quot; &lt;&lt; input &lt;&lt; endl;
    }
    }
    return 0;
}
</code></pre>
<p>Messages vom client zum server schicken klappt nur kann der server keine schicken da kommt &quot;[ERROR]Send fehler.10038&quot;</p>
<p>Bei msdn steht:</p>
<p>Socket operation on nonsocket.<br />
Ein Vorgang bezog sich auf ein Objekt, das kein Socket ist. Entweder hat der Sockethandleparameter nicht auf einen gültigen Socket verwiesen, oder für select war ein Member einer fd_set-Struktur nicht gültig.</p>
<p>Also mein socket ist doch ein richtiger socket oder?? <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/2489029</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2489029</guid><dc:creator><![CDATA[userneedhelp32]]></dc:creator><pubDate>Mon, 29 Feb 2016 19:34:33 GMT</pubDate></item><item><title><![CDATA[Reply to c++ Winsock fehler bei send() thread ? on Mon, 29 Feb 2016 20:00:44 GMT]]></title><description><![CDATA[<p>Habs nicht ausprobiert aber ich glaube dein erstellen des Threads ist fehlerhaft:</p>
<pre><code>CreateThread(0,0,(LPTHREAD_START_ROUTINE)schicken,&amp;connectedsocket,0,0);
</code></pre>
<p>Hier übergibst du die Adresse auf deinen Socket aber deine Funktion akzeptiert direkt den Socket. Ändere mal die Funktion so, dass die Signatur passt und du dir den Cast zu <code>(LPTHREAD_START_ROUTINE)</code> sparen kannst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2489033</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2489033</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Mon, 29 Feb 2016 20:00:44 GMT</pubDate></item><item><title><![CDATA[Reply to c++ Winsock fehler bei send() thread ? on Mon, 29 Feb 2016 23:17:41 GMT]]></title><description><![CDATA[<p>Jetzt funktioniert habe es so geändert :<br />
SERVER:</p>
<pre><code>//empfangen funktion...
DWORD WINAPI empfangen(LPVOID lpParam)
{
    //int numberThread = (int)lpParam;
    SOCKET sock = (SOCKET)lpParam;
    while(1)
    {
        std::getline(std::cin, input);;
        strcpy(buffers,input.c_str());
        rc = send(sock,buffers,input.length(),0);
        if (rc == SOCKET_ERROR)
        {
            cout &lt;&lt; &quot;[ERROR]Send fehler.&quot;&lt;&lt; WSAGetLastError() &lt;&lt; endl;
        } else
        {
            cout &lt;&lt; &quot;[INFO]You send: &quot; &lt;&lt; input;
    }
    }
}

//Und thread darauf

DWORD  threadId;
    HANDLE threadHandle;
    threadHandle = CreateThread(NULL, 0, empfangen, (LPVOID)connectedsocket,    0,&amp;threadId)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2489051</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2489051</guid><dc:creator><![CDATA[userneedhelp32]]></dc:creator><pubDate>Mon, 29 Feb 2016 23:17:41 GMT</pubDate></item><item><title><![CDATA[Reply to c++ Winsock fehler bei send() thread ? on Mon, 29 Feb 2016 23:25:03 GMT]]></title><description><![CDATA[<p>OK, aber was wenn <code>sizeof(SOCKET) &gt; sizeof(LPVOID)</code> ? So wäre es besser:</p>
<pre><code>DWORD WINAPI empfangen(LPVOID lpParam)
{
  SOCKET sock = *((SOCKET*)lpParam);
  // ...
}

threadHandle = CreateThread(NULL, 0, empfangen, &amp;connectedsocket, 0, &amp;threadId);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2489052</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2489052</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Mon, 29 Feb 2016 23:25:03 GMT</pubDate></item></channel></rss>