I have some experience with client-server codes. TCP and udp client. I want to know how a stream socket works with HTTP.
This client is for a server-client code where the greatest common divisor is calculated and displayed.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#define MAX_BUFFER_LENGTH 100
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in their_addr; // connector's address information
struct hostent *he;
int numbytes;
int serverPort;
int a = 0;
int b = 0;
struct timespec time_a, time_b;
printf("TCP client example \n\n");
if (argc != 5) {
fprintf(stderr,"Usage: tcpClient serverName serverPort int1 int2\n");
exit(1);
}
serverPort = atoi(argv[2]);
a = atoi(argv[3]);
b = atoi(argv[4]);
//Resolv hostname to IP Address
if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
herror("gethostbyname");
exit(1);
}
/* ******************************************************************
Create socket
******************************************************************* */
clock_gettime(CLOCK_REALTIME, &time_a);
sockfd = socket(PF_INET,SOCK_STREAM,0);
//setup transport address
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(serverPort);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
/* ******************************************************************
Binding
******************************************************************* */
connect(sockfd,(struct sockaddr*)&their_addr, sizeof(their_addr));
printf("Connection established \n");
unsigned char buffer[4];
packData(&buffer, a, b);
printf("Data packed \n");
/* ******************************************************************
Send data
******************************************************************* */
send(sockfd,buffer,sizeof(buffer),0);
printf("Data Sent \n");
printf("A: %d and B: %d. \n", a,b );
/* ******************************************************************
Close socket
******************************************************************* */
close(sockfd);
clock_gettime(CLOCK_REALTIME, &time_b);
double diff = (time_b.tv_sec - time_a.tv_sec) + ((time_b.tv_nsec - time_a.tv_nsec)/1E9);
printf("Client needed %lf sec.\n\n", diff);
return 0;
}
int packData(unsigned char *buffer, unsigned int a, unsigned int b) {
/* ******************************************************************
pack data
******************************************************************* */
buffer[0]=a>>8;
buffer[1]=a;
buffer[2]=b>>8;
buffer[3]=b;
}
That it works with netcat, for instance. How do I rewrite the code to make it work with HTTP ? I been trying to figure it out, but nothing so far. It should only be some minor changes.