title: Socket Programming in visual studio with C++
body: <p>I write a program for chat, but when i run server side,it produce error in listen function. i don't know what's the causes error!
  by the way, how i must run that?first server then client?what about IP address? how i set them? i want to run it just on the same PC.
  `</p>
  
  <pre><code>    #include "stdafx.h"
      #include &lt;winsock2.h&gt;
      #include &lt;Ws2tcpip.h&gt;
      #include &lt;stdio.h&gt;
      #include "iostream"
      #include "conio.h"
      using namespace std;
     #pragma comment(lib, "Ws2_32.lib")
     #define WIN32_LEAN_AND_MEAN
     #define DEFAULT_BUFLEN 512
     #define DEFAULT_PORT 27015
  
  int main()
  {
  
  WSADATA wsaData;
  int iResult = 0;            
  int recvbuflen = DEFAULT_BUFLEN;
  char *sendbuf = "Server: sending data test";
  char recvbuf[DEFAULT_BUFLEN] = "";
  
  
  SOCKET ListenSocket = INVALID_SOCKET;
  
  
  sockaddr_in service;
  
  
  iResult = WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
  if (iResult != NO_ERROR) {
      cout&lt;&lt;"Error at WSAStartup()\n";
  
  }
  
  ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (ListenSocket == INVALID_SOCKET) {
      cout&lt;&lt;"socket function failed with error: %u\n"&lt;&lt; WSAGetLastError();
      WSACleanup();
      getch();
  
  }
  
  service.sin_family = AF_INET;
  service.sin_addr.s_addr = inet_addr("192.168.1.52");
  service.sin_port = htons(27015);
  
  iResult = bind(ListenSocket, (SOCKADDR *) &amp;service, sizeof (service));
  if (iResult == SOCKET_ERROR) {
      cout&lt;&lt;"bind failed with error \n"&lt;&lt; WSAGetLastError();
      getch();
      closesocket(ListenSocket);
      WSACleanup();
  
  }
  else{
  
      wprintf(L"bind returned success\n");
      WSACleanup();
      getch();
  }
  
  if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR){
      cout&lt;&lt;"listen function failed with error: "&lt;&lt; WSAGetLastError();
      iResult = closesocket(ListenSocket);
      WSACleanup();
      getch();
  }
  
  
  
  cout&lt;&lt;"Waiting for client to connect...\n";
  
  ListenSocket = accept(ListenSocket, NULL, NULL);
  if (ListenSocket == INVALID_SOCKET) {
      cout&lt;&lt;"accept failed with error: \n"&lt;&lt; WSAGetLastError();
      closesocket(ListenSocket);
      WSACleanup();
      getch();
  } else
      cout&lt;&lt;"Client connected.\n";
  
  iResult = send( ListenSocket, sendbuf, (int)strlen(sendbuf), 0 );
  if (iResult == SOCKET_ERROR) {
      cout&lt;&lt;"send failed with error: "&lt;&lt; WSAGetLastError();
      closesocket(ListenSocket);
      getch();
      WSACleanup();
  
  }else
      cout&lt;&lt;"Bytes Sent: "&lt;&lt; iResult;
  
  
  do {
  
      iResult = recv(ListenSocket, recvbuf, recvbuflen, 0);
      if ( iResult &gt; 0 )
          cout&lt;&lt;"Bytes received: "&lt;&lt; iResult;   
      else if ( iResult == 0 ){
          cout&lt;&lt;"Connection closed";   
      getch();}
      else{
          cout&lt;&lt;"recv failed with error: "&lt;&lt; WSAGetLastError();
         getch();
      }
  } while( iResult &gt; 0 );
  
  
  iResult = closesocket(ListenSocket);
  if (iResult == SOCKET_ERROR) {
      cout&lt;&lt;"close failed with error: \n"&lt;&lt; WSAGetLastError();
      WSACleanup();
      getch();
  }
  
  
  return 0;
      }
  </code></pre>
  
  <p>`</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;winsock2.h&gt;
  #include &lt;Ws2tcpip.h&gt;
  #include &lt;stdio.h&gt;
  #include "iostream"
  #include "conio.h"
  using namespace std;
  #pragma comment(lib, "Ws2_32.lib")
  #define WIN32_LEAN_AND_MEAN
  #define DEFAULT_BUFLEN 512
   #define DEFAULT_PORT 27015
  
  int main() {
  
  int Result;
  WSADATA wsaData;
  
  SOCKET SocketNumber = INVALID_SOCKET;
  struct sockaddr_in clientService; 
  
  int recvbuflen = DEFAULT_BUFLEN;
  char *sendbuf = "Client: sending data test";
  char recvbuf[DEFAULT_BUFLEN] = "";
  
  Result = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
  if (Result != NO_ERROR) {
      cout&lt;&lt;"WSAStartup failed with error: "&lt;&lt; Result;
      getch();
  }
  
  SocketNumber = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (SocketNumber == INVALID_SOCKET) {
      cout&lt;&lt;"socket failed with error: "&lt;&lt; WSAGetLastError();
         getch();
      WSACleanup();
  }
  
  clientService.sin_family = AF_INET;
  clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
  clientService.sin_port = htons( DEFAULT_PORT );
  
  Result = connect( SocketNumber, (SOCKADDR*) &amp;clientService, sizeof(clientService) );
  if (Result == SOCKET_ERROR) {
      cout&lt;&lt;"connect failed with error: "&lt;&lt; WSAGetLastError();
      closesocket(SocketNumber);
      getch();
      WSACleanup();
  
   }
  Result = send( SocketNumber, sendbuf, (int)strlen(sendbuf), 0 );
  if (Result == SOCKET_ERROR) {
      cout&lt;&lt;"send failed with error: "&lt;&lt; WSAGetLastError();
      closesocket(SocketNumber);
      getch();
      WSACleanup();
  
  }
  
  cout&lt;&lt;"Bytes Sent: "&lt;&lt; Result;
  
  Result = shutdown(SocketNumber, SD_SEND);
  if (Result == SOCKET_ERROR) {
      cout&lt;&lt;"shutdown failed with error: "&lt;&lt; WSAGetLastError();
      closesocket(SocketNumber);
         getch();
      WSACleanup();
  
  }
  do {
  
      Result = recv(SocketNumber, recvbuf, recvbuflen, 0);
      if ( Result &gt; 0 )
          cout&lt;&lt;"Bytes received: "&lt;&lt; Result;   
      else if ( Result == 0 )
          cout&lt;&lt;"Connection closed";   
      else
          cout&lt;&lt;"recv failed with error: "&lt;&lt; WSAGetLastError();
         getch();
  } while( Result &gt; 0 );
  
  
  Result = closesocket(SocketNumber);
  if (Result == SOCKET_ERROR) {
      wprintf(L"close failed with error: %d\n", WSAGetLastError());
         getch();
      WSACleanup();
      return 1;
  }
  
  WSACleanup();
  
  return 0;
  }
  </code></pre>