Repository navigation
Expand file tree
/
Copy pathBroadcastClient1.cpp
More file actions
150 lines (119 loc) · 3.7 KB
/
Copy pathBroadcastClient1.cpp
File metadata and controls
150 lines (119 loc) · 3.7 KB
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
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <netinet/in.h>
#include <resolv.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include <vector>
#include <pthread.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#define SIZE 1024
pthread_mutex_t mutexBuffer;
using namespace google::protobuf::io;
using namespace std;
volatile sig_atomic_t flag = 0;
char message[SIZE];
void* sendMessage(void*);
void* receiveMessage(void*);
int main(int argv, char** argc){
int host_port= 9016;
char host_name[]="172.20.3.153";
struct sockaddr_in my_addr;
char buffer[1024];
char server_message[1024]={0};
int bytecount;
int buffer_len=0;
pthread_mutex_init(&mutexBuffer, NULL);
int hsock;
int * p_int;
int err;
pthread_t send ;
pthread_t receive;
hsock = socket(AF_INET, SOCK_STREAM, 0);
if(hsock == -1){
printf("Error initializing socket %d\n",errno);
goto FINISH;
}
p_int = (int*)malloc(sizeof(int));
*p_int = 1;
if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
(setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
printf("Error setting options %d\n",errno);
free(p_int);
goto FINISH;
}
free(p_int);
my_addr.sin_family = AF_INET ;
my_addr.sin_port = htons(host_port);
memset(&(my_addr.sin_zero), 0, 8);
my_addr.sin_addr.s_addr = inet_addr(host_name);
if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){
if((err = errno) != EINPROGRESS){
fprintf(stderr, "Error connecting socket %d\n", errno);
goto FINISH;
}
}
pthread_t recv_msg;
if(pthread_create(&recv_msg, NULL, receiveMessage, (void*)&hsock) != 0){
cout << "Error creating thread" << endl;
return EXIT_FAILURE;
}
pthread_t send_msg;
if(pthread_create(&send_msg, NULL, sendMessage, (void*)&hsock) != 0){
cout << "Error creating thread" << endl;
return EXIT_FAILURE;
}
while (1){
if(flag){
printf("\nDisconnected\n");
break;
}
}
FINISH:
close(hsock);
}
void* sendMessage(void* lp){
int *csock = (int*)lp;
int bytecount=0;
cout << "\n\n" << endl;
while(true) {
if(strlen(message) != '\0') {
pthread_mutex_lock(&mutexBuffer);
if ((bytecount = send(*csock, message, SIZE, 0)) == -1) {
cerr << "Error sending data " << endl;
}
memset(message, 0, sizeof(message));
pthread_mutex_unlock(&mutexBuffer);
}
}
}
void* receiveMessage(void* lp){
int *csock = (int*)lp;
int bytecount=0;
char buffer[4];
while(true){
if(strlen(message) == '\0') {
pthread_mutex_lock(&mutexBuffer);
if ((bytecount = recv(*csock, message, SIZE, 0)) == -1) {
cerr << "Error receiving data " << endl;
}
else{
/*if(strcmp(message, "shutdown")==0)
{
strcpy(message, "Shutting down client\n");
sendMessage(csock);
return 0;
}*/
cout << message << endl;
}
pthread_mutex_unlock(&mutexBuffer);
}
}
}