Saturday, July 6, 2013

A Simple Server in C++

Makefile


default:
 g++ server.cpp
 ./a.out
clean:
 rm -f a.out

server.cpp


//
//  server.cpp
//  SimpleServer
//
//  Created by Kurry Tran on 7/3/13.
//  Copyright (c) 2013 Kurry Tran. All rights reserved.
//

#include <iostream>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <netinet/in.h>
#include <resolv.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <netdb.h>

void* SocketHandler(void*);

int main(int argc, const char * argv[])
{
    int host_port= 1101;
 struct sockaddr_in my_addr;
 int hsock;
 int *p_int;
    
 socklen_t addr_size = 0;
 int* csock;
 sockaddr_in sadr;
 pthread_t thread_id=0;
    
 hsock = socket(AF_INET, SOCK_STREAM, 0);
 if(hsock == -1){
        std::cout <<"Error initializing socket " << errno << std::endl;
  goto FINISH;
 }
 p_int = (int*)malloc(sizeof(int));
 *p_int = 1;
    
 if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
       (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
        std::cout <<"Error setting options " << errno << std::endl;
  free(p_int);
  goto FINISH;
 }
 free(p_int);
    
 my_addr.sin_family = AF_INET ;
 my_addr.sin_port = htons(host_port);
 
 memset(&(my_addr.sin_zero), 0, 8);
 my_addr.sin_addr.s_addr = INADDR_ANY ;
 
 if( bind( hsock, (sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){
        std::cerr << "Error binding to socket, make sure nothing else is listening on this port " << errno << std::endl;
  goto FINISH;
 }
    
 if(listen( hsock, 10) == -1 ){
        std::cerr << "Error listening " << errno << std::endl;
  goto FINISH;
 }
    
 addr_size = sizeof(sockaddr_in);
 
 while(true){
        std::cout <<"waiting for a connection on localhost:"<<host_port<< std::endl;
  csock = (int*)malloc(sizeof(int));
  if((*csock = accept( hsock, (sockaddr*)&sadr, &addr_size))!= -1){
            std::cout <<"---------------------\nReceived connection from "<< inet_ntoa(sadr.sin_addr) << std::endl;
   pthread_create(&thread_id,0,&SocketHandler, (void*)csock );
   pthread_detach(thread_id);
  }
  else{
            std::cerr << "Error accepting " << errno << std::endl;
  }
 }
    
FINISH:
    ;
}

void *SocketHandler(void *lp){
    int *csock = (int *)lp;
    
 char buffer[1024];
 int buffer_len = 1024;
 ssize_t bytecount;
    
 memset(buffer, 0, buffer_len);
 if((bytecount = recv(*csock, buffer, buffer_len, 0))== -1){
        std::cerr << "Error receiving data " << errno << std::endl;
  goto FINISH;
 }
    std::cout << "Received bytes " << bytecount << std::endl << "Received string "<< buffer << std::endl;

 if((bytecount = send(*csock, buffer, strlen(buffer), 0))== -1){
        std::cerr << "Error sending data " << errno << std::endl;
  goto FINISH;
 } 
    std::cout << "Sent bytes " << bytecount << std::endl;
    
FINISH:
    close(*csock);
 free(csock);
    return 0;
}


3 comments :

Packers And Movers Bangalore said...

It has always been my belief that very good writing such as this takes researching and also talent. It’s very apparent you have performed your homework.Good job!Also visit my web blog …
Packers And Movers Bangalore
Packers And Movers Bangalore

steve said...


Thanks
شركة شحن عفش من المدينة المنورة لمصر
ارقام شراء الاثاث المستعمل بالاحساء

شركة شراء اثاث مستعمل بالدمام

Manish Packers and Movers Pvt Ltd said...

Packers and Movers Indore - Call 09303355424, Local Household Shifting in Indore, Domestic Home Relocation from Indore. International Home Relocation from Indore, Office Shifting within Indore, Car and Bike Transportation from Indore, Safe Packing and Moving Services,
Packers and Movers Indore
Packers and Movers Gurgaon
Packers and Movers Kolkata
Packers and Movers Mumbai
Packers and Movers Delhi
Packers and Movers Jaipur
Packers and Movers Raipur
Packers and Movers Pune
Packers and Movers Ahmedabad
Packers and Movers Indore Blog