1
+ /*
2
+ C socket server example, handles multiple clients using threads
3
+ */
4
+
5
+ #include <stdio.h>
6
+ #include <string.h> //strlen
7
+ #include <stdlib.h> //strlen
8
+ #include <sys/socket.h>
9
+ #include <arpa/inet.h> //inet_addr
10
+ #include <unistd.h> //write
11
+ #include <pthread.h> //for threading , link with lpthread
12
+
13
+ //the thread function
14
+ void * connection_handler (void * );
15
+
16
+ int main (int argc , char * argv [])
17
+ {
18
+ int socket_desc , client_sock , c , * new_sock ;
19
+ struct sockaddr_in server , client ;
20
+
21
+ //Create socket
22
+ socket_desc = socket (AF_INET , SOCK_STREAM , 0 );
23
+ if (socket_desc == -1 )
24
+ {
25
+ printf ("Could not create socket" );
26
+ }
27
+ puts ("Socket created" );
28
+
29
+ //Prepare the sockaddr_in structure
30
+ server .sin_family = AF_INET ;
31
+ server .sin_addr .s_addr = INADDR_ANY ;
32
+ server .sin_port = htons ( 8888 );
33
+
34
+ //Bind
35
+ if ( bind (socket_desc ,(struct sockaddr * )& server , sizeof (server )) < 0 )
36
+ {
37
+ //print the error message
38
+ perror ("bind failed. Error" );
39
+ return 1 ;
40
+ }
41
+ puts ("bind done" );
42
+
43
+ //Listen
44
+ listen (socket_desc , 3 );
45
+
46
+ //Accept and incoming connection
47
+ puts ("Waiting for incoming connections..." );
48
+ c = sizeof (struct sockaddr_in );
49
+
50
+
51
+ //Accept and incoming connection
52
+ puts ("Waiting for incoming connections..." );
53
+ c = sizeof (struct sockaddr_in );
54
+ while ( (client_sock = accept (socket_desc , (struct sockaddr * )& client , (socklen_t * )& c )) )
55
+ {
56
+ puts ("Connection accepted" );
57
+
58
+ pthread_t sniffer_thread ;
59
+ new_sock = malloc (1 );
60
+ * new_sock = client_sock ;
61
+
62
+ if ( pthread_create ( & sniffer_thread , NULL , connection_handler , (void * ) new_sock ) < 0 )
63
+ {
64
+ perror ("could not create thread" );
65
+ return 1 ;
66
+ }
67
+
68
+ //Now join the thread , so that we dont terminate before the thread
69
+ //pthread_join( sniffer_thread , NULL);
70
+ puts ("Handler assigned" );
71
+ }
72
+
73
+ if (client_sock < 0 )
74
+ {
75
+ perror ("accept failed" );
76
+ return 1 ;
77
+ }
78
+
79
+ return 0 ;
80
+ }
81
+
82
+ /*
83
+ * This will handle connection for each client
84
+ * */
85
+ void * connection_handler (void * socket_desc )
86
+ {
87
+ //Get the socket descriptor
88
+ int sock = * (int * )socket_desc ;
89
+ int read_size ;
90
+ char * message , client_message [2000 ];
91
+
92
+ //Send some messages to the client
93
+ message = "Greetings! I am your connection handler\n" ;
94
+ write (sock , message , strlen (message ));
95
+
96
+ message = "Now type something and i shall repeat what you type \n" ;
97
+ write (sock , message , strlen (message ));
98
+
99
+ //Receive a message from client
100
+ while ( (read_size = recv (sock , client_message , 2000 , 0 )) > 0 )
101
+ {
102
+ //Send the message back to client
103
+ write (sock , client_message , strlen (client_message ));
104
+ }
105
+
106
+ if (read_size == 0 )
107
+ {
108
+ puts ("Client disconnected" );
109
+ fflush (stdout );
110
+ }
111
+ else if (read_size == -1 )
112
+ {
113
+ perror ("recv failed" );
114
+ }
115
+
116
+ //Free the socket pointer
117
+ free (socket_desc );
118
+
119
+ return 0 ;
120
+ }
0 commit comments