<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=28424&amp;view=findpost&amp;p=197112</guid>
        <pubDate>Thu, 19 Jun 2003 14:06:54 +0000</pubDate>
        <title>Кто глючит: SOCKET или Программер?</title>
        <link>https://forum.sources.ru/index.php?showtopic=28424&amp;view=findpost&amp;p=197112</link>
        <description><![CDATA[SMG: Да shutdown (s_new, 0) и close (s_new) здесь лишнее. И не забывай делать так:<br><div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">memset(sin,0x0,sizeof(sin));&#60;br&#62;memset(sin_acc,0x0,sizeof(sin_acc));</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script>]]></description>
        <author>SMG</author>
        <category>C/C++: Сетевое программирование</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=28424&amp;view=findpost&amp;p=197111</guid>
        <pubDate>Thu, 19 Jun 2003 13:03:46 +0000</pubDate>
        <title>Кто глючит: SOCKET или Программер?</title>
        <link>https://forum.sources.ru/index.php?showtopic=28424&amp;view=findpost&amp;p=197111</link>
        <description><![CDATA[Beef: Зачем ты в цикле делаешь ШУТДАУН и клозесокет!!!!!!!! Ты не только закрывакшь сокет, но и шутдаеном закрываешь весю &nbsp;среду. Вот тебе код, который не глючит!!! Просто вставь его в консольный проект. Будь здоров!!!!<br>PS Шутдаун можно вообще не делать.<br><br><br><br>#include &lt;winsock2.h&gt;<br><br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>#define DEFAULT_PORT &nbsp; &nbsp; &nbsp; &nbsp;5150<br>#define DEFAULT_BUFFER &nbsp; &nbsp; &nbsp;4096<br><br>int &nbsp; &nbsp;iPort &nbsp; &nbsp; &nbsp;= DEFAULT_PORT; // Port to listen for clients on<br>BOOL &nbsp; bInterface = FALSE, &nbsp; &nbsp; &nbsp; // Listen on the specified interface<br> &nbsp; &nbsp; &nbsp; bRecvOnly &nbsp;= FALSE; &nbsp; // Receive data only; don't echo back<br>char &nbsp; szAddress[128]; &nbsp; &nbsp; &nbsp; // Interface to listen for clients on<br><br>// Function: ClientThread<br>//<br>// Description:<br>// &nbsp; &nbsp;This function is called as a thread, and it handles a given<br>// &nbsp; &nbsp;client connection. &nbsp;The parameter passed in is the socket <br>// &nbsp; &nbsp;handle returned from an accept() call. &nbsp;This function reads<br>// &nbsp; &nbsp;data from the client and writes it back.<br>//<br>DWORD WINAPI ClientThread(LPVOID lpParam)<br>{<br> &nbsp; &nbsp;SOCKET &nbsp; &nbsp; &nbsp; &nbsp;sock=(SOCKET)lpParam;<br> &nbsp; &nbsp;char &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;szBuff[DEFAULT_BUFFER];<br> &nbsp; &nbsp;int &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nLeft,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idx;<br><br> &nbsp; &nbsp;while(1)<br> &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp;// Perform a blocking recv() call<br> &nbsp; &nbsp; &nbsp; &nbsp;//<br> &nbsp; &nbsp; &nbsp; &nbsp;ret = recv(sock, szBuff, DEFAULT_BUFFER, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (ret == 0) &nbsp; &nbsp; &nbsp; &nbsp;// Graceful close<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;else if (ret == SOCKET_ERROR)<br> &nbsp; &nbsp; &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;recv() failed: \%d\n&quot;, WSAGetLastError());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp;szBuff[ret] = '{text}';<br> &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;RECV: '\%s'\n&quot;, szBuff);<br> &nbsp; &nbsp; &nbsp; &nbsp;//<br> &nbsp; &nbsp; &nbsp; &nbsp;// If we selected to echo the data back, do it <br> &nbsp; &nbsp; &nbsp; &nbsp;//<br> &nbsp; &nbsp; &nbsp; &nbsp;if (!bRecvOnly)<br> &nbsp; &nbsp; &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nLeft = ret;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idx = 0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Make sure we write all the data<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(nLeft &gt; 0)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ret = send(sock, &amp;szBuff[idx], nLeft, 0);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (ret == 0)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if (ret == SOCKET_ERROR)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;send() failed: \%d\n&quot;, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WSAGetLastError());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nLeft -= ret;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idx += ret;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp;}<br> &nbsp; &nbsp;return 0;<br>}<br><br>//<br>// Function: main<br>//<br>// Description:<br>// &nbsp; &nbsp;Main thread of execution. Initialize Winsock, parse the<br>// &nbsp; &nbsp;command line arguments, create the listening socket, bind<br>// &nbsp; &nbsp;to the local address, and wait for client connections.<br>//<br>int main(int argc, char **argv)<br>{<br> &nbsp; &nbsp;WSADATA &nbsp; &nbsp; &nbsp; wsd;<br> &nbsp; &nbsp;SOCKET &nbsp; &nbsp; &nbsp; &nbsp;sListen,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sClient;<br> &nbsp; &nbsp;int &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iPort, iAddrSize;<br> &nbsp; &nbsp;HANDLE &nbsp; &nbsp; &nbsp; &nbsp;hThread;<br> &nbsp; &nbsp;DWORD &nbsp; &nbsp; &nbsp; &nbsp; dwThreadId;<br> &nbsp; &nbsp;struct sockaddr_in local,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client;<br><br>// &nbsp; &nbsp;ValidateArgs(argc, argv);<br> &nbsp; &nbsp;if (WSAStartup(MAKEWORD(2,2), &amp;wsd) != 0)<br> &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;Failed to load Winsock!\n&quot;);<br> &nbsp; &nbsp; &nbsp; &nbsp;return 1;<br> &nbsp; &nbsp;}<br> &nbsp; &nbsp;// Create our listening socket<br> &nbsp; &nbsp;//<br> &nbsp; &nbsp;sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP/*IPPROTO_TCP*/);<br> &nbsp; &nbsp;if (sListen == SOCKET_ERROR)<br> &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;socket() failed: \%d\n&quot;, WSAGetLastError());<br> &nbsp; &nbsp; &nbsp; &nbsp;return 1;<br> &nbsp; &nbsp;}<br><br> &nbsp; &nbsp; &nbsp;iPort = 25;<br> &nbsp; &nbsp;local.sin_addr.s_addr = htonl(INADDR_ANY);<br> &nbsp; &nbsp;local.sin_family = AF_INET;<br> &nbsp; &nbsp;local.sin_port = htons(iPort);<br><br> &nbsp; &nbsp;if (bind(sListen, (struct sockaddr *)&amp;local, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sizeof(local)) == SOCKET_ERROR)<br> &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;bind() failed: \%d\n&quot;, WSAGetLastError());<br> &nbsp; &nbsp; &nbsp; &nbsp;return 1;<br> &nbsp; &nbsp;}<br> &nbsp; &nbsp;listen(sListen, 8);<br> &nbsp; &nbsp;//<br> &nbsp; &nbsp;// In a continous loop, wait for incoming clients. Once one <br> &nbsp; &nbsp;// is detected, create a thread and pass the handle off to it.<br> &nbsp; &nbsp;//<br> &nbsp; &nbsp;while (1)<br> &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp;iAddrSize = sizeof(client);<br> &nbsp; &nbsp; &nbsp; &nbsp;sClient = accept(sListen, (struct sockaddr *)&amp;client,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;iAddrSize); &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;if (sClient == INVALID_SOCKET)<br> &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;accept() failed: \%d\n&quot;, WSAGetLastError());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;Accepted client: \%s:\%d\n&quot;, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;inet_ntoa(client.sin_addr), ntohs(client.sin_port));<br><br> &nbsp; &nbsp; &nbsp; &nbsp;hThread = CreateThread(NULL, 0, ClientThread, <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(LPVOID)sClient, 0, &amp;dwThreadId);<br> &nbsp; &nbsp; &nbsp; &nbsp;if (hThread == NULL)<br> &nbsp; &nbsp; &nbsp; &nbsp;{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf(&quot;CreateThread() failed: \%d\n&quot;, GetLastError());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;}<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hThread);<br> &nbsp; &nbsp;}<br> &nbsp; &nbsp;closesocket(sListen);<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;WSACleanup();<br> &nbsp; &nbsp;return 0;<br>}]]></description>
        <author>Beef</author>
        <category>C/C++: Сетевое программирование</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=28424&amp;view=findpost&amp;p=197110</guid>
        <pubDate>Thu, 19 Jun 2003 12:36:05 +0000</pubDate>
        <title>Кто глючит: SOCKET или Программер?</title>
        <link>https://forum.sources.ru/index.php?showtopic=28424&amp;view=findpost&amp;p=197110</link>
        <description><![CDATA[Flex_Ferrum: int opt = 1;<br>setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &amp;opt, 4);<br><br>тебе поможет.<br>]]></description>
        <author>Flex_Ferrum</author>
        <category>C/C++: Сетевое программирование</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=28424&amp;view=findpost&amp;p=197109</guid>
        <pubDate>Thu, 19 Jun 2003 10:26:44 +0000</pubDate>
        <title>Кто глючит: SOCKET или Программер?</title>
        <link>https://forum.sources.ru/index.php?showtopic=28424&amp;view=findpost&amp;p=197109</link>
        <description><![CDATA[CString: Вот мой сервак:<br><br>................<br>SOCKET &nbsp; &nbsp;s,s_acc;<br>sockaddr_in sin,sin_acc;<br>................<br>s = socket (AF_INET, SOCK_STREAM, 0); <br>sin.sin_family = AF_INET; <br>sin.sin_addr.s_addr = INADDR_ANY; <br>sin.sin_port = SRV_PORT; <br>bind (s, (sockaddr *)&amp;sin, sizeof(sin)); <br>listen (s, MAX_CLIENT); <br>while (1) <br>{ <br> &nbsp; &nbsp; &nbsp; &nbsp; int len = sizeof(sockaddr_in); <br> &nbsp; &nbsp; &nbsp; &nbsp; s_acc = accept (s, (sockaddr*)&amp;sin_acc, &amp;len); <br> &nbsp; &nbsp; &nbsp; &nbsp; recv(s_acc,buffer,sizeof(buffer),0);<br> &nbsp; &nbsp; &nbsp; &nbsp; shutdown (s_new, 0); <br> &nbsp; &nbsp; &nbsp; &nbsp; close (s_new); <br>}; <br><br>Всё как в стандартных примерах....<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;НО !!!!!!!!!!!!!!!!!!!!<br><br>....... я не могу никаким клиентом подключиться второй раз. Выдаёт эрор 10048 &quot;Address already in use&quot;, типа он испльзуеться.<br><br>HELP !!!!!!!!!!!!<br>С меня пиво.]]></description>
        <author>CString</author>
        <category>C/C++: Сетевое программирование</category>
      </item>
	
      </channel>
      </rss>
	