-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.c
86 lines (67 loc) · 1.88 KB
/
Client.c
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
#include<stdio.h>
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include<pthread.h>
#include <unistd.h>
#define MAX 100
#define SockAdd struct sockaddr
#define PORT 8080
#define max_thread 3000
int mainSock;
struct sockaddr_in servInfo;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void func(int mainSock){
char msg[300];
char buf[300];
bzero(buf,sizeof(buf));
puts("\n------------------------ Listening from client side -------------- \n");
read(mainSock,buf,sizeof(buf));
printf("Server message ---->> %s",buf);
}
void *mulClient(void *vargp){
pthread_mutex_lock(&lock);
mainSock= socket(AF_INET,SOCK_STREAM,0);
if(mainSock ==-1){
printf("\n Main Socket Failed \n");
}else{
printf("\n Main Socket Success \n");
}
bzero(&servInfo, sizeof(servInfo));
servInfo.sin_family = AF_INET;
//servInfo.sin_addr.s_addr = INADDR_ANY;
//servInfo.sin_addr.s_addr = "10.0.2.15";
servInfo.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "10.0.2.15", &servInfo.sin_addr)<=0) {
printf("\nInvalid address/ Address not supported \n");
exit(0);
}
if(connect(mainSock,(SockAdd*)&servInfo,sizeof(servInfo))!=0){
puts("\n Inside Clinet Connection Failed \n");
exit(0);
}else{
puts("\n Inside Client Connect \n");
printf("mainSocket ==>> %d \n",mainSock);
}
func(mainSock);
pthread_mutex_unlock(&lock);
}
int main(){
int i;
pthread_t thread_id[max_thread];
for(i=0;i<max_thread;i++){
if((pthread_create(&thread_id[i],NULL,mulClient,NULL))!=0)
printf("\n Thread creation failed at ============ >>>>> %d \n ",i);
else
printf("\n Thread successfully created at =======>> %d\n",i);
}
for(i=0;i<max_thread;i++){
pthread_join(thread_id[i],NULL);
printf("\n after thread_id[%d] joining \n",i);
}
return 0;
}