到目前為止,小小專案大改了兩次。第一次實現了點對點的聊天,使用tcp套接字實現。第二次在第一次的基礎上實現了多執行緒處理聊天事件,通過執行緒同步實現。我把部分核心**貼出來,供大家交流。
總**量上千,全部貼出來不方便。客戶端和服務端都只分享主函式和執行緒函式。
server:
void recvandsend(void) //監聽**執行緒入口函式
}if (statusflag == 0)
break;
case sig_client_register:
print_recv_mess(sig_client_register, mess.usename);
printf("client name : %s\n", mess.usename);
printf("client age : %s\n", mess.userage);
printf("client *** : %s\n", mess.user***);
printf("client phone: %s\n", mess.userphone);
printf("client secr : %s\n", mess.usesecr);
memcpy(sock->person.usename, mess.usename, strlen(mess.usename));
memcpy(sock->person.userage, mess.userage, strlen(mess.userage));
memcpy(sock->person.user***, mess.user***, strlen(mess.user***));
memcpy(sock->person.userphone, mess.userphone, strlen(mess.userphone));
memcpy(sock->person.usesecr, mess.usesecr, strlen(mess.usesecr));
sock->person.status = 1;
send_client_mess(sock->fd, "register succeed, please login on!", sig_client_register_succeed);
break;
case sig_trasmit_message:
print_recv_mess(sig_trasmit_message, mess.usename);
send_client_mess(sock->fd, "transmit message send succeed!", sig_trasmit_message_succeed);
break;
case sig_client_send_msg:
print_recv_mess(sig_client_send_msg, sock->person.usename);
send_client_mess(sock->fd, "chat message send succeed!", sig_client_send_msg_succeed);
break;
case sig_client_quit:
print_recv_mess(sig_client_quit, sock->person.usename);
exitclient(sock);
send_client_mess(sock->fd, "logout succeed!", sig_client_quit_succeed);
break;}}
} }pthread_exit(null);
exit(1);
}int main(int argc, char *ar**)
/* 伺服器端開始建立 socket 描述符 */
if ((sockfd = socket(af_inet, sock_stream, 0)) == -1)
/* 伺服器端填充 sockaddr 結構 */
bzero(&server_addr, sizeof(struct sockaddr_in));
server_addr.sin_family = af_inet;
server_addr.sin_addr.s_addr = htonl(inaddr_any);
server_addr.sin_port = htons(port);
/* ** sockfd 描述符 */
if (bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1)
printf("server port : %d ... ...\n", port);
/* 監聽 sockfd 描述符 */
if (listen(sockfd, listenq) == -1)
pthread_create(&p_thread, null, recvandsend, null);
while (1)
}else
else
}} }
close(sockfd);
exit(0);
}
client:
int main(int argc, char *ar**)
connect_server(); // 連線伺服器
rec_mess_init(); // 初始化接收訊息執行緒
while(threadflag1){}; // 等待客戶端連線成功
client_worker_init(); // 客戶端工作執行緒初始化
while(threadflag2){}; //
while(running)
} }/*接受訊息執行緒初始化*/
void rec_mess_init()
while(iloop--)
printf("recreate the receive message thread , %d ... ...\n", iloop);
sleep(1);
} if(iloop != 5)
pthread_attr_destroy(&attr);
}// 初始化客戶端介面函式
// 主要處理客戶端註冊或者登入事件
void client_worker_init()
while(iloop--)
printf("recreate the receive message thread , %d ... ...\n", iloop);
sleep(1);
} if(iloop != 5)
pthread_attr_destroy(&attr);
}
LINUX同步聊天小程式
檔名 tcp client.c 檔案描述 tcp迴圈伺服器客戶端 xue,2012 07 26 include include include include include include include include define buffer 1024 define port 7777 in...
基於C 的聊天程式
1.引言 1.1目的 編寫詳細設計說明書是軟體開發過程必不可少的部分,其目的是為了在完成需求分析說明書的基礎上完成需求分析說明規定的各項模組的具體實現的設計工作。1.2定義 套接字socket 網路上的兩個程式通過乙個雙向的通訊連線實現資料的交換,這個連線的一端稱為乙個socket。tcp協議 tc...
Socket 聊天小程式
伺服器端 server.c include include include include include include include in.h include include include define buflen 1024 define port 6666 define listnum ...