Subversion Repositories f9daq

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
235 f9daq 1
/*
2
    C socket server example, handles multiple clients using threads */
3
 
4
#include<stdio.h> 
5
#include<string.h> //strlen 
6
#include<stdlib.h> //strlen 
7
#include<sys/socket.h> 
8
#include<arpa/inet.h> //inet_addr 
9
#include<unistd.h> 
10
#//write 
11
#include<pthread.h>
12
//for threading , link with lpthread
13
#define PORT 3210
14
//the thread function 
15
 
16
 
17
 
18
void *connection_handler(void *);
19
 
20
int is_connected = 0;
21
int ncalls=0;
22
 
23
int writesock(int sock, char *msg, int len ){
24
    int hdr[2];
25
    hdr[0]=ncalls++;
26
    hdr[1]= len + 8;
27
    write(sock , hdr , 8);
28
    return write(sock , msg , len);
29
}
30
 
31
 
32
int main(int argc , char *argv[]) {
33
    int socket_desc , client_sock , c , *new_sock=NULL;
34
    struct sockaddr_in server , client;
35
 
36
    //Create socket
37
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
38
    if (socket_desc == -1)
39
    {
40
        printf("Could not create socket");
41
    }
42
    puts("Socket created");
43
 
44
    //Prepare the sockaddr_in structure
45
    server.sin_family = AF_INET;
46
    server.sin_addr.s_addr = INADDR_ANY;
47
    server.sin_port = htons( PORT );
48
 
49
    //Bind
50
    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
51
    {
52
        //print the error message
53
        perror("bind failed. Error");
54
        return 1;
55
    }
56
    puts("bind done");
57
 
58
    //Listen
59
    listen(socket_desc , 3);
60
 
61
    //Accept and incoming connection
62
    puts("Waiting for incoming connections...");
63
    c = sizeof(struct sockaddr_in);
64
 
65
 
66
    //Accept and incoming connection
67
    puts("Waiting for incoming connections...");
68
    c = sizeof(struct sockaddr_in);
69
    while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
70
    {
71
        puts("Connection accepted");
72
 
73
        pthread_t sniffer_thread;
74
 
75
        if (is_connected){
76
            char message[0xFF]="Only one client can connect. Disconnect it first!";
77
            perror(message);
78
            writesock(client_sock , message , strlen(message));
79
            close (client_sock);
80
            continue;
81
        }
82
        new_sock = (int *) malloc(1);
83
        *new_sock = client_sock;
84
        if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
85
        {
86
            perror("could not create thread");
87
            return 1;
88
        }
89
 
90
 
91
        //Now join the thread , so that we dont terminate before the thread
92
        //pthread_join( sniffer_thread , NULL);
93
        puts("Handler assigned");
94
 
95
    }
96
 
97
    if (client_sock < 0)
98
    {
99
        perror("accept failed");
100
        return 1;
101
    }
102
 
103
    return 0;
104
}
105
 
106
 
107
 
108
void *daq_handler(void *data){
109
 
110
 int s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
111
 //Get the socket descriptor
112
 int sock = *(int*)data;
113
 char msg[0xFF];
114
 int cnt=0;
115
 while (1){
116
    sprintf(msg, "daq_handler %d %d", sock, cnt++);
117
    int error = 0;
118
    socklen_t len = sizeof (error);
119
    int retval = getsockopt (sock, SOL_SOCKET, SO_ERROR, &error, &len);
120
    if (retval!=0 || error!=0) break;
121
    writesock(sock , msg , strlen(msg));
122
    sleep(5);
123
 }
124
 printf("Exiting thread %d", sock ) ;
125
 
126
}
127
 
128
/*
129
 * This will handle connection for each client
130
 * */
131
 
132
void *connection_handler(void *socket_desc) {
133
    //Get the socket descriptor
134
    int sock = *(int*)socket_desc;
135
    int read_size;
136
    char *message , client_message[2000];
137
    is_connected = 1;
138
    //Send some messages to the client
139
    message = "Info from connection handler\n";
140
    writesock(sock , message , strlen(message));
141
 
142
    //Receive a message from client
143
    pthread_t daq_thread = 0;
144
 
145
    while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
146
    {
147
        //Send the message back to client
148
        int * hdr = (int *) client_message;
149
        char *msg = &client_message[8];
150
        printf("Received %d bytes RECID = %d LEN %d\n",read_size, hdr[0], hdr[1]);
151
        switch (hdr[0]){
152
           case 0:
153
           hdr[0]= sock;
154
           if( pthread_create( &daq_thread , NULL , daq_handler , (void*) &client_message) < 0)
155
        {
156
            perror("could not create daq thread");
157
            break ;
158
        }
159
 
160
break;
161
           case 3:
162
         if (daq_thread) {
163
            pthread_cancel(daq_thread);
164
            daq_thread = 0;
165
         }
166
break;
167
           default: break;
168
 
169
        }
170
        writesock(sock , msg , strlen(msg));
171
    }
172
 
173
    if(read_size == 0)
174
    {
175
        puts("Client disconnected");
176
        fflush(stdout);
177
    }
178
    else if(read_size == -1)
179
    {
180
        perror("recv failed");
181
 
182
    }
183
    if (daq_thread) {
184
      pthread_cancel(daq_thread);
185
      daq_thread = 0;
186
    }
187
 
188
 
189
    //Free the socket pointer
190
    free(socket_desc);
191
    socket_desc = 0;
192
    is_connected = 0;
193
 
194
    return 0;
195
}
196