/***************************************************************************
* Copyright (C) 2007 by mati *
* infecto@lcm.pl *
* *
* 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., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <pthread.h>
#define MAX_MSG 100
#define LINE_ARRAY_SIZE (MAX_MSG+1)
#include "serwer.h"
using namespace std;
int main(int argc, char** argv)
{
pthread_t thread1, thread2;
int listenSocket, connectSocket, i;
int iret1, iret2,message1;
unsigned short int listenPort;
socklen_t clientAddressLength;
struct sockaddr_in clientAddress, serverAddress;
char line[LINE_ARRAY_SIZE];
listenPort = atoi(argv[1]);
// Create socket for listening for client connection requests.
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket < 0) {
cerr << "cannot create listen socket";
exit(1);
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(listenPort);
if (bind(listenSocket,
(struct sockaddr *) &serverAddress,
sizeof(serverAddress)) < 0) {
cerr << "cannot bind socket";
exit(1);
}
listen(listenSocket, 5);
while (1) {
cout << "Waiting for TCP connection on port " << listenPort << " ...\n";
clientAddressLength = sizeof(clientAddress);
connectSocket = accept(listenSocket,(struct sockaddr *) &clientAddress,&clientAddressLength);
if (connectSocket < 0) {
cerr << "cannot accept connection ";
exit(1);
} else {
cout << " connected to " << inet_ntoa(clientAddress.sin_addr);
cout << ":" << ntohs(clientAddress.sin_port) << "\n";
memset(line, 0x0, LINE_ARRAY_SIZE);
//to wpakowac w watek ktory w parametrze dostanie soketa na ktorym ma sie komunikowac
// while (recv(connectSocket, line, MAX_MSG, 0) > 0) {
// cout << " -- " << line << " connected to " << connectSocket << "\n";
// for (i = 0; line[i] != '\0'; i++)
// line[i] = toupper(line[i]);
// if (send(connectSocket, line, strlen(line) + 1, 0) < 0)
// cerr << "Error: cannot send modified data";
// memset(line, 0x0, LINE_ARRAY_SIZE); // set line to all zeroes
// }
}
}
}