网络聊天室
服务器:
1 #include <myhead.h>
2 int main(int argc, const char *argv[])
3 {
4 if(argc!=3)
5 {
6 printf("请输入IP和端口号\n");
7 return -1;
8 }
9 int sfd = socket(AF_INET, SOCK_DGRAM, 0);
10 if(sfd == -1)
11 {
12 perror("socket error");
13 return -1;
14 }
15
16 int reuse = 1;
17 if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
18 {
19 perror("setsockopt error");
20 return -1;
21 }
22
23 struct sockaddr_in sin;
24 sin.sin_family = AF_INET;
25 sin.sin_port = htons(atoi(argv[2]));
26 sin.sin_addr.s_addr = inet_addr(argv[1]);
27
28 //2.2 绑定
29 if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) ==-1)
30 {
31 perror("bind error");
32 return -1;
33 }
34 printf("bind success\n");
35
36 struct pollfd fall[2];
37 fall[0].fd=0;
38 fall[0].events=POLLIN;
39 fall[1].fd=sfd;
40 fall[1].events=POLLIN;
41
42 //地址组
43 struct node1
44 {
45 char type;
46 char name[128];
47 struct sockaddr_in sin;
48 };
49 struct node1 node[1024];
50 for(int j=0;j<1024;j++)
51 {
52 node[j].type='0';
53 }
54 struct sockaddr_in cin;
55 socklen_t addrlen = sizeof(cin);
56
57 //数据包
58 struct rbuf1
59 {
60 char type;
61 char name[128];
62 char data[1024];
63 };
64 struct rbuf1 rbuf;
65 while(1)
66 {
67 int res=poll(fall,2,-1);
68 if(res==-1)
69 {
70 perror("poll");
71 return -1;
72 }
73 else if(res==0)
74 {
75 printf("timeout\n");
76 return -1;
77 }
78
79 if(fall[0].revents==POLLIN)
80 {
81 bzero(&rbuf,sizeof(rbuf));
82 fgets(rbuf.data,sizeof(rbuf.data),stdin);
83 rbuf.data[strlen(rbuf.data)-1]='\0';
84 char fun[1024]="**system**:";
85
86 strcat(fun,rbuf.data);
87 strcpy(rbuf.data,fun);
88 for(int j=0;j<1024;j++)
89 {
90 sendto(sfd,&rbuf,sizeof(rbuf),0,(struct sockaddr*)&node[j].sin,addrlen);
91 }
92 printf("**system** [%s:%d]chat成功\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
93 }
94
95 else if(fall[1].revents==POLLIN)
96 {
97 bzero(&rbuf,sizeof(rbuf));
98 recvfrom(sfd,&rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,&addrlen);
99 if(rbuf.type=='0')
100 {
101 strcpy(rbuf.data,"请输入姓名>>>");
102 rbuf.type='1';
103 sendto(sfd,&rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,addrlen);
104 }
105
106 else if(rbuf.type=='1')
107 {
108 for(int j=0;j<1024;j++)
109 {
110 if(strcmp(node[j].name,rbuf.name)==0)
111 {
112 node[j].type='0';
113 }
114 }
115 for(int j=0;j<1024;j++)
116 {
117 if(node[j].type=='0')
118 {
119 node[j].type='1';
120 strcpy(node[j].name,rbuf.name);
121 node[j].sin=cin;
122 break;
123 }
124 }
125 printf("%s [%s:%d]登录成功\n",rbuf.name,inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
126 char fun[128]="-----";
127 strcat(fun,rbuf.data);
128 strcpy(rbuf.data,fun);
129 strcat(rbuf.data,"已登录-----");
130
131 for(int j=0;j<1024;j++)
132 {
133 if(node[j].type=='1')
134 {
135 if(strcmp(node[j].name,rbuf.name)!=0)
136 sendto(sfd,&rbuf,sizeof(rbuf),0,(struct sockaddr*)&node[j].sin,addrlen);
137 }
138 }
139 }
140
141 else if(rbuf.type=='2')
142 {
143 char run[1024]="";
144 strcat(run,rbuf.name);
145 strcat(run,":");
146 strcat(run,rbuf.data);
147 strcpy(rbuf.data,run);
148 strcat(run,":");
149 for(int j=0;j<1024;j++)
150 {
151 if(node[j].type=='1')
152 {
153 if(strcmp(node[j].name,rbuf.name)!=0)
154 sendto(sfd,&rbuf,sizeof(rbuf),0,(struct sockaddr*)&node[j].sin,addrlen);
155 }
156 }
157 printf("%s [%s:%d]chat成功\n",rbuf.name,inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
158 }
159 }
160 }
161 close(sfd);
162 return 0;
163 }
164
客户端:
28
29 //收数据包
30 struct rbuf1
31 {
32 char type;
33 char name[128];
34 char data[1024];
35 };
36 struct rbuf1 buf;
37 buf.type='0';
38 strcpy(buf.data,"");
39 strcpy(buf.name,"");
40
41 //发数据包
42 struct rbuf1 rbuf;
43 rbuf.type='0';
44 strcpy(rbuf.data,"");
45 strcpy(rbuf.name,"");
46
47 sendto(sfd,&rbuf,sizeof(rbuf),0,(struct sockaddr*)&sin,addrlen);
48
49 while(1)
50 {
51 int fel=poll(fall,2,-1);
52 if(fel==-1)
53 {
54 perror("poll");
55 return -1;
56 }
57 else if(fel==0)
58 {
59 printf("timeout");
60 return -1;
61 }
62
63 if(fall[0].revents==POLLIN)
64 {
65 if(rbuf.type=='1')
66 {
67 rbuf.type='2';
68 bzero(rbuf.data,sizeof(rbuf.data));
69 fgets(rbuf.data,sizeof(rbuf.data),stdin);
70 rbuf.data[strlen(rbuf.data)-1]='\0';
71 sendto(sfd,&rbuf,sizeof(rbuf),0,(struct sockaddr*)&sin,addrlen);
72 rbuf.type='1';
73 }
74 }
75
76 else if(fall[1].revents==POLLIN)
77 {
78 if(rbuf.type=='0')
79 {
80 bzero(&buf,sizeof(buf));
81 bzero(rbuf.data,sizeof(rbuf.data));
82 recvfrom(sfd,&buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
83 printf("%s",buf.data);
84 fgets(rbuf.data,sizeof(rbuf.data),stdin);
85 rbuf.data[strlen(rbuf.data)-1]='\0';
86 strcpy(rbuf.name,rbuf.data);
87 rbuf.type='1';
88 sendto(sfd,&rbuf,sizeof(rbuf),0,(struct sockaddr*)&sin,addrlen);
89
90 }
91 else if(rbuf.type=='1')
92 {
93 bzero(&buf,sizeof(buf));
94 recvfrom(sfd,&buf,sizeof(rbuf),0,(struct sockaddr*)&sin,&addrlen);
95 printf("%s\n",buf.data);
96 }
97 }
98 }
99 close(sfd);
100 return 0;
101 }
~