服务器:
创建套接字(socket) -> 绑定IP(bind) -> 设置监听上限(listen)-> 开始接受(accept) -> 接收消息(recv) ->发送消息(send)
#include <iostream>
#include <string.h>
#include <WinSock2.h>
#include <errno.h>
#include <WS2tcpip.h>
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
WSADATA wsaData;
int iRet = 0;
iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iRet != 0)
{
cout << "WSAStartup(MAKEWORD(2, 2), &wsaData) execute failed!" << endl;
return -1;
}
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
WSACleanup();
cout << "WSADATA version is not correct!" << endl;
return -1;
}
SOCKET server_sock, server_connect_sock;
server_sock = socket(AF_INET,SOCK_STREAM,0);
if (INVALID_SOCKET == server_sock)
{
cout << "socket create error!!!" << endl;
return -1;
}
cout << "socket create success !!" << endl;
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.S_un.S_addr= htonl(INADDR_ANY);
server_addr.sin_port = htons(6000);
if (SOCKET_ERROR == bind(server_sock, (sockaddr *)&server_addr, sizeof(sockaddr)))
{
cout << "bind error!!!" << endl;
return -1;
}
cout << "bind success !!" << endl;
if (SOCKET_ERROR == listen(server_sock, 5))
{
cout << "listen fail" << endl;
return -1;
}
cout << "listen successs" << endl;
struct sockaddr_in client_addr;
int len = sizeof(sockaddr);
server_connect_sock = accept(server_sock, (sockaddr *)&client_addr, &len);
if (INVALID_SOCKET == server_connect_sock)
{
cout << "accept error !!" << endl;
}
while (true)
{
char recvBuf[100];
char sendBuf[100] = { "hello" };
memset(recvBuf, 0, sizeof(recvBuf));
recv(server_connect_sock, recvBuf, 100, 0);
cout << "server recvs " << inet_ntoa(client_addr.sin_addr) << " : " << recvBuf << endl;
send(server_connect_sock, sendBuf, 100, 0);
}
cout << "OK" << endl;
closesocket(server_sock);
closesocket(server_connect_sock);
WSACleanup();
return 0;
}
客户端
创建套接字(socket) -> 建立链接(connect) ->发送消息(send) -> 接收消息(recv)
#include<iostream>
#include <winsock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
WSADATA wsaData;
int iRet = 0;
iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iRet != 0)
{
cout << "WSAStartup(MAKEWORD(2, 2), &wsaData) execute failed!" << endl;
return -1;
}
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
WSACleanup();
cout << "WSADATA version is not correct!" << endl;
return -1;
}
SOCKET client_Socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_Socket == INVALID_SOCKET)
{
cout << "client_Socket create failed!" << endl;
return -1;
}
cout << "client_Socket create success !! " << endl;
SOCKADDR_IN server_addr;
inet_pton(AF_INET, "192.168.0.102", &server_addr.sin_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(6000);
iRet = connect(client_Socket, (SOCKADDR*)&server_addr, sizeof(SOCKADDR));
if (0 != iRet)
{
cout << "connect failed!" << endl;
return -1;
}
cout << "connect success!" << endl;
char sendBuf[100];
char recvBuf[100];
while (true)
{
memset(sendBuf, 0, sizeof(sendBuf));
memset(recvBuf, 0, sizeof(recvBuf));
cout << "send:";
cin >> sendBuf;
send(client_Socket, sendBuf, strlen(sendBuf) + 1, 0);
recv(client_Socket, recvBuf, 100, 0);
cout << "client recv : " << recvBuf << endl;
}
closesocket(client_Socket);
WSACleanup();
return 0;
}
|