以下是C语言WinSock学习笔记第2/2页的完整攻略:
概述
WinSock(Windows套接字)是一组用于网络编程的API,最初由Microsoft开发并在Windows95上引入。WinSock API使得开发人员可以使用C语言编写网络应用程序,如web浏览器和FTP客户端等。本文将介绍如何使用WinSock API进行网络编程,构建客户端和服务器程序。
客户端
在WinSock中,客户端程序的基本工作流程如下:
- 初始化WinSock库
- 创建套接字
- 连接到服务器
- 从服务器接收数据(如果需要)
- 关闭套接字和WinSock库
下面是一个简单的示例,它演示了如何使用WinSock API编写一个s客户端程序:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSADATA wsaData;
SOCKET ConnectSocket;
struct sockaddr_in serv_addr;
char receiveBuffer[256];
int iResult;
int receiveSize = 256;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(80);
serv_addr.sin_addr.s_addr = inet_addr("74.125.24.106");
iResult = connect(ConnectSocket, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if (iResult == SOCKET_ERROR) {
printf("connect failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
iResult = recv(ConnectSocket, receiveBuffer, receiveSize, 0);
if (iResult > 0) {
printf("Received: %s\n", receiveBuffer);
} else if (iResult == 0) {
printf("Connection closed\n");
} else {
printf("recv failed with error: %d\n", WSAGetLastError());
}
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
上述代码将建立与Google首页的TCP连接,并将收到的数据打印到控制台上。
服务器端
在WinSock中,服务器程序的基本工作流程如下:
- 初始化WinSock库
- 创建套接字,并将其与一个IP地址和端口号绑定
- 在套接字上侦听传入的连接请求
- 接受连接请求
- 从客户端接收数据(如果需要)
- 处理客户端请求
- 关闭连接和套接字,并清除WinSock库
下面是一个简单的示例,它演示了如何使用WinSock API编写一个UDP服务器程序:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSADATA wsaData;
SOCKET ListenSocket;
struct sockaddr_in serv_addr, cli_addr;
char receiveBuffer[256];
int iResult;
int receiveSize = 256;
int cli_len;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(iResult) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ListenSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(9000);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
iResult = bind(ListenSocket, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if(iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
while(1) {
printf("Waiting for datagrams...\n");
memset((char *)&cli_addr, 0, sizeof(cli_addr));
iResult = recvfrom(ListenSocket, receiveBuffer, receiveSize, 0, (struct sockaddr *)&cli_addr, &cli_len);
if(iResult == SOCKET_ERROR) {
printf("recvfrom failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
} else {
receiveBuffer[iResult] = '\0';
printf("Received datagram from client: %s\n", receiveBuffer);
iResult = sendto(ListenSocket, "Hello from server!", 19, 0, (struct sockaddr *)&cli_addr, sizeof(cli_addr));
if(iResult == SOCKET_ERROR) {
printf("sendto failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
} else {
printf("Sent %d bytes to client\n", iResult);
}
}
}
closesocket(ListenSocket);
WSACleanup();
return 0;
}
上述代码将侦听UDP数据报,将收到的数据报打印到控制台上,并向客户端发送一个字符串"Hello from server!"。
希望以上内容对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言WinSock学习笔记第2/2页 - Python技术站