Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined (WIN32)
- #include <winsock2.h>
- #include <pthread.h>
- typedef int socklen_t;
- #elif defined (linux)
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #define INVALID_SOCKET -1
- #define SOCKET_ERROR -1
- #define closesocket(s) close(s)
- typedef int SOCKET;
- typedef struct sockaddr_in SOCKADDR_IN;
- typedef struct sockaddr SOCKADDR;
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "network.h"
- #define PORT 23
- int sendData(int csock, Question question){
- int error;
- error = send(csock, &question, sizeof(question), 0);
- if(error == SOCKET_ERROR){
- return 0;
- }
- return 1;
- }
- char receiveData(int csock){
- char bufferRecv[50];
- int error;
- error = recv(csock, bufferRecv, sizeof(bufferRecv)-1, 0);
- if(error != SOCKET_ERROR){
- bufferRecv[error] = '\0';
- printf("client dit: %s \n", bufferRecv);
- return bufferRecv;
- }else{
- return '0';
- }
- }
- int init_connection(){
- WSADATA WSAData;
- WSAStartup(MAKEWORD(2,0), &WSAData);
- SOCKADDR_IN server, client;
- int clientSock, c;
- SOCKET socket_desc;
- socket_desc = socket(AF_INET, SOCK_STREAM, 0);
- if (socket_desc == -1)
- {
- printf("Could not create socket");
- }
- puts("Socket created");
- //Prepare the sockaddr_in structure
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = INADDR_ANY;
- server.sin_port = htons( 8888 );
- //Bind
- if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
- {
- //print the error message
- perror("bind failed. Error");
- return 1;
- }
- puts("bind done");
- //Listen
- listen(socket_desc , 3);
- //Accept and incoming connection
- puts("Waiting for incoming connections...");
- c = sizeof(struct sockaddr_in);
- //Accept and incoming connection
- puts("Waiting for incoming connections...");
- c = sizeof(struct sockaddr_in);
- pthread_t thread_id;
- while( (clientSock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
- {
- puts("Connection accepted");
- if( pthread_create( &thread_id , NULL , connectionHandler , (void*) &clientSock) < 0)
- {
- perror("could not create thread");
- return 1;
- }
- //Now join the thread , so that we dont terminate before the thread
- //pthread_join( thread_id , NULL);
- puts("Handler assigned");
- printf("Un client se connecte avec la socket %d de %s:%d\n", clientSock, inet_ntoa(server.sin_addr), htons(server.sin_port));
- }
- if (clientSock < 0)
- {
- perror("accept failed");
- return 1;
- }
- return 0;
- }
- void *connectionHandler(void *socket_desc)
- {
- //Get the socket descriptor
- int sock = *(int*)socket_desc;
- int read_size;
- int numberQ; /**< rajouter la commande sql de comptage des question */
- char *message , client_message[2000];
- char bufferSend[50];
- Question question;
- numberQ = question.numberQ;
- strcpy(question.email, "hubertgauthier5@gmail.com");
- question.id_question = malloc(sizeof(int)* numberQ);
- sendData(sock, question);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement