客户端:
#include <iostream>
#include <winsock2.h>
using namespace std;
#pragma comment (lib, "ws2_32.lib")
#define PORT 4000
#define IP_ADDRESS "127.0.0.1"
int main(int agrc, char * agrv[])
{
WSADATA ws;
SOCKET ClientSocket;
struct sockaddr_in ServerAddr;
int Ret = 0;
int AddrLen = 0;
HANDLE hTread = NULL;
char SendBuffer[MAX_PATH];
int errorcode = 0;
//Init Window Socket
if ((errorcode = WSAStartup(MAKEWORD(2, 2), &ws)) != 0)
{
cout << "Init Windows Socket Failed::" << errorcode << endl;
return -1;
}
// Create Socket
ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ClientSocket == INVALID_SOCKET)
{
cout << "Create Socket Failed::" << WSAGetLastError() << endl;
return -1;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
ServerAddr.sin_port = htons(PORT);
memset(ServerAddr.sin_zero, 0x00, 8);
// connect
Ret = connect(ClientSocket, (struct sockaddr*)&ServerAddr, sizeof(ServerAddr));
if (Ret == SOCKET_ERROR)
{
cout << "Connect Error::" << WSAGetLastError() << endl;
return -1;
}else{
cout << "连接成功!!" << endl;
}
while (true)
{
cin.getline(SendBuffer, sizeof(SendBuffer));
Ret = send(ClientSocket, SendBuffer, (int)strlen(SendBuffer), 0);
if (Ret == SOCKET_ERROR)
{
cout << "Sentd Info Error::" << GetLastError() << endl;
break;
}
}
closesocket(ClientSocket);
WSACleanup();
return 0;
}
服务端:
#include <iostream>
#include <WinSock2.h>
using namespace std;
#pragma comment (lib, "ws2_32.lib")
#define PORT 4000
#define IP_ADDREESS "127.0.0.1"
int main(int agrc, char* argc[])
{
WSADATA ws; // 之后储存WSAStartUP返回 的数据, 全称:Windows Socker API Data
SOCKET ServerSocket = 0, ClientSocket = 0;
struct sockaddr_in LocalAddr, ClientAddr;
int errorcode;
int Ret = 0;
int AddrLen = 0;
HANDLE hThread = NULL;
//Init Windwos Socket
if ((errorcode = WSAStartup(MAKEWORD(2, 2), &ws)) != 0)
{
cout << "Create Socket Failed:" << errorcode << endl;
return -1;
}
// CreateSocket
ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ServerSocket == INVALID_SOCKET)
{
cout << "Create Socket Failed::" << WSAGetLastError() << endl;
return -1;
}
LocalAddr.sin_family = AF_INET;
LocalAddr.sin_addr.s_addr = inet_addr(IP_ADDREESS); //将ip 127.0.0.1 转换成网络字节序
LocalAddr.sin_port = htons(PORT); // 转换为网络字节序
memset(LocalAddr.sin_zero, 0x00, 8);
//Bind Socket
Ret = bind(ServerSocket,(struct sockaddr*)&LocalAddr, sizeof(LocalAddr));
if (Ret != 0)
{
cout << "Bind Socket Failded::" << WSAGetLastError() << endl;
return -1;
}
// listen
Ret = listen(ServerSocket, 10);
if(Ret != 0)
{
cout << "Listen Socket Failed::" << WSAGetLastError() << endl;
return -1;
}
cout << " 服务端已经启动!" << endl;
while (true)
{
AddrLen = sizeof(ClientAddr);
ClientSocket = accept(ServerSocket, (struct sockaddr*)&ClientAddr, &AddrLen);
if (ClientSocket == INVALID_SOCKET)
{
cout << "Accept Failed::" << WSAGetLastError() << endl;
break;
}
char RecvBuffer[MAX_PATH];
while (true)
{
memset(RecvBuffer, 0x00, sizeof(RecvBuffer));
// 接收数据
// recv函数返回实际copy的字节数;
// copy时出错,返回SOCKET_ERROR;等待协议接收数据时网络中断,返回0。
Ret = recv(ClientSocket, RecvBuffer, MAX_PATH, 0);
if (Ret == 0 || Ret == SOCKET_ERROR)
{
cout << "Recv Failed::" << WSAGetLastError() << endl;
break;
}
cout << "接收到的客户信息为:" << RecvBuffer << endl;
}
}
closesocket(ServerSocket);
closesocket(ClientSocket);
WSACleanup();
return 0;
}
AF_INET 与 AF_INET6
AF_INET(又称 PF_INET)是 IPv4 网络协议的套接字类型,AF_INET6 则是 IPv6 的;而 AF_UNIX 则是 Unix 系统本地通信。
选择 AF_INET 的目的就是使用 IPv4 进行通信。因为 IPv4 使用 32 位地址,相比 IPv6 的 128 位来说,计算更快,便于用于局域网通信。
而且 AF_INET 相比 AF_UNIX 更具通用性,因为 Windows 上有 AF_INET 而没有 AF_UN