网络编程基本函数和基本数据结构
数据结构
struct sockaddr {
u_short sa_family; //16位地址类型 2字节
char sa_data[14]; //14字节地址数据:ip + port
};
struct sockaddr_in {
short sin_family; //16位地址类型
u_short sin_port; //16位端口号 65535 2的16次方
struct in_addr sin_addr; //32位IP地址 4字节
char sin_zero[8]; //8字节填充
};
效果如下:
服务器端:
// socket_demo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <iostream>
#include<windows.h>
#pragma comment(lib, "ws2_32.lib")
bool CreateServerSocket()
{
BOOL bRet=FALSE;
do
{
//1、创建套接字
SOCKET server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET== server)
{
SetLastError(WSAGetLastError());
break;
}
//2、绑定Socket到本地
sockaddr_in server_addr = {0};
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(28888); //htons:将参数转换成TCP/IP字节序列
server_addr.sin_addr.S_un.S_addr = inet_addr("10.11.50.111");//IP地址
if (SOCKET_ERROR == bind(server, (sockaddr*)&server_addr, sizeof(server_addr)))
{
SetLastError(WSAGetLastError());
break;
}
//3、监听
printf("Is listening\n");
if (SOCKET_ERROR == listen(server, 10))
{
SetLastError(WSAGetLastError());
break;
}
//4、处理连接请求
sockaddr_in client_addr = {0};
int len = sizeof(client_addr);
SOCKET client=accept(server, (sockaddr*)&client_addr, &len);
if (INVALID_SOCKET == client)
{
SetLastError(WSAGetLastError());
break;
}
printf("is receiving \n");
//5、接受数据
char szInfo[1024] = { 0 };
int iRet=-1;
while (1)
{
iRet = recv(client, szInfo, 1013, 0);
if (0 >= iRet)
{
continue;
}
else
{
printf("receive:%s\n", szInfo);
iRet = -1;
}
}
bRet = TRUE;
} while (FALSE);
return bRet;
}
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
return -1;
}
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return -1;
}
if (FALSE==CreateServerSocket())
{
printf("CreateServerSocket failed with error: %d\n", GetLastError());
return -1;
}
system("pause");
WSACleanup();
return 0;
}
客户端代码:
// client_socket_demo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <iostream>
#include<windows.h>
#pragma comment(lib, "ws2_32.lib")
bool CreateClientSocket()
{
BOOL bRet = FALSE;
do
{
//1、创建套接字
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET == client)
{
SetLastError(WSAGetLastError());
break;
}
//2、连接服务器socke地址
sockaddr_in server_addr = { 0 };
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(28888); //htons:将参数转换成TCP/IP字节序列
server_addr.sin_addr.S_un.S_addr = inet_addr("10.11.50.111");//IP地址//10.11.50.111 虚拟机:192.168.48.128
if (SOCKET_ERROR == connect(client, (sockaddr*)&server_addr, sizeof(server_addr)))
{
SetLastError(WSAGetLastError());
break;
}
//3、发送数据
char sz_send_info[1024] = { 0 };
int iRet = 0;
while (1)
{
printf("Please input the message you want to send \n");
std::cin >> sz_send_info;
iRet=send(client, (char*)sz_send_info, sizeof(sz_send_info), 0);
if (iRet!=SOCKET_ERROR)
{
printf("send successfully\n");
}
else
{
printf("failed to send");
}
}
bRet = TRUE;
} while (FALSE);
return bRet;
}
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
return -1;
}
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return -1;
}
if (FALSE == CreateClientSocket())
{
printf("CreateClientSocket failed with error: %d\n", GetLastError());
return -1;
}
system("pause");
WSACleanup();
return 0;
}