Follow us on Twitter!
Imagination is more valuable than knowledge - Albert Einstein
Monday, April 21, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 25
Guests Online: 24
Members Online: 1

Registered Members: 82852
Newest Member: sockpuppets
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Win Socks


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 01-06-10 10:36
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


Code

#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


Code

#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


img337.imageshack.us/img337/4299/capturesh.jpg


Thanks
Author

RE: Win Socks

WightBoy
Member



Posts: 35
Location: Australia - NSW
Joined: 08.06.09
Rank:
Apprentice
Posted on 01-06-10 11:09
Analyze the following lines, see if you can spot your error, explained below.
Code

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.


i195.photobucket.com/albums/z148/gabfrab/sig-1.jpg

Edited by WightBoy on 01-06-10 11:24
http://onewayexpress.ismywebsite.com
Author

RE: Win Socks


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 02-06-10 13:13
Thanks!!!