/
serverC.c
238 lines (181 loc) · 6.89 KB
/
serverC.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
// serverC.c
// demo
//
// Created by 世泰 陈 on 15/10/24.
// Copyright (c) 2015年 世泰 陈. All rights reserved.
//
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <netdb.h>
#include <stdlib.h>
#define UDP 23651
#define TCP 25651
struct router {
char name[20];
char cost[20];
};
/*
* function:send itself connection to the client
* The format is from Beej or Internet as http://blog.csdn.net/piaojun_pj/article/details/5920888
*/
void sendmessage()
{
int client_sockfd;
int len =1 ;
struct sockaddr_in remote_addr;//服务器端网络地址结构体
char buf[BUFSIZ];//数据传送的缓冲区
char **pptr;
char str[32];
struct hostent * hptr = gethostbyname("localhost");
// //show the IP address!!!
// switch(hptr->h_addrtype)
// {
// case AF_INET:
// case AF_INET6:
// pptr=hptr->h_addr_list;
// for(; *pptr!=NULL; pptr++)
// printf(" address:%s\n",
// inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
// printf(" first address: %s\n",
// inet_ntop(hptr->h_addrtype, hptr->h_addr, str, sizeof(str)));
// break;
// default:
// printf("unknown address type\n");
// break;
// }
memset(&remote_addr,0,sizeof(remote_addr)); //数据初始化--清零
pptr= hptr->h_addr_list;
remote_addr.sin_family=PF_INET; //设置为IP通信
//printf(" address:%s\n",inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
remote_addr.sin_addr.s_addr=inet_addr(inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));//服务器IP地址
int length = sizeof(remote_addr);
//getsockname(client_sockfd, (struct sockaddr *) &remote_addr, (socklen_t *) &length);
//printf("TCP prot number %d ",remote_addr.sin_port);
remote_addr.sin_port=htons(25651); //服务器端口号
printf("The Server C is up and running! \n");
struct router server;
FILE *fp;
int i = 0;
int name;
int cost;
fp = fopen("serverC.txt", "r");
if(fp == NULL)
{
printf("CANNOT OPEN \n");
exit(1);
}
//printf("读取了文件 \r\n");
char buffer[1024];
char buffer2[30];
char message[50];
char *ptr = NULL;
int sin_size=sizeof(struct sockaddr_in);
/*创建客户端套接字--IPv4协议,面向连接通信,TCP协议*/
if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
{
printf("socket fail ! \r\n");
//return -1;
}
//printf("socket ok !\r\n");
/*将套接字绑定到服务器的网络地址上*/
if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)
{
perror("connect");
//return 1;
}
// printf("connect ok !\r\n");
// printf("connected to server \r\n");
// 获取子socket的port!!!!!!!!
getsockname(client_sockfd, (struct sockaddr *) &remote_addr, (socklen_t *) &length);
//printf("TCP port number %d ",remote_addr.sin_port);
printf("Neighbor------Cost\n");
while(fgets(buffer, 1024, fp) != NULL)
{
printf("%s", buffer);
int len =send(client_sockfd,buffer,1024,0);
}
printf("\n");
printf("The Server C finishe sending its neighbor information to the Client with TCP port number: %d and IP address: %s \n", TCP, inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
printf("For this connection with Client, the Server C has TCP port number: %d and IP address: %s \n", remote_addr.sin_port,inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
close(client_sockfd);//关闭套接字
}
/*
* function:receive the topology of the whole internet
*The format is from Beej or Internet as http://blog.csdn.net/piaojun_pj/article/details/5920888
*/
void receiver()
{
int server_sockfd;
int len = 1;
struct sockaddr_in my_addr; //服务器网络地址结构体
struct sockaddr_in remote_addr; //客户端网络地址结构体
int sin_size;
//char buf[BUFSIZ]; //数据传送的缓冲区
char **pptr;
char str[32];
struct hostent * hptr = gethostbyname("localhost");
memset(&my_addr,0,sizeof(my_addr)); //数据初始化--清零
my_addr.sin_family=PF_INET; //设置为IP通信
pptr= hptr->h_addr_list;
my_addr.sin_addr.s_addr=inet_addr(inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));//服务器IP地址--允许连接到所有本地地址上
my_addr.sin_port=htons(UDP); //服务器端口号
//int buf[4];
int matrix[4][4];
int i = 0;
int j = 0;
/*创建服务器端套接字--IPv4协议,面向无连接通信,UDP协议*/
if((server_sockfd=socket(PF_INET,SOCK_DGRAM,0))<0)
{
printf("socket fail ! \r\n");
//return -1;
}
//printf("socket ok !\r\n");
/*将套接字绑定到服务器的网络地址上*/
if (bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)
{
printf("bind fail !\r\n");
//return -1;
}
//printf("bind ok !\r\n");
sin_size=sizeof(struct sockaddr_in);
//printf("waiting for a packet...\n");
/*接收客户端的数据并将其发送给客户端--recvfrom是无连接的*/
while(1)
{
char buf[BUFSIZ];
recvfrom(server_sockfd,buf,BUFSIZ,0,(struct sockaddr *)&remote_addr,&sin_size);
//if(len <= 0) break; 一开始就是len = 0 所以就跳出来了。所以要注释掉,但是为什么呢??????
matrix[i][j++] = atoi(buf);
if(j >= 4)
{
i++;
j = 0;
}
if(i >= 4) break;
}
printf("The Server C has received the network topology from the Client with UDP port number: %d and IP address: %s \n",TCP,inet_ntoa(remote_addr.sin_addr));
printf("\n");
printf("Edge------Cost\n");
if(matrix[0][1] != 0) printf("AB %d\n",matrix[0][1]);
if(matrix[0][2] != 0) printf("AC %d\n",matrix[0][2]);
if(matrix[0][3] != 0) printf("AD %d\n",matrix[0][3]);
if(matrix[1][2] != 0) printf("BC %d\n",matrix[1][2]);
if(matrix[1][3] != 0) printf("BD %d\n",matrix[1][3]);
if(matrix[2][3] != 0) printf("CD %d\n",matrix[2][3]);
printf("\n");
getsockname(server_sockfd, (struct sockaddr *) &remote_addr, (socklen_t *) &sin_size);
printf("For this connection with Client, The Server C has UDP port number: %d and IP address: %s\n", remote_addr.sin_port, inet_ntoa(remote_addr.sin_addr));
close(server_sockfd);
}
int main(int argc, const char * argv[]) {
// insert code here...
int matrix[4][4];
sendmessage();
receiver();
return 0;
}