资源描述:
《用udp 实现局域网聊天程序源码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include#include#include#include#include#include#defineCLIENT_LOGIN100#defineCLIENT_CHAT200#defineCLIENT_QUIT300#defineSERVER_CHAT400#defineSERVER_QUIT500structnode{charname[20];
2、structsockaddr_inclient_addr;structnode*next;};structmessage{longtype;charname[20];charmtext[512];};structnode*create_list(void);voidinsert_list(structnode*,char*,structsockaddr_in*);voiddelete_list(structnode*,char*);voidrecv_message(int,structnode*);voidsend_message(int,structsock
3、addr_in*,pid_t);voidclient_login(int,structnode*,structmessage*,structsockaddr_in*);voidclient_chat(int,structnode*,structmessage*);voidclient_quit(int,structnode*,structmessage*);voidserver_chat(int,structnode*,structmessage*);voidserver_quit(int,structnode*,structmessage*);voidbro
4、cast_msg(int,structnode*,structmessage*);voidfather_func(intsig_no){return;}intmain(intargc,constchar*argv[]){intsocket_fd;pid_tpid;structsockaddr_inserver_addr;structnode*head;if(argc<3){fprintf(stderr,"usages:%sipport",argv[0]);exit(-1);}if((socket_fd=socket(AF_INET,SOCK_DGRAM,0
5、))<0){perror("failedtocreatesocket");exit(-1);}head=create_list();server_addr.sin_family=AF_INET;server_addr.sin_port=htons(atoi(argv[2]));server_addr.sin_addr.s_addr=inet_addr(argv[1]);if(bind(socket_fd,(structsockaddr*)&server_addr,sizeof(server_addr))<0){perror("failedtobind");ex
6、it(-1);}if((pid=fork())<0)//创建子经常{perror("failedtoforkpid");exit(-1);}if(pid==0)recv_message(socket_fd,head);elsesend_message(socket_fd,&server_addr,pid);return0;}structnode*create_list(void){structnode*head;head=(structnode*)malloc(sizeof(structnode));head->next=NULL;returnhead;}vo
7、idinsert_list(structnode*head,char*name,structsockaddr_in*client_addr){structnode*new;new=(structnode*)malloc(sizeof(structnode));strcpy(new->name,name);new->client_addr=*client_addr;new->next=head->next;head->next=new;return;}voiddelete_list(structnode*head,char*name){structnode*p=
8、head->next;structno