WinSock2网络通信(win32编程)
本代码有服务器端server.cpp和客户端client.cpp,客户端连接服务器端即可实现简单文字通信。
服务器server.cpp代码如下:
// server.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include#pragma comment(lib, "ws2_32") //引入lib库
void SockInit();
void Error();
DWORD WINAPI ClientThread(void *p);
int _tmain(int argc, _TCHAR* argv[])
{
SockInit();//网络初始化
SOCKET serverSock = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(4086);
addr.sin_addr.S_un.S_addr = ADDR_ANY;//可能有多个网卡,所以设为所有ip
int ret1 = bind(serverSock, (sockaddr*)&addr, sizeof(sockaddr));
//printf("%d",ret1);
Error();
listen(serverSock, 5);//最多监听5个
printf("server run...\n");
while (1) {
sockaddr_in cliAddr;
int addLen = sizeof(cliAddr);
SOCKET clientSock = accept(serverSock, (sockaddr*)&cliAddr, &addLen);
char *buff = "welocme connect";
int ret2 = send(clientSock, buff, strlen(buff),0);
CreateThread(0, 0, ClientThread, &clientSock, 0, 0);//有新客户端连接时,开启一个线程用于接收消息
}
closesocket(serverSock);
return 0;
}
DWORD WINAPI ClientThread(void *p) {
SOCKET clientSock = *(SOCKET*)p;
while (1) {
char buff[1024] = { 0 };
int ret3 = recv(clientSock, buff, 1024, 0);
ERROR();
printf(buff);
send(clientSock, buff, strlen(buff), 0);
}
}
void SockInit()//网络初始化,相当于允使程序使用网络
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
printf("WSAStartup failed witherror: %d\n", err);
exit(-1);
}
}
void Error() {//获取异常错误
int err = GetLastError();
if (err >0) {
printf("Error code = %d\n", err);
exit(-1);
}
}
客户端client.cpp代码如下:
// client.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include#pragma comment(lib, "ws2_32") //引入lib库#includevoid SockInit();
void Error();
DWORD WINAPI ServerThread(void *p);
int _tmain(int argc, _TCHAR* argv[])
{
SockInit();//网络初始化
SOCKET clientSock = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(4086);
//通过ipconfig查看
//本机ip127.0.0.1
//addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//连接本机,所以ip是这样
addr.sin_addr.S_un.S_un_b.s_b1 = 127;
addr.sin_addr.S_un.S_un_b.s_b2 = 0;
addr.sin_addr.S_un.S_un_b.s_b3 = 0;
addr.sin_addr.S_un.S_un_b.s_b4 = 1;
connect(clientSock, (sockaddr*)&addr, sizeof(sockaddr));
CreateThread(0, 0, ServerThread, &clientSock, 0, 0);
while (1) {
char buff[1024] = { 0 };
gets_s(buff);
int ret4 = send(clientSock, buff, strlen(buff), 0);
}
closesocket(clientSock);
return 0;
}
DWORD WINAPI ServerThread(void *p) {//开启线程用于和服务器连接
SOCKET clientSock = *(SOCKET*)p;
while (1) {
char buff[1024] = { 0 };
recv(clientSock, buff, 1024, 0);
printf("Recv : %s\n", buff);
}
}
void SockInit()//网络初始化
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
printf("WSAStartup failed witherror: %d\n", err);
exit(-1);
}
}
void Error() {//查看网络连接是否异常。如果有错误码,则可以点击工具-〉错误查找,输入错误码即可
int err = GetLastError();
if (err >0) {
printf("Error code = %d\n", err);
exit(-1);
}
}
解决方案中右键点击server.cpp,设为启动项,再点击开始执行,先运行服务器。
同理再将client.cpp运行。运行结果如下: