1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| /*
* File: main.cpp
* Author: x
*
* Created on 3 janvier 2010, 16:51
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <pthread.h>
using namespace std;
#define MYPORT "8001" // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
#define MAXLEN 1024
#define MAXPLAYER 100
#define NTHREADS 15 //le nombre de thread max gérés.
#define errexit(code,str) \
fprintf(stderr,"%s: %s\n",(str),strerror(code)); \
exit(1);
/*
*
*/
struct thread_data
{
int thread_id;
int sum;
char *message;
};
void *hola(void * threadarg)
{
int errcode;
socklen_t addr_size;
int sockfd;
int* new_fd;
void*buf;
char * recvbuf = new char [MAXLEN];
int taskid, sum;
char *hello_msg;
struct thread_data *my_data;
bool test=false;
sleep(1);
my_data = (struct thread_data *) threadarg;
taskid = my_data->thread_id;
sum = my_data->sum;
hello_msg = my_data->message;
cout << "Thread " << taskid << ": ";
if(hello_msg !=NULL)
{
cout << "MSG => ";
cout << hello_msg;
}
else
cout << "NO MESSAGE";
cout << " Sum="<< sum << endl;
test=true;
struct addrinfo hints, *res, *p;
struct sockaddr_storage their_addr;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
getaddrinfo(NULL, MYPORT, &hints, &res);
// make a socket:
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
// bind it to the port we passed in to getaddrinfo():
bind(sockfd, res->ai_addr, res->ai_addrlen);
// now accept an incoming connection:
cout << "Thread " << taskid << "will listening" << endl;
listen(sockfd, BACKLOG);
cout << "Thread " << taskid << "was listening" << endl;
addr_size = sizeof their_addr;
new_fd=new int[MAXPLAYER];
int nbr;
int i,j=0;
cout << "Thread " << taskid << "will accept" << endl;
new_fd[j] = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
cout << "Thread " << taskid << ": " << "recv will begin" << endl;
while(nbr=recv(new_fd[j], recvbuf, MAXLEN, 0))
{
recvbuf[nbr]=0;
cout << endl << "Str recu thread nbr" << taskid << ": ";
cout<<recvbuf;
}
freeaddrinfo(res); // free the linked list
pthread_exit(NULL);
}
int main(int argc, char** argv) {
int worker;
pthread_t threads[NTHREADS]; /* holds thread info */
int ids[NTHREADS]; /* holds thread args */
struct thread_data _session[NTHREADS];
int status;
char ipstr[INET6_ADDRSTRLEN];
for (worker=0; worker<NTHREADS; worker++)
{
ids[worker]=worker;
_session[worker].thread_id=worker;
_session[worker].message="Hello World";
_session[worker].sum=sizeof(_session[worker].message);
if(0!=pthread_create(&threads[worker],NULL,hola,&_session[worker]))
perror("impossible de créer le thread:");
}
while(1)
{
}
/*
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((status = getaddrinfo("192.168.1.10", NULL, &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for 192.168.1.10:\n\n");
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// convert the IP to a string and print it:
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf(" %s: %s\n", ipver, ipstr);
}
//*/
return 0;
} |
Partager