Skip to content

Instantly share code, notes, and snippets.

@abuiles
Created May 26, 2009 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abuiles/118093 to your computer and use it in GitHub Desktop.
Save abuiles/118093 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>
#include <curl/curl.h>
#include <arpa/inet.h>
using namespace std;
#define PORT 1111
#define MAXMSG 512
int main (void)
{
struct sockaddr_in server,client;
size_t size;
int sock,cr;
char buff [MAXMSG];
FILE* fp;
sock = socket (PF_INET,SOCK_STREAM,0);
if (sock < 0)
{
perror ("socket");
exit (EXIT_FAILURE);
}
server.sin_family= AF_INET;
server.sin_port = htons (PORT);
server.sin_addr.s_addr = htonl(INADDR_ANY);
if ( bind(sock, (struct sockaddr *) &server, sizeof (server)) < 0)
{
perror ("bind");
exit (EXIT_FAILURE);
}
int l = listen(sock,2) ;
if (l < 0 ){
perror("Listening");
exit(EXIT_FAILURE);
}
int fromlen;
cr= accept (sock,(sockaddr*) &client,(socklen_t*) &fromlen);
if(cr<0 ){
perror ("Accepting ");
exit(EXIT_FAILURE);
}
cout << inet_ntop(AF_INET, &client.sin_addr,buff, sizeof(buff))<<"\n";
fp = fdopen (cr, "r+");
close (cr);
bool finished = false;
char x;
while (x= (char) fgetc(fp) ){
cout << x;
if ( x == '\r')
break;
}
// cout << "test \n";
// char c;
// for (int i = 0; i <20; i++) {
// while (( c = fgetc(fp)) != EOF) {
// //fputc((int)c,stdout);
// cout<< c;
// if (c == '\n' || c == '\r')
// break;
// }
// }
//req = fdopen(ns,"r+");
//int nbytes;
//nbytes = read (cr, buffer, MAXMSG);
// if (nbytes < 0)
// {
// /* Read error. */
// perror ("read");
// exit (EXIT_FAILURE);
// }
// else if (nbytes == 0)
// /* End-of-file. */
// return -1;
// else
// {
// /* Data read. */
// fprintf (stderr, "Server: got message: ‘%s’\n", buffer);
// return 0;
// }
cout << "something \n";
close (sock);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment