當前位置:首頁 » 編程軟體 » 套盒子編程

套盒子編程

發布時間: 2024-01-04 03:12:43

① C語言套接字編程實現通信

給你兩個簡單示例參考下,先運行服務端,再運行客服端,可以實現兩者之間的通信。

服務端:
#include <WINSOCK2.H>
#include <iostream>
using namespace std;

#pragma comment(lib,"ws2_32.lib")

void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return;
}

SOCKET listenfd = socket(AF_INET,SOCK_STREAM,0);
if (listenfd == INVALID_SOCKET)
{
cout<<"socket failed with err code="<<WSAGetLastError()<<endl;
return;
}

sockaddr_in servaddr;
memset(&servaddr,0,sizeof(sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5555);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
// servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
int opt = 1;
int ret = setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&opt,sizeof(opt));
if (ret == SOCKET_ERROR)
{
cout<<"setsockopt failed with err code="<<WSAGetLastError()<<endl;
return;
}
ret = bind(listenfd,(sockaddr*)&servaddr,sizeof(servaddr));
if (ret == SOCKET_ERROR)
{
cout<<"bind failed with err code="<<WSAGetLastError()<<endl;
return;
}

ret = listen(listenfd,10);
if (ret == SOCKET_ERROR)
{
cout<<"listen failed with err code="<<WSAGetLastError()<<endl;
return;
}

sockaddr_in peeraddr;
int len = sizeof(sockaddr_in);

// while (1)
// {
SOCKET connSocket = accept(listenfd,(sockaddr*)&peeraddr,&len);
if (connSocket == INVALID_SOCKET)
{
cout<<"accept failed with err code="<<WSAGetLastError()<<endl;
return;
}
// 開辟一個線程

cout<<"peer ip=["<<inet_ntoa(peeraddr.sin_addr)<<"]"<<" port=["<<ntohs(peeraddr.sin_port)<<"]"<<endl;

send(connSocket,"welcome to by",strlen("welcome to by"),0);

char buf[1024];
while (1)
{
memset(buf,0,sizeof(buf));
recv(connSocket,buf,1024,0);
cout<<buf<<endl;
send(connSocket,buf,strlen(buf),0);

}
}
客戶端:
#include <WINSOCK2.H>
#include <iostream>
using namespace std;

#pragma comment(lib,"ws2_32.lib")

void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return;
}

SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
if (sock == INVALID_SOCKET)
{
cout<<"socket failed with err code="<<WSAGetLastError()<<endl;
return;
}

sockaddr_in servaddr;
memset(&servaddr,0,sizeof(sockaddr_in));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(5555);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

int ret = connect(sock,(sockaddr*)&servaddr,sizeof(servaddr));
if (ret == SOCKET_ERROR)
{
cout<<"connect failed with err code="<<WSAGetLastError()<<endl;
return;
}

char recvbuf[1024] = {0};
recv(sock,recvbuf,1024,0);
cout<<recvbuf<<endl;

char sendbuf[1024] = {0};
while (1)
{
memset(sendbuf,0,sizeof(sendbuf));
memset(recvbuf,0,sizeof(recvbuf));
cin>>sendbuf;
send(sock,sendbuf,strlen(sendbuf),0);
recv(sock,recvbuf,1024,0);
cout<<recvbuf<<endl;

}
}

熱點內容
原神用安卓手機玩為什麼畫質那麼低 發布:2025-01-23 03:09:31 瀏覽:847
空調壓縮機是外機嗎 發布:2025-01-23 03:09:31 瀏覽:950
大學資料庫學 發布:2025-01-23 02:54:30 瀏覽:588
部隊營區監控系統錄像存儲多少天 發布:2025-01-23 02:49:26 瀏覽:523
oraclelinux用戶名和密碼 發布:2025-01-23 02:43:06 瀏覽:404
安卓手機主頁滑動屏幕怎麼設置 發布:2025-01-23 02:41:15 瀏覽:225
小臉解壓 發布:2025-01-23 02:24:17 瀏覽:368
網易電腦版我的世界布吉島伺服器 發布:2025-01-23 02:20:17 瀏覽:985
xlc編譯選項 發布:2025-01-23 02:11:25 瀏覽:721
電腦訪問存儲伺服器硬碟 發布:2025-01-23 02:08:29 瀏覽:569