当前位置:首页 » 编程软件 » 套盒子编程

套盒子编程

发布时间: 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;

}
}

热点内容
androidappwidget 发布:2024-11-16 23:27:18 浏览:676
图片加密上传 发布:2024-11-16 23:24:54 浏览:71
骗软件算法 发布:2024-11-16 23:21:50 浏览:646
20人团队解压拓展怎么玩 发布:2024-11-16 23:03:34 浏览:159
rsa解密算法c 发布:2024-11-16 22:41:43 浏览:27
python3log 发布:2024-11-16 22:41:34 浏览:658
手机如何热点密码是多少 发布:2024-11-16 22:41:31 浏览:350
android上传多个文件 发布:2024-11-16 22:36:24 浏览:313
苹果微信25区怎么改为安卓区 发布:2024-11-16 22:32:39 浏览:651
数控编程轻松 发布:2024-11-16 22:23:38 浏览:814