-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunication.cpp
More file actions
149 lines (114 loc) · 2.43 KB
/
Copy pathcommunication.cpp
File metadata and controls
149 lines (114 loc) · 2.43 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
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<cstdlib>
#include<string.h>
#include<sstream>
#include<ctime>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<map>
#define MAX_MESSAGE_LEN 65536
using namespace std;
typedef map<string,struct sockaddr_in> channel_type;
struct sockaddr_in my_sock;
struct sockaddr_in next_sock;
//ofstream fout[4000];
void client_process::socket_creator()
{
int i,rc;
s = socket(PF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
//perror ("socket() failed\n");
exit(1);
}
cerr<<endl<<"socket created";
my_sock.sin_family = AF_INET;
my_sock.sin_port = htons(port);
my_sock.sin_addr.s_addr = INADDR_ANY;
int err;
err = bind(s, (struct sockaddr*)&my_sock, sizeof my_sock);
if (err < 0)
{
perror("bind failed\n");
}
else
{
printf("bound socket\n");
}
}
//fout=new int(client.port);
ofstream fout;
void client_process::sender(string var1, int var2)
{
ssize_t bytes;
size_t len;
char msg_comm[1000];
strcpy(msg_comm, var1.c_str());
//len= msg[1].length();
//cout<<msg[1]<<endl;
next_sock.sin_family = AF_INET;
next_sock.sin_port = htons(var2);
bytes = sendto(s, msg_comm, 100, 0, (struct sockaddr*)&next_sock, sizeof next_sock);
//cerr<<"sent " << port<<" "<<msg_comm << endl;
}
/*
void client_process::receiver_old()
{
int rc;
int k=0;
struct timeval tv;
tv.tv_sec=2;
ssize_t bytes;
void *data;
size_t len;
char msg_recv[100];
socklen_t fromlen;
fromlen = sizeof(my_sock);
while(1)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(s, &fds);
//cout << "before select" << endl;
rc = select(s+1, &fds, NULL, NULL, NULL);
//cout << "after select" << endl;
if (rc < 0)
{
printf("error in select\n");
getchar();
}
else
{
int socket_data = 0;
if (FD_ISSET(s,&fds))
{
cout<< "from" << port<<" "<<msg_recv<<endl;
bytes = recvfrom(s, msg_recv, sizeof(msg), 0, (struct sockaddr*)&my_sock, &fromlen);
//cerr<<"yooo " << bytes<<endl;
socket_data = 1;
}
}
cout<<"broke"<<endl;
}
}
*/
string client_process::receiver()
{
int k=0;
struct timeval tv;
tv.tv_sec=2;
ssize_t bytes;
void *data;
size_t len;
char msg_recv[100];
socklen_t fromlen;
fromlen = sizeof(my_sock);
bytes = recvfrom(s, msg_recv, sizeof(msg), 0, (struct sockaddr*)&my_sock, &fromlen);
//fout<< "from " << port<<" "<<msg_recv<<endl;
//cerr<<"yooo " << bytes<<endl;
return msg_recv;
}