新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
C++如何實(shí)現(xiàn)聊天程序-創(chuàng)新互聯(lián)
本篇內(nèi)容介紹了“C++如何實(shí)現(xiàn)聊天程序”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
服務(wù)端
#include#include #pragma comment(lib,"ws2_32.lib") using namespace std; void initialization(); int main(){ //定義長(zhǎng)度變量 int send_len=0; int recv_len=0; int len=0; //定義發(fā)送緩沖區(qū)和接受緩沖區(qū) char send_buf[100]; char recv_buf[100]; //定義服務(wù)端套接字,接受請(qǐng)求套接字 SOCKET s_server; SOCKET s_accept; //服務(wù)端地址客戶(hù)端地址 SOCKADDR_IN server_addr; SOCKADDR_IN accept_addr; initialization(); //填充服務(wù)端信息 server_addr.sin_family=AF_INET; server_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(1234); //創(chuàng)建套接字 s_server=socket(AF_INET,SOCK_STREAM,0); if(bind(s_server,(SOCKADDR *)&server_addr,sizeof(SOCKADDR))==SOCKET_ERROR){ cout<<"套接字綁定失敗!"< >send_buf; send_len=send(s_accept,send_buf,100,0); if(send_len<0){ cout<<"發(fā)送失敗!"< 客戶(hù)端
#include#include #pragma comment(lib,"ws2_32.lib") using namespace std; void initialization(); int main(){ //定義長(zhǎng)度變量 int send_len=0; int recv_len=0; //定義發(fā)送緩沖區(qū)和接受緩沖區(qū) char send_buf[100]; char recv_buf[100]; //定義服務(wù)端套接字,接受請(qǐng)求套接字 SOCKET s_server; //服務(wù)端地址客戶(hù)端地址 SOCKADDR_IN server_addr; initialization(); //填充服務(wù)端信息 server_addr.sin_family=AF_INET; server_addr.sin_addr.S_un.S_addr=inet_addr("192.168.1.1"); server_addr.sin_port=htons(1234); //創(chuàng)建套接字 s_server=socket(AF_INET,SOCK_STREAM,0); if(connect(s_server,(SOCKADDR *)&server_addr,sizeof(SOCKADDR))==SOCKET_ERROR){ cout<<"服務(wù)器連接失敗!"< >send_buf; send_len=send(s_server,send_buf,100,0); if(send_len<0){ cout<<"發(fā)送失敗!"< “C++如何實(shí)現(xiàn)聊天程序”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)站名稱(chēng):C++如何實(shí)現(xiàn)聊天程序-創(chuàng)新互聯(lián)
URL標(biāo)題:http://www.ef60e0e.cn/article/djheph.html