Jump to content
Sign in to follow this  

Winsock program oppfører seg rart

Recommended Posts

#include <stdio.h>
#include "winsock2.h"
#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;
int main(int argc, char *argv[]) {
   // MessageBox(NULL, "hallo", "socketIndication", MB_OK);
              return 1;
   // Initialize Winsock.
   WSADATA wsaData;
   int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
   if ( iResult != NO_ERROR )
       printf("Error at WSAStartup()\n");

   // Create a socket.
   SOCKET m_socket;
   m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

   if ( m_socket == INVALID_SOCKET ) {
       printf( "Error at socket(): %ld\n", WSAGetLastError() );
       return 1;

 SOCKADDR_IN serverInfo;
LPHOSTENT hostEntry;
serverInfo.sin_family = AF_INET;
hostEntry = gethostbyname("counter.rapidcounter.com");	// Specifying the server by its name;
     	// another option is gethostbyaddr() (see below)

if (!hostEntry) {

serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

serverInfo.sin_port = htons(80);	

   if ( connect( m_socket, (SOCKADDR*) &serverInfo, sizeof(serverInfo) ) == SOCKET_ERROR) {
       printf( "Failed to connect.\n" );
       return 1;

  char sendbuf[2000]="GET /counter/081";
    strcat(sendbuf, " HTTP/1.1\r\nHost: ");
   strcat(sendbuf, "counter.rapidcounter.com");
   strcat(sendbuf, "\r\nUser-Agent: who_cares?\r\n\r\n");
   // strcat(sendbuf, "Referer: google.com\r\n");
   // strcat(sendbuf, "Connection: close\r\n\r\n");
   cout<< sendbuf << "\n";
   char recvbuf[500];

  int i=0;
   send( m_socket, sendbuf, sizeof(sendbuf), 0 );
   return 1;


Ja folkens, counteren blir altså ikke oppdatert. Har vel noe med at det er et bilde, eller jeg vet ikke. Noen som vet ?

Share this post

Link to post

Du kan ikke ha doble newlines mellom ting i headeren. Aka du kan ikke ha \r\n 2x imellom User-Agent,Referer og Connection linjene. Du skal kun ha dobbel \r\n par etter siste header linje, aka etter Connection i din kode.

Og ps: du har jo sagt til srv at connection skal closes etter request, så while loopen din vil ikke fungere.

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...