#pragma pack(1)
37
38 typedef struct qqq
39 {
40 char type;
41 char name[10];
42 char s[128];
43 }qqq;
44
45
46 typedef struct Node
47 {
48 union
49 {
50 int len;
51 struct sockaddr_in cin;
52 };
53 struct Node* next;
54 }Node;
55
56 typedef struct
57 {
58 struct sockaddr_in cin;
59 Node* head;
60 int sfd;
61 // char s[128];
62 pthread_t tid;
63 }info;
64
65
66 Node* list_create()
67 {
68 Node* p=malloc(sizeof(Node));
69 bzero(p,sizeof(Node));
70 p->len=0;
71 p->next=NULL;
72 return p;
73 }
74
75 Node* create_node(struct sockaddr_in cin)
76 {
77 Node* p=malloc(sizeof(Node));
78 bzero(p,sizeof(Node));
79 p->cin=cin;
80 p->next=NULL;
81 return p;
82 }
83
84 void insert_node(Node* p,Node* head)
85 {
86 Node* q=head;
87 while(q->next!=NULL)
88 {
89 q=q->next;
90 }
91 q->next=p;
92 p->next=NULL;
93 head->len++;
94 }
95
96 int find_node_pos(struct sockaddr_in cin,Node* head)
97 {
98 Node* q=head;
99 int pos=0;
100 while(q!=NULL)
101 {
102 if((q->cin.sin_port==cin.sin_port)&&\
103 (q->cin.sin_addr.s_addr==cin.sin_addr.s_addr))
104 {
105 return pos;
106 }
107 else
108 {
109 pos++;
110 q=q->next;
111 }
112 }
113 return -1;
114 }
115
116 void delete_node(int pos,Node* head)
117 {
118 Node* q=head;
119 for(int i=1;i<=pos-1;i++)
120 {
121 q=q->next;
122 }
123
124 Node* t=q->next;
125 q->next=t->next;
126 head->len--;
127 free(t);
128 }
130
131
132 void* funa(void* b)
133 {
134 struct sockaddr_in cin=((info*)b)->cin;
135 int sfd=((info*)b)->sfd;
136 char s[128]="";
137 // strcpy(s,((info*)b)->s);
138 Node* head=((info*)b)->head;
139 Node* q=head->next;
140 pthread_detach(pthread_self());
141 // int pos=find_node_pos(cin,head);
142
143
144
145 }
146
147 void* funb(void* b)
148 {
149 Node* head=((info*)b)->head;
150 struct sockaddr_in cin=((info*)b)->cin;
151 int sfd=((info*)b)->sfd;
152 char p[128]="";
153 char s[128]="";
154 pthread_detach(pthread_self());
155
156 while(1)
157 {
158 bzero(s,sizeof(s));
159 bzero(p,sizeof(p));
160 fgets(p,sizeof(p),stdin);
161 p[strlen(p)-1]='\0';
162
163 strcpy(s,"服务器");
164 strcat(s,p);
165
166 Node* q=head->next;
167 struct sockaddr_in qin;
168
169 for(int i=1;i<=head->len;i++)
170 {
171
172 qin=q->cin;
173 if((qin.sin_addr.s_addr==cin.sin_addr.s_addr)&&\
174 qin.sin_port==cin.sin_port)
175 {
176 q=q->next;
177 continue;
178 }
179 sendto(sfd,s,strlen(s),0,(struct sockaddr*)&qin,sizeof(qin));
180 q=q->next;
181 }
182 }
183
184 }
185
186 int main(int argc, const char *argv[])
187 {
188 int sfd=socket(AF_INET,SOCK_DGRAM,0);
189
190 struct sockaddr_in sin,cin;
191 sin.sin_family=AF_INET;
192 sin.sin_port=htons(PORT_C);
193 sin.sin_addr.s_addr=inet_addr(IP_C);
194
195
196 int reuse = 1;
197 setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
198
199 Node* head=list_create();
200
201 bind(sfd,(struct sockaddr*)&sin,sizeof(sin));
202
203 // listen(sfd,128);
204
205 qqq aaa;
206
207 int newfd=-1;
208 ssize_t res=0;
209 char s[128]="";
210 socklen_t addrlen=sizeof(cin);
211 pthread_t tid;
212
213 info msg;
214 msg.cin=cin;
215 msg.sfd=sfd;
216 msg.head=head;
217 pthread_create(&tid,NULL,funb,&msg);
218
219 while(1)
220 {
221 // newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
222 bzero(&aaa,sizeof(aaa));
223 res=recvfrom(sfd,&aaa,sizeof(aaa),0,(struct sockaddr*)&cin,&addrlen);
224 // printf("aaaa\n");
225 int flag=find_node_pos(cin,head);
226 if(flag==-1)
227 {
228
229 // strcpy(msg.s,s);
230
231 Node* p=create_node(cin);
232 insert_node(p,head);
233
234 // pthread_create(&tid,NULL,funa,&msg);
235 // msg.tid=tid;
236
237 Node* q=head->next;
238 struct sockaddr_in qin;
239
240 bzero(s,sizeof(s));
241 if(aaa.type==1)
242 {
243 strcpy(s,aaa.name);
244 strcat(s,":上线");
245 }
246 else if(aaa.type==3)
247 {
248 strcpy(s,aaa.name);
249 strcat(s,":下线");
250 }
251 else
252 {
253 strcpy(s,aaa.name);
254 strcat(s,":");
255 strcat(s,aaa.s);
256 }
257
258
259 for(int i=1;i<=head->len;i++)
260 {
261
262 qin=q->cin;
263 if((qin.sin_addr.s_addr==cin.sin_addr.s_addr)&&\
264 qin.sin_port==cin.sin_port)
265 {
266
267 q=q->next;
268 continue;
269 }
270 sendto(sfd,s,strlen(s),0,(struct sockaddr*)&qin,sizeof(qin));
271 q=q->next;
272 }
273
274 }
275 else if(flag>0)
276 {
277
278 Node* q=head->next;
279 struct sockaddr_in qin;
280
281 bzero(s,sizeof(s));
282
283 if(aaa.type==1)
284 {
285 strcpy(s,aaa.name);
286 strcat(s,":上线");
287 }
288 else if(aaa.type==3)
289 {
290 strcpy(s,aaa.name);
291 strcat(s,":下线");
292 }
293 else
294 {
295 strcpy(s,aaa.name);
296 strcat(s,":");
297 strcat(s,aaa.s);
298 }
299
300
301 for(int i=1;i<=head->len;i++)
302 {
303
304 qin=q->cin;
305 if((qin.sin_addr.s_addr==cin.sin_addr.s_addr)&&\
306 qin.sin_port==cin.sin_port)
307 {
308 q=q->next;
309 continue;
310 }
311 sendto(sfd,s,strlen(s),0,(struct sockaddr*)&qin,sizeof(qin));
312 q=q->next;
313 }
314
315 }
316
317
318 printf("%s:%d type:%d name:%s s:%s 123:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),\
319 aaa.type,aaa.name,aaa.s,s);
320