Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Win Socks


ghost's Avatar
0 0

I recently started programming(Recently–yesterday) I coded a really basic Server-Client pair… I got some errors,but resolved them.

However,I was unable to get the desired output. Instead of displaying the sent message ("Hello\n"), It is showing some other characters(not even letters.

I post here,the code for both the client and server as well as a screenshot of the output of the server.

Server

#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <winsock2.h> //calls the winsocket version 2 header file
main()
{
      //Start WinSock:
      WSADATA wsaData; //a required parameter for WSAStartup();
      WSAStartup(MAKEWORD(2,2), &wsaData); // requests Winsocket version 2.2
      
      if ( (LOBYTE( wsaData.wVersion ) != 2)||(HIBYTE( wsaData.wVersion ) !=2 ))
     {
         system("pause");
     }
     else
     {
         puts("Version correct");
     }

      
      //Create Socket:
      SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0); //this line creates the socket
      
       //-----------------------------------------------------------------------
      //------------------------------------------------------------------------
      
      if (mysock == INVALID_SOCKET) 
      { // if anything goes wrong...
      cout << "Error: " << WSAGetLastError() << " occurred!" << endl;
      system ("pause");
      }
      else
      {
          puts("Socket Created");
      }
      
      
       //-----------------------------------------------------------------------
      //------------------------------------------------------------------------
      
            
      //Bind Socket:
     sockaddr_in anews;
      anews.sin_port = htons(87);
      anews.sin_addr.s_addr = INADDR_ANY;
      anews.sin_family = AF_INET;
      if (bind(mysock, (sockaddr*)&anews, sizeof(anews)) == SOCKET_ERROR) 
 
      //------------------------------------------------------------------------
      //------------------------------------------------------------------------
      
      {
       cout << "Error: " << WSAGetLastError() << " occurred!" << endl;
       system ("pause");
       return 0;
      }
      else
      {
          puts("Binded socket");
      }
      
      //------------------------------------------------------------------------
      //------------------------------------------------------------------------
      
      //Listen:
      while(listen(mysock,SOMAXCONN)==SOCKET_ERROR);
      
      //Accept connection:
      SOCKET client;
      client = accept(mysock,NULL,NULL);
      //recieve info:
      char buf[200];
      recv(mysock, buf, sizeof(buf), 0);
      
      //close winsock:
      closesocket(mysock);
      closesocket(client);
      WSACleanup();

      //show recived message:
      puts("Received: \n");
      puts(buf);
      system("pause");//Pause
      
      //return:
      return 0;
}

Client

#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
using namespace std;

int main () 
{
WSADATA wsaData;
int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
SOCKET mysock = socket(AF_INET,SOCK_STREAM,0);
sockaddr_in anews;
anews.sin_port = htons(87);
anews.sin_addr.s_addr = inet_addr("127.0.0.1");
anews.sin_family = AF_INET;
connect(mysock,(sockaddr*)&anews, sizeof(anews));
char buf[200] = "Hello\n";
send(mysock, buf, sizeof(buf), 0);
puts(buf);
system("pause");
closesocket(mysock);
WSACleanup();
return 0;
}

Output

Thanks


WightBoy's Avatar
Member
0 0

Analyze the following lines, see if you can spot your error, explained below.

SOCKET client;
client = accept(mysock,NULL,NULL);
//recieve info:
char buf[200];
recv(mysock, buf, sizeof(buf), 0);

When you accept() the connections that are queued up during the listen() call, a new socket is returned (by the accept()). So you have to use the newly created socket for dealing with send()'ing and recv()'ing.


ghost's Avatar
0 0

Thanks!!!