-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.cc
More file actions
64 lines (55 loc) · 1.28 KB
/
context.cc
File metadata and controls
64 lines (55 loc) · 1.28 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
#include "msg.pb.h"
#include "doevent.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#define ERR_EXIT(info)\
do\
{\
perror(info);\
exit(EXIT_FAILURE);\
} while(0)
//deal with connect in client
extern void do_client(int listenfd);
int main(void)
{
int count = 0;
for (;;)
{
//socket
int listenfd = socket(AF_INET,SOCK_STREAM,0);
if(listenfd == -1)
{
sleep(5); // make server enough time to deal with close
ERR_EXIT("socket");
}
//the server's address and port
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5588);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//connect
if (connect(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) == -1)
{
ERR_EXIT("connect");
}
struct sockaddr_in localaddr;
socklen_t addrlen = sizeof(localaddr);
if (getsockname(listenfd, (struct sockaddr*)&localaddr, &addrlen) < 0)
{
ERR_EXIT("getsockname");
}
printf("ip=%s port=%d\n",inet_ntoa(localaddr.sin_addr),ntohs(localaddr.sin_port));
printf("count=%d\n", ++count);
}
//do_client(listenfd);
return 0;
}