-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser2.c
More file actions
71 lines (54 loc) · 1.43 KB
/
user2.c
File metadata and controls
71 lines (54 loc) · 1.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
#include "msocket.h"
int main()
{
int s = m_socket(AF_INET, SOCK_MTP, 0);
if (s < 0)
{
perror("socket error\n");
return -1;
}
printf("socket created\n");
printf("socket id = %d\n", s);
long s_ip = inet_addr("127.0.0.1");
int s_port = htons(1237);
long d_ip = inet_addr("127.0.0.1");
int d_port = htons(1236);
int ret = m_bind(s, s_ip, s_port, d_ip, d_port);
if (ret < 0)
{
perror("bind error\n");
return -1;
}
printf("bind successful with s_ip = %ld, s_port = %d\n", s_ip, s_port);
char buf[1024];
int fd = open("juliet.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
while (1)
{
usleep(100000);
ret = m_recvfrom(s, buf, 1024, 0, d_ip, d_port);
while (ret < 0)
{
perror("recvfrom error");
usleep(100000);
ret = m_recvfrom(s, buf, 1024, 0, d_ip, d_port);
}
// printf("message received\n");
// printf("message = %s\n", buf);
// check if the message is terminal message
if (strcmp(buf, "##########") == 0)
{
break;
}
write(fd, buf, strlen(buf));
printf("message received\n");
memset(buf, '\0', sizeof(buf));
}
// ret = m_close(s);
// if (ret < 0)
// {
// perror("close error\n");
// return -1;
// }
// printf("socket closed\n");
return 0;
}