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;
}


2 comments :

Komali said...

Very Nice Blog...

Window Phones

brand new outlook

Rethu said...

Hey Nice Blog!!! Thank you for sharing information. Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!

Are you a social media narcissist? And you would like to be the same person on social media as you are in real life? ‘Yes’ then install

Click below link

Bigo Live Connector
50 Million Bigo Live connector
Bigo Live PC connecto
Bigo Live
Live Stream