WinSock2.0通信的一个例子(基于VC++6.0开发测试)

实验目的:

掌握Winsock2.0套接字编程技术的基本方法。

实验要求:

运用TCP/IP Winsock2.0套接字编程技术,使用VC编写一个面向连接通信的服务端程序与客户端程序,服务器先与端口3000绑定,

然后等待客户连接,连接成功后就可与客户端互相收、发信息。交互中,通信任一方只要发送 bye, 通信过程便自动结束。

服务端:

#include <conio.h>
#include <stdio.h>
#include <winsock2.h> #pragma comment(lib, "Ws2_32.lib") int main(int, char **)
{
struct sockaddr addr;
int recvLen, errCode; char msg[] = {};
SOCKET sock, sersock; WORD wVersionRequested;
WSADATA wsaData;
LPSTR messageBuffer = NULL; SetConsoleTitle("服务端"); wVersionRequested = MAKEWORD(, );
errCode = WSAStartup(wVersionRequested, &wsaData); if (errCode)
{
printf("没有Windows Socket动态库!\n");
return WSAGetLastError();
} if (LOBYTE(wsaData.wVersion) != || //不是Winsock 2.0
HIBYTE(wsaData.wVersion) != )
{
printf("需要Windows Socket 2!\n");
WSACleanup();
return WSAGetLastError();
} sock = socket(AF_INET, SOCK_STREAM, ); if (sock == INVALID_SOCKET)
{
printf("不能创建Socket!\n");
getch();
WSACleanup();
return ;
} ((sockaddr_in *)&addr)->sin_family = AF_INET; ((sockaddr_in *)&addr)->sin_port = htons(); ((sockaddr_in *)&addr)->sin_addr.s_addr = inet_addr("127.0.0.1"); bind(sock, &addr, sizeof(addr)); listen(sock, ); printf("等待客户连接!\n");
recvLen = sizeof(addr);
sersock = accept(sock, &addr, &recvLen); if (sersock == INVALID_SOCKET)
{
errCode = WSAGetLastError(); size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&messageBuffer, , NULL); printf("%s\n", messageBuffer);
free(messageBuffer); getch();
closesocket(sock);
WSACleanup();
return ;
} printf("有客户连接!\n输入bye通信结束\n"); do
{
printf("\n等待客户端输入信息......");
recvLen = recv(sersock, msg, , );
printf("\n客户端:%s", msg); if (!strcmp(msg, "bye"))
break;
printf("\n服务器:");
gets(msg); send(sersock, msg, strlen(msg) + , ); } while (strcmp(msg, "bye")); closesocket(sersock);
closesocket(sock);
printf("\n\n会话结束,按任一键返回\n");
getch();
WSACleanup();
return ;
}

客户端:

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h> #pragma comment(lib, "Ws2_32.lib") int main(int, char **)
{
SOCKET sock;
struct sockaddr addr;
int errCode;
char msg[] = {}; WORD wVersionRequested;
WSADATA wsaData; SetConsoleTitle("客户端");
wVersionRequested = MAKEWORD(, );
errCode = WSAStartup(wVersionRequested, &wsaData); if (errCode)
{
printf("没有Windows Socket动态库!\n");
return WSAGetLastError();
} if (LOBYTE(wsaData.wVersion) != ||
HIBYTE(wsaData.wVersion) != )
{
printf("需要Windows Socket 2!\n");
WSACleanup();
return WSAGetLastError();
} sock = socket(AF_INET, SOCK_STREAM, ); if (sock == INVALID_SOCKET)
{
printf("不能创建Socket!\n");
getch();
WSACleanup();
return WSAGetLastError();
} ((sockaddr_in *)&addr)->sin_family = AF_INET; ((sockaddr_in *)&addr)->sin_addr.s_addr = inet_addr("127.0.0.1"); ((sockaddr_in *)&addr)->sin_port = htons(); printf("与服务器连接...!\n"); errCode = connect(sock, (sockaddr *)&addr, sizeof(addr));
if (errCode == SOCKET_ERROR)
{
printf("连接失败!\n");
getch();
WSACleanup();
return ;
} printf("成功连接到服务器127.0.0.1!\n若输入bye则通信结束\n"); do
{
printf("\n\n客户端:");
gets(msg); send(sock, msg, strlen(msg) + , ); if (!strcmp(msg, "bye")) break; printf("等待服务器响应......\n"); recv(sock, msg, , ); printf("服务器:%s", msg); } while (strcmp(msg, "bye")); closesocket(sock);
printf("\n会话结束,按任一键返回\n");
getch();
WSACleanup();
return ;
}

运行效果:

WinSock2.0通信的一个例子(基于VC++6.0开发测试)

WinSock2.0通信的一个例子(基于VC++6.0开发测试)

此为学校实验题目:word排版请用http://www.planetb.ca/syntax-highlight-word

Winsock2.0相关参考网站,拥有大量示例代码:https://www.tenouk.com/

上一篇:oracle获取某一字段字符串长度


下一篇:Xcode中iOS模拟器程序中的plist路径