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

套盒子編程

發布時間: 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;

}
}

熱點內容
oppoa33存儲設置方法 發布:2024-11-17 00:41:54 瀏覽:390
sql的if函數怎麼用 發布:2024-11-17 00:36:52 瀏覽:238
填滿我的世界的伺服器叫什麼 發布:2024-11-17 00:32:16 瀏覽:27
mysql資料庫在哪裡看 發布:2024-11-17 00:25:28 瀏覽:138
電腦上面顯示配置怎麼看 發布:2024-11-17 00:20:17 瀏覽:587
括弧計演算法 發布:2024-11-17 00:06:18 瀏覽:870
筆記本ghost配置是什麼 發布:2024-11-17 00:04:12 瀏覽:335
androidappwidget 發布:2024-11-16 23:27:18 瀏覽:677
圖片加密上傳 發布:2024-11-16 23:24:54 瀏覽:72
騙軟體演算法 發布:2024-11-16 23:21:50 瀏覽:647