All pastes #76470 Raw Edit

Someone

public text v1 · immutable
·published 2006-07-01 17:22 UTC
rendered paste body
#include <stdio.h>
#include <conio.h>
 
#include <winsock.h> //sys/socket.h for linux..
 
WSADATA W;
 
typedef struct SocketHandler
{
       SOCKET socket;
       struct sockaddr_in * sockAddress;
       char buffer[1024];
}SockH;
 
void SetConnection(SockH * sockh, struct sockaddr_in * b, char * address, unsigned short port)
{
     sockh->sockAddress=b;
     //printf("%d",sockh->sockAddress);
     sockh->sockAddress->sin_family=AF_INET; //ip family... (right now it's IPv4 tcp/ip)
     sockh->sockAddress->sin_port=htons(port); //sin_port is a short variable (in the structure ofcourse) and he is being used as the socket's port number. howerver, we cant just use a numerical value 80 in the socket and we have to convert into network bytes order for short values by using the "htons" function.
     sockh->sockAddress->sin_addr.s_addr=inet_addr(address);
     sockh->socket = socket(sockh->sockAddress->sin_family, SOCK_STREAM, 0);
}
 
void OpenConnection(SockH * sockh)
{
     if(connect(sockh->socket,(struct sockaddr *)(sockh->sockAddress), sizeof(*sockh->sockAddress))==0);
}
 
void Send(SockH * sockh,char * text)
{
     send(sockh->socket, text, strlen(text), 0);
}
 
void Receive(SockH * sockh)
{
     recv(sockh->socket, sockh->buffer, sizeof(sockh->buffer), 0);
 }
 
 int CloseConnection(SockH * sockh)
 {
      closesocket(sockh->socket);
      return 1;
 }
 
 void Write(char * text)
 {
      printf("%s \n", text);
 }
 
 
int main()
{
    struct sockaddr_in a;
    SockH sockets;
    int i;
    WSAStartup( 0x101, &W );
 Write("[+] Setting Connection.");
 SetConnection(&sockets,&a,"212.199.126.132",80);
  Write("[+] Openning connection.");
 OpenConnection(&sockets);
  Write("[+] Sending text...");
  Send(&sockets,"GET /index.php HTTP /1.1 \n\r");
  Write("[+] Receiving text...");
  Receive(&sockets);
  Write("\n\n\n\n\n\n");
  Write("[+] Text Received:\n");
 printf("%s [END OF DATA]\n", sockets.buffer);
 CloseConnection(&sockets);
 Write("\n\n\n\nPress enter to continue...");
getch();
return 0;
}