




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、/* * server.c * * Created on: 2012-6-15 * Author: root */#include #include #include #include #include #include #include #include #include#define PORT 7999#define MAX_NUM 3 /client連接最大個(gè)數(shù)#define MAX_CLIENT 15#define MAX_SIZE 1024pthread_rwlock_t idx_lock, wait_lock;/client 信息typedef struct _client int
2、 sockfd;char name20;pthread_t pid;int flg; c_client;c_client clientMAX_CLIENT;/定義client;/等待的clientstruct _client_ int sockfd;char name20;pthread_t pid;struct _client_ *next;typedef struct _client_ c_client_c;c_client_c *head = NULL;c_client_c *temp_c1 = NULL, *temp_c2 = NULL;/等待的/初始化client信息void ini
3、t_client() int i = 0;for (i = 0; i MAX_CLIENT; i+) clienti.sockfd = -1;memset(, 0, 20);clienti.pid = -1;clienti.flg = -1;/查找結(jié)構(gòu)體數(shù)組中sockfd為-1的下標(biāo)值int find_fd(c_client *client) int i = 0;while (i sockfd & i MAX_NUM) if (strcmp(, p) = 0)return 0;i+;return 1;/SHOWvoid showuser(c_cl
4、ient *p_client) int i = 0;char buf1024 = 0 ;strcpy(buf, 200rn);for (i = 0; i sockfd, buf, strlen(buf), 0);/ALLvoid sendto_all(c_client *p_client, char *buf) int i = 0;char sendbuf1024 = 0 ;sprintf(sendbuf, AFROMrn%srn%s, p_client-name, buf + 5);for (i = 0; i MAX_NUM; i+) if (clienti.sockfd != -1 & c
5、lienti.flg != -1)if(send(clienti.sockfd, sendbuf, strlen(sendbuf), 0) = 0)printf(send errrrrrn);exit(1);int findname(char *name) int i = 0;for (i = 0; i sockfd, sendbuf, strlen(sendbuf), 0); else sprintf(sendbuf, FROMrn%srn%s, p_client-name, buf + 4 + strlen(name) + 2);if(send(sock, sendbuf, strlen(
6、sendbuf), 0)name);close(p_client-sockfd);p_client-sockfd = -1;p_client-pid = -1;p_client-flg = -1;sprintf(buf,NOTICE1rn%s退出聊天室rnrn,p_client-name);memset(p_client-name,0,20);for(i=0;inext != NULL)memset(buf,0,1024);pthread_rwlock_rdlock(&idx_lock);idx = find_fd(client);pthread_rwlock_unlock(&idx_lock
7、);clientidx.sockfd = head-next-sockfd;pthread_rwlock_wrlock(&wait_lock);temp = head-next;head-next = head-next-next;free(temp);pthread_rwlock_unlock(&wait_lock);sprintf(buf,NOTICErn您已被喚醒,請(qǐng)繼續(xù)操作rnrn);send(clientidx.sockfd,buf,strlen(buf),0);if (pthread_create(&clientidx.pid, NULL, (void *)pthread_fun,
8、(void *) &clientidx) != 0) perror(pthread_create);exit(1);pthread_detach(clientidx.pid);void pthread_fun(void* cclient) c_client *p_client = (c_client *) cclient;char bufMAX_SIZE = 0 ;char sendbuf1024 = 0 ;int i, n;char *p;sprintf(sendbuf, %s, NOTICErn通訊通道開(kāi)啟rnrn);if (send(p_client-sockfd, sendbuf, s
9、trlen(sendbuf), 0) sockfd, buf, sizeof(buf) - 1, MSG_NOSIGNAL);if (n sockfd);p_client-sockfd = -1;break;if (logform(buf) if (cmpname(buf, p_client) = 0) send(p_client-sockfd, ERRORrn用戶(hù)名重復(fù)rnrn, 26, 0);continue; else p_client-flg = 1;p = strtok(buf + 7, rnrn);strcpy(p_client-name, p);sprintf(sendbuf,
10、100rn%srnrn, p_client-name);send(p_client-sockfd, sendbuf, sizeof(sendbuf), 0);printf(%s進(jìn)入聊天室n, p_client-name);for (i = 0; i sockfd & clienti.flg != -1)send(clienti.sockfd, sendbuf, sizeof(sendbuf), 0);memset(sendbuf, 0, 1024);while (1) memset(buf, 0, MAX_SIZE);if (n = recv(p_client-sockfd, buf, MAX
11、_SIZE, 0) sockfd, ERRORrn信息不符合協(xié)議要求rnrn,38, 0); else send(p_client-sockfd, ERRORrn未登錄,請(qǐng)您登錄再進(jìn)行其他操作rnrn, 56, 0);pthread_exit(NULL);int main() int ser_sockfd, clt_sockfd;struct sockaddr_in addr;int idx;char buf1024 = 0 ;/pthread_rwlock_t idx_lock,wait_lock;pthread_rwlock_init(&idx_lock, NULL);pthread_rw
12、lock_init(&wait_lock, NULL);init_client();/創(chuàng)建服務(wù)器sockfdif (ser_sockfd = socket(AF_INET, SOCK_STREAM, 0) = -1) perror(socket);exit(1);/設(shè)置服務(wù)器網(wǎng)絡(luò)地址bzero(&addr, sizeof(addr);addr.sin_family = AF_INET;addr.sin_port = htons(PORT);addr.sin_addr.s_addr = htonl(INADDR_ANY);/設(shè)置端口可重用int opt = 1;setsockopt(ser_so
13、ckfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt);/將套接字綁定到服務(wù)器的網(wǎng)絡(luò)地址上if (bind(ser_sockfd, (struct sockaddr*)&addr, sizeof(addr) = -1) perror(bind);exit(1);printf(bind successn);/監(jiān)聽(tīng)連接請(qǐng)求-監(jiān)聽(tīng)隊(duì)列長(zhǎng)度為10if (listen(ser_sockfd, 10) = -1) perror(listen);exit(1);printf(listen successn);while (1) if (clt_sockfd = a
14、ccept(ser_sockfd, NULL, NULL) = -1) perror(accept);exit(1);pthread_rwlock_rdlock(&idx_lock);idx = find_fd(client);/printf(idx=%dn,idx);pthread_rwlock_unlock(&idx_lock);if (idx != -1) /連接末滿(mǎn)clientidx.sockfd = clt_sockfd;if (pthread_create(&clientidx.pid, NULL,(void *) pthread_fun,(void *)&clientidx) !
15、= 0) perror(pthread_create);exit(1);pthread_detach(clientidx.pid); else /連接已滿(mǎn),等待temp_c1 = (c_client_c *) malloc(sizeof(c_client_c);temp_c1-sockfd = clt_sockfd;temp_c1-next = NULL;pthread_rwlock_wrlock(&wait_lock);if (head = NULL) head = (c_client_c *) malloc(sizeof(c_client_c);head-next = temp_c1; e
16、lse for (temp_c2 = head; temp_c2-next != NULL; temp_c2= temp_c2-next);temp_c2-next = temp_c1;pthread_rwlock_unlock(&wait_lock);memset(buf, 0, 1024);sprintf(buf, NOTICErn服務(wù)器已滿(mǎn),請(qǐng)等候rnrn);/客戶(hù)端接受 則等待if (send(temp_c1-sockfd, buf, strlen(buf), 0) = 0) printf(sendr errn);/* * client.c * * Created on: 2012-6
17、-18 * Author: root */#include #include #include #include #include #include #include #include #include #include #include #define MAX_SIZE 1024#define PORT 7999static int FLAGE = -1;char name20 = 0;void fun_show(int sockfd)char sendbuf256 = 0;sprintf(sendbuf,SHOWrnrn);if(send(sockfd,sendbuf,strlen(sen
18、dbuf),0)=0)printf(send errn);close(sockfd);exit(1);void fun_all(int sockfd)char sendbufMAX_SIZE = 0;sprintf(sendbuf,ALLrn,5);printf(輸入發(fā)送的內(nèi)容:n);scanf(%s,sendbuf+5);sprintf(sendbuf+strlen(sendbuf),rnrn);if(send(sockfd,sendbuf,strlen(sendbuf),0) = 0)printf(send errn);close(sockfd);exit(1);void fun_one(
19、int sockfd)char sendbufMAX_SIZE = 0;char name320 = 0;printf(輸入聊天對(duì)象:);scanf(%s,name3);sprintf(sendbuf,TOrn%srn,name3);printf(輸入聊天內(nèi)容:n);scanf(%s,sendbuf+strlen(sendbuf);sprintf(sendbuf+strlen(sendbuf),rnrn);if(send(sockfd,sendbuf,strlen(sendbuf),0) = 0)printf(send errn);close(sockfd);exit(1);void fun_
20、quit(int sockfd)char sendbuf256 = QUITrnrn;if(send(sockfd,sendbuf,strlen(sendbuf),0) = 0)printf(send errn);close(sockfd);exit(1);void *pthread_fun(int *sock)int sockfd = *sock;char recvbuf1024 = 0;int n = 0;char *p,*q;char name220 = 0;while(1)memset(recvbuf,0,1024);n = recv(sockfd,recvbuf,sizeof(rec
21、vbuf),0);if(n=0)printf(recv failedn);exit(1);if(!strncmp(recvbuf,NOTICErn通訊通道開(kāi)啟rnrn,30)printf(通訊通道開(kāi)啟n);FLAGE=1;if (!strncmp(recvbuf, 100rn, 5) char *p = strtok(recvbuf + 5, rnrn);/100rn:3個(gè)字符后取出rnrn前的名字strcpy(name2, p);printf(NOTICE%s進(jìn)入聊天室n, name2);FLAGE = 4;if (!strncmp(recvbuf, ERRORrn用戶(hù)名重復(fù)rnrn, 26
22、) printf(用戶(hù)名重復(fù)n);FLAGE = 3;if (!strncmp(recvbuf, 200rn, 5) p = strtok(recvbuf+5,rnrn);while(p != NULL)printf(%sn,p);p = strtok(NULL,rnrn);printf(please input con:n);if(!strncmp(recvbuf,AFROMrn,7)/printf(recvbuf=%sn,recvbuf);p = strtok(recvbuf+6,rn);q = strtok(NULL,rnrn);printf(%s)群聊:%sn,p, q);printf
23、(please input con:n);if(!strncmp(recvbuf,FROMrn,6)p = strtok(recvbuf+6,rn);q = strtok(NULL,rnrn);printf(%s)私聊(%s):%sn,p,name,q);printf(please input con:n);if(!strncmp(recvbuf,ERROR2rn,8)p = strtok(recvbuf + 8, rn);printf(%sn, p);if(!strncmp(recvbuf,NOTICE1rn,9)p = strtok(recvbuf + 9, rn);printf(用戶(hù)%s
24、n, p);if(!strncmp(recvbuf,NOTICErn服務(wù)器已滿(mǎn),請(qǐng)等候rnrn,37)printf(服務(wù)器已滿(mǎn),請(qǐng)等候n);FLAGE = 0;if(!strncmp(recvbuf,NOTICErn您已被喚醒,請(qǐng)繼續(xù)操作rnrn,45)printf(你已經(jīng)被喚醒,請(qǐng)繼續(xù)操作n);FLAGE = 2;int main(int argc,char *argv)if(argc != 2)printf(input server ip:n);exit(1);pthread_t pid;int sockfd;struct sockaddr_in addr;char recvbuf1024
25、 = 0;char sendbuf1024 = 0;int k=0;int n;char str6 = 0;char *p,*q;if(sockfd = socket(AF_INET,SOCK_STREAM,0) = -1)perror(socket);exit(1);bzero(&addr,sizeof(addr);addr.sin_family = AF_INET;addr.sin_port = htons(PORT);if(inet_pton(AF_INET,argv1,(void *)&addr.sin_addr) 0)usleep(100);/讓子進(jìn)程先運(yùn)行while(FLAGE=0)printf(服務(wù)器已滿(mǎn),wait.n);sleep(1);tcflush(0,TCIFLUSH);while(FLAGE=2) /當(dāng)FLAGE2時(shí)正好父進(jìn)程運(yùn)行,則等
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 長(zhǎng)春工業(yè)大學(xué)人文信息學(xué)院《BM安裝工程計(jì)量》2023-2024學(xué)年第二學(xué)期期末試卷
- 南昌理工學(xué)院《現(xiàn)代控制》2023-2024學(xué)年第二學(xué)期期末試卷
- 昆明幼兒師范高等專(zhuān)科學(xué)?!督鹑趯W(xué)前沿動(dòng)態(tài)》2023-2024學(xué)年第二學(xué)期期末試卷
- 信陽(yáng)農(nóng)林學(xué)院《臺(tái)港暨海外華文文學(xué)研究》2023-2024學(xué)年第二學(xué)期期末試卷
- 西安體育學(xué)院《大數(shù)據(jù)機(jī)器學(xué)習(xí)》2023-2024學(xué)年第二學(xué)期期末試卷
- 濰坊工商職業(yè)學(xué)院《機(jī)器學(xué)習(xí)實(shí)驗(yàn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 廣東信息工程職業(yè)學(xué)院《UML及形式化建?!?023-2024學(xué)年第二學(xué)期期末試卷
- 山西旅游職業(yè)學(xué)院《化工原理(Ⅰ)》2023-2024學(xué)年第二學(xué)期期末試卷
- 湘潭醫(yī)衛(wèi)職業(yè)技術(shù)學(xué)院《信號(hào)分析與處理》2023-2024學(xué)年第二學(xué)期期末試卷
- 麗水職業(yè)技術(shù)學(xué)院《詩(shī)歌導(dǎo)讀》2023-2024學(xué)年第二學(xué)期期末試卷
- 五年級(jí)下冊(cè)音樂(lè)課程綱要
- 食材配送、包裝、運(yùn)輸、驗(yàn)收、售后服務(wù)方案應(yīng)急預(yù)案
- 萬(wàn)千教育學(xué)前讀懂兒童的思維:支持自主游戲中的圖式探索
- 產(chǎn)品外觀(guān)檢驗(yàn)標(biāo)準(zhǔn)通用
- 中石化YC分公司易捷便利店市場(chǎng)營(yíng)銷(xiāo)策略研究
- 醫(yī)院護(hù)理培訓(xùn)課件:《病區(qū)環(huán)境管理查房》
- 《小羊和蝴蝶》繪本故事
- 鋼筋工理論考試題庫(kù)及答案
- 大數(shù)據(jù)技術(shù)基礎(chǔ)及應(yīng)用教程(Linux+Hadoop+Spark) 習(xí)題答案
- 高等數(shù)學(xué)(新標(biāo)準(zhǔn)教材)高職PPT完整全套教學(xué)課件
- 人教A版選擇性6.2.1排列6.2.2排列數(shù)課件(20張)
評(píng)論
0/150
提交評(píng)論