/*
 *   tcpsrv           a very simple TCP server for benchmarking
 *   Copyright (c)    2006 Michal Trojnara <Michal.Trojnara@mobi-com.net>
 *                    All Rights Reserved
 *
 *   Version:         1.00
 *   Date:            2006.12.17
 *
 *   Author:          Michal Trojnara  <Michal.Trojnara@mobi-com.net>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main(int argc, char*argv[]) {
    int ls, fd, optval=1;
    struct sockaddr_in server;
    char c='*';

    ls=socket(AF_INET, SOCK_STREAM, 0);
    if(ls<0) {
        perror("socket");
        return 1;
    }

    if(setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))<0) {
        perror("setsockopt"); 
        return 1;
    }

    server.sin_family=AF_INET;
    server.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
    server.sin_port=ntohs(50002);
    if(bind(ls, (struct sockaddr*)&server, sizeof server)<0) {
        perror("bind");
        return 1;
    }

    listen(ls, 10);
    for(;;) {
        fd=accept(ls, NULL, NULL);
        if(fd==-1) {
            perror("accept");
            continue;
        }
        if(write(fd, &c, 1)!=1)
            perror("write");
        close(fd);
    }

    return 0;
}

/* end of tcpsrv.c file */
