服务器
#include <myhead.h>
#define ERR_MSG(msg){\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0);
#define IP "192.168.1.110"
#define PORT 25565
struct mesg{
char flag;
char name[20];
char txt[128];
};
//定义存储客户端IP与端口的链表
typedef struct sockaddr_in datatype;
typedef struct Node{
union{
datatype data;
int len;
};
struct Node *next;
}linklist;
linklist *link_create();
linklist *get_node(datatype e);
int link_add(linklist *L,datatype e);
int list_find(linklist *L,datatype e);
int link_dele(linklist *L,datatype e);
linklist *L;
void *ser_rcv(void *arg)
{
pthread_detach(pthread_self());
struct mesg msg;
int sfd = *(int *)arg;
ssize_t res = 0;
struct sockaddr_in sin;
socklen_t addrlen = sizeof(sin);
while(1)
{
bzero(msg.txt,sizeof(msg.txt));
bzero(msg.name,sizeof(msg.name));
res = recvfrom(sfd,&msg,sizeof(msg),0,(struct sockaddr*)&sin,&addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
break;
}
linklist *p = L;
switch(msg.flag)
{
case 'd':
strcpy(msg.txt,"client online!");
for(int i = 1;i <= L->len;i++){
p = p->next;
if(sendto(sfd,&msg,sizeof(msg),0,(struct sockaddr*)&p->data,sizeof(p->data)) < 0){
ERR_MSG("sendto");
return NULL;
}
}
printf("serve sendto [%s] load message success\n",msg.name);
printf("[%s %d]%s %s\n",inet_ntoa(sin.sin_addr),\
ntohs(sin.sin_port),msg.name,msg.txt);
link_add(L,sin);
break;
case 'c':
for(int i = 1;i <= L->len;i++){
p = p->next;
if((p->data.sin_port == sin.sin_port) &&(p->data.sin_addr.s_addr == sin.sin_addr.s_addr))
continue;
if(sendto(sfd,&msg,sizeof(msg),0,(struct sockaddr*)&p->data,sizeof(p->data)) < 0){
ERR_MSG("sendto");
return NULL;
}
}
printf("serve send [%s] chat message success\n",msg.name);
break;
case 'q':
link_dele(L,sin);
strcpy(msg.txt,"client offline!");
for(int i = 1;i <= L->len;i++){
p = p->next;
if(sendto(sfd,&msg,sizeof(msg),0,(struct sockaddr*)&p->data,sizeof(p->data)) < 0){
ERR_MSG("sendto");
return NULL;
}
}
printf("serve sendto [%s] quit message success\n",msg.name);
break;
}
}
}
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
printf("serve socket success\n");
//填写信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
socklen_t addrlen = sizeof(sin);
//绑定服务器端口与IP
if(bind(sfd,(struct sockaddr*)&sin,addrlen) < 0){
ERR_MSG("bind");
return -1;
}
printf("sreve bind success\n");
pthread_t tid_rcv;
L = link_create();
if(NULL == L){
printf("链表创建失败\n");
return -1;
}
if(pthread_create(&tid_rcv,NULL,ser_rcv,&sfd) != 0){
fprintf(stderr,"pthread_create ser_rcv failed!\n");
return -1;
}
//主线程发送给所有客户端的信息
struct mesg msg;
bzero(msg.name,sizeof(msg.name));
strcpy(msg.name,"system");
while(1){
bzero(msg.txt,sizeof(msg.txt));
fgets(msg.txt,sizeof(msg.txt),stdin);
msg.txt[strlen(msg.txt) - 1] = 0;
linklist *p = L;
for(int i = 1;i <= L->len;i++){
p = p->next;
if(sendto(sfd,&msg,sizeof(msg),0,(struct sockaddr*)&p->data,sizeof(p->data)) < 0){
ERR_MSG("sendto");
return -1;
}
}
printf("serve sendto success\n");
}
return 0;
}
linklist *link_create()
{
linklist *L = (linklist *)malloc(sizeof(linklist));
if(NULL == L){
printf("链表创建失败\n");
return NULL;
}
L->len = 0;
L->next = NULL;
return L;
}
linklist *get_node(datatype e)
{
linklist *p = (linklist*)malloc(sizeof(linklist));
if(NULL == p){
printf("创建节点失败\n");
return NULL;
}
p->data = e;
p->next = NULL;
return p;
}
int link_add(linklist *L,datatype e)
{
if(NULL == L){
printf("链表不合法\n");
return 0;
}
linklist *p = get_node(e);
p->next = L->next;
L->next = p;
L->len++;
return 0;
}
//按值进行查找,查找对应数据在链表的位置
int link_find(linklist *L,datatype e)
{
if(NULL == L){
printf("链表不合法\n");
return -1;
}
linklist *p = L->next;
for(int i = 1;i <= L->len;i++){
if((p->data.sin_port == e.sin_port) &&(p->data.sin_addr.s_addr == e.sin_addr.s_addr))
return i;
p = p->next;
}
return 0;
}
//按值进行删除
int link_dele(linklist *L,datatype e)
{
if(NULL == L){
printf("链表不合法\n");
return -1;
}
int pos = link_find(L,e);
linklist *p = L;
for(int i = 0;i < pos-1;i++){
p = p->next;
}
linklist *q = p->next;
p->next = q->next;
free(q);
L->len--;
return 0;
}
客户端
#include <myhead.h>
#define ERR_MSG(msg){\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0);
#define IP "192.168.1.110"
#define PORT 25565
struct mesg{
char flag;
char name[20];
char txt[128];
};
struct mesg msg_rcv;
struct mesg msg;
pthread_t tid_rcv,tid_snd;//线程接收,发送
void *cli_snd(void *arg)
{
int sfd = *(int*)arg;
//填写信息结构体
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(PORT);
cin.sin_addr.s_addr = inet_addr(IP);
socklen_t addrlen = sizeof(cin);
bzero(msg.txt,sizeof(msg.txt));
printf("please enter your name");
fgets(msg.name,sizeof(msg.name),stdin);
msg.name[strlen(msg.name) - 1] = 0;
msg.flag = 'd';
if(sendto(sfd,&msg,sizeof(msg),0,(struct sockaddr*)&cin,addrlen) < 0){
ERR_MSG("sendto");
exit(0);
}
printf("login message send success\n");
while(1){
bzero(msg.txt,sizeof(msg.txt));
fgets(msg.txt,sizeof(msg.txt),stdin);
msg.txt[strlen(msg.txt) - 1] = 0;
//判断是否是退出信息
if(strcmp(msg.txt,"quit") == 0)
msg.flag = 'q';
else
msg.flag = 'c';
if(sendto(sfd,&msg,sizeof(msg),0,(struct sockaddr*)&cin,addrlen) < 0){
ERR_MSG("sendto");
break;
}
printf("message send success\n");
if(msg.flag == 'q'){
printf("%s退出聊天\n",msg.name);
break;
}
}
pthread_cancel(tid_rcv);
pthread_exit(NULL);
}
void *cli_rcv(void *arg)
{
int sfd = *(int *)arg;
ssize_t res = 0;
while(1){
res = recvfrom(sfd,&msg_rcv,sizeof(msg_rcv),0,NULL,NULL);/
if(res < 0){
ERR_MSG("recvfrom");
break;
}
if(strcmp(msg.name,msg_rcv.txt) == 0){
printf("offline by kick\n");
break;
}
printf("cli [%s] recvfrom success\n",msg.name);
printf("%s>>%s\n",msg_rcv.name,msg_rcv.txt);
}
pthread_cancel(tid_snd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0){
ERR_MSG("socket");
return -1;
}
if(pthread_create(&tid_rcv,NULL,cli_rcv,&sfd) != 0){
fprintf(stderr,"pthread_create cli_rcv failed!\n");
return -1;
}
if(pthread_create(&tid_snd,NULL,cli_snd,&sfd) != 0){
fprintf(stderr,"pthread_create cli_snd failed!\n");
return -1;
}
pthread_join(tid_rcv,NULL);
pthread_join(tid_snd,NULL);
return 0;
}