目录
项目:基于UDP的网络聊天室
1.项目需求:
1).如果有用户登录,其他用户可以收到这个人的登录信息
2).如果有人发送信息,其他用户可以收到这个人的群聊信息
3).如果有人下线,其他用户可以收到这个人的下线信息
4).服务器可以发送系统信息
2.写项目的方法:
1).画流程图
2).根据流程图写框架
3).将每个功能实现(一 个个实现,不要一起写)
一、 服务器代码
创建一个链表,用于存储IP地址和端口号,链表自然得有一个指针指向下个链表,
然后建立线程同时进行数据收发,注意区别各自的功能
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define N 128
#define ERRMSG(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0)
int sfd; //套接字
//存储地址的链表结构体
typedef struct Node
{
struct sockaddr_in cin;
struct Node* next;
}linklist;
//接收信息
void* recvfrom_a(void* arg)
{
linklist* L = (linklist*)arg;
//存储接收到的地址信息
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
char buf[N];
int flag = 0;
linklist* q = NULL;
linklist* p = NULL;
while(1)
{
flag = 0;//默认不在线
//接收信息
bzero(buf, sizeof(buf));
recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen);
printf("%s\n", buf);
//判断是否在线
q = L;
while(q->next != NULL)//遍历链表
{
q = q->next;
if(cin.sin_port == q->cin.sin_port)//已在线不添加节点
{
flag = 1;
break;
}
}
if(flag == 0)//第一次登录,将地址信息添加到链表
{
linklist* p = (linklist*)malloc(sizeof(linklist));//申请节点
p->cin = cin; //记录地址信息
p->next = L->next;//头插
L->next = p;
int len = strlen(buf);
strcat(buf, "上线了");
}
//判断收到消息是否是quit
int len = strlen(buf) - 4;//分离消息(名字:消息)
if(strcasecmp(buf+len, "quit") == 0)
{
q = L;
while(q->next != NULL)//找到该节点的前一个节点
{
if(cin.sin_port == q->next->cin.sin_port)//找到退出循环
{
break;
}
q = q->next;
}
p = q->next; //删除的节点
q->next = p->next;
free(p);
p = NULL;
strcpy(buf+len, "下线了");//将quit用下线了替换在转发给其他在线用户
}
//转发信息
q =L;
while(q->next != NULL)
{
q = q->next;
if(q->cin.sin_port != cin.sin_port)//将消息群发
{
sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&q->cin, sizeof(q->cin));
}
}
}
pthread_exit(NULL);
}
//服务器发送系统信息
void* send_a(void* arg)
{
char buf[128]="[系统]";//系统名字
int len = strlen(buf);
linklist* L = (linklist*)arg;
linklist *q = NULL;
while(1)
{
bzero(buf+len, sizeof(buf));//清空消息
fgets(buf+len,sizeof(buf),stdin);//输入信息
buf[strlen(buf)-1] = '\0';
//发送系统消息
q = L;
while(q->next != NULL)
{
q = q->next;
sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&q->cin, sizeof(*q));
}
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建报式套接字
if(-1 == (sfd = socket(AF_INET, SOCK_DGRAM, 0)))
{
ERRMSG("socket");
return -1;
}
//初始化服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.s_addr = inet_addr("192.168.31.191");
//绑定地址信息
if(-1 == bind(sfd, (struct sockaddr*)&sin, sizeof(sin)))
{
ERRMSG("bind");
return -1;
}
//创建链表
linklist *L;
L = (linklist*)malloc(sizeof(linklist));
L->cin = sin;
L->next = NULL;
//创建两个线程一个发送信息一个接收信息
pthread_t tid1, tid2;
if(-1 == pthread_create(&tid1, NULL, send_a, (void*)L))
{
ERRMSG("pthread_create");
return-1;
}
if(-1 == pthread_create(&tid2, NULL, recvfrom_a, (void*)L))
{
ERRMSG("pthread_create");
return -1;
}
printf("服务器准备就绪\n");
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
L = NULL;
close(sfd);
return 0;
}
二、客户端代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define ERRMSG(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0)
int sfd; //套接字
//发送信息
void* send_b(void* arg)
{
struct sockaddr_in sin = *(struct sockaddr_in*)arg;
char buf[128]="";
printf("请输入你的网名:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
//发送网名
buf[strlen(buf)] = ':';//使发送的消息(网名:消息)
int len = strlen(buf);
strcat(buf,"已上线");
sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin));
printf("登录成功\n");
while(1)
{
bzero(buf+len, sizeof(buf));//清空(网名:)后的字符串
fgets(buf+len,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
//发送信息
sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin));
if(0 == strcasecmp(buf+len, "quit"))//退出进程条件
{
exit(0);
}
}
pthread_exit(NULL);
}
//接收信息
void* recvfrom_b(void* arg)
{
//存储地址信息
struct sockaddr_in sin = *(struct sockaddr_in*)arg;
socklen_t addrlen = sizeof(sin);
char buf[128];
while(1)
{
bzero(buf, sizeof(buf));
//接收信息
recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
printf("%s\n", buf);//打印
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
//创建报式套接字
if(-1 == (sfd = socket(AF_INET, SOCK_DGRAM, 0)))
{
ERRMSG("socket");
return -1;
}
//初始化服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.s_addr = inet_addr("192.168.31.191");
socklen_t addrlen = sizeof(sin);
//创建两个线程一个发送信息一个接收信息
pthread_t tid1, tid2;
if(-1 == pthread_create(&tid1, NULL, send_b, (void*)&sin))
{
ERRMSG("pthread_create");
return -1;
}
if(-1 == pthread_create(&tid2, NULL, recvfrom_b, (void*)&sin))
{
ERRMSG("pthread_create");
return -1;
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
close(sfd);
return 0;
}
三、执行结果