All pastes #403734 Raw Edit

Mine

public cpp v1 · immutable
·published 2007-03-20 16:18 UTC
rendered paste body
#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;void *obsluga (void *arg) {int i,fd = *(int *)arg;char line[LINE_ARRAY_SIZE];printf("watek\n");                        while (recv(fd, line, MAX_MSG, 0) >0) {                                cout << "  --  " << line << "  connected to " << fd << "\n";                             for (i = 0; line[i] != '\0'; i++)                                     line[i] = toupper(line[i]);                                if (send(fd, line, strlen(line) + 1, 0) < 0)                                     cerr << "Error: cannot send modified data";                                     memset(line, 0x0, LINE_ARRAY_SIZE);  // set line to all zeroes                        } pthread_exit(0);}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);                     pthread_create( &thread1, NULL, obsluga , &connectSocket );                     //to wpakowac w watek ktory w parametrze dostanie soketa na ktorym ma sie komunikowac//                           }                }             }}