套盒子编程
① 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;
}
}