Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by Vogelino ( 14 years ago )
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>

int main (int argc, char *argv[] )
{
  int mySocket, n;
  struct sockaddr_in sin; struct hostent *host;
  char msg[1000] = "HEAD / HTTP/1.1\r\nHost: http://www.fit.vutbr.cz\r\n\r\n" ;
  
  if ( (mySocket = socket(AF_INET, SOCK_STREAM, 0 ) ) == -1 ) { /* create socket*/
    perror("error on socket");  /* socket error */
    return -1;
  }
  sin.sin_family = AF_INET;              /*set protocol family to Internet */
  sin.sin_port = htons(atoi(argv[2]));  /* set port no. */
  if ( (host =  gethostbyname(argv[1]) ) == NULL){
    fprintf(stderr, "gethostname error: %s", argv[1]);
    return -1;
   }
  memcpy( &sin;.sin_addr, host->h_addr, host->h_length);
  if (connect (mySocket, (struct sockaddr *)&sin;, sizeof(sin) ) == -1 ){
    perror("error on connect"); return -1;   /* connect error */
  }
  if ( write(mySocket, msg, strlen(msg) +1) == -1 ) {  /* send message to server */
    perror("error on write");    return -1; /*  write error */
  }
  if ( ( n = read(mySocket, msg, sizeof(msg) ) ) == -1) {  /* read message from server */
    perror("error on read"); return -1; /*  read error */
  }
  printf ("received %d bytes: %s\n", n, msg);  /* print message to screen */
  /* close connection, clean up socket */
  if (close(mySocket) < 0) { 
    perror("error on close");   /* close error */
    return -1;
  }
  return 0;
}

 

Revise this Paste

Parent: 39822
Children: 46969
Your Name: Code Language: