Windows网络编程:基于Scoket最简单的CS
一.简介
socket顾名思义就是套接字的意思,用于描述地址和端口,是一个通信链的句柄。应用程序通过socket向网络发出请求或者回应。
socket编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW),前两者较常用。基于TCP的socket编程是流式套接字。
二.客户端(Client)
#include <iostream>
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
const int BUF_SIZE = 64;
int RetVal;
WSADATA Wsd;
if(WSAStartup(MAKEWORD(2, 2), &Wsd) != 0)
{
cout << "初始化套接字动态库失败" << endl;
return -1;
}
SOCKET ServerSocket;
ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(ServerSocket == INVALID_SOCKET)
{
cout << "创建套接字失败" << endl;
WSACleanup();
return -1;
}
SOCKADDR_IN ServerAddr;
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(2345);
ServerAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
RetVal = connect(ServerSocket, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr));
if(RetVal == SOCKET_ERROR)
{
cout << "连接服务器失败" << endl;
closesocket(ServerSocket);
WSACleanup();
return -1;
}
char SendBuff[BUF_SIZE];
char RECVBuff[BUF_SIZE];
while(true)
{
ZeroMemory(SendBuff, BUF_SIZE);
cout << "向服务器发送数据" << endl;
cin >> SendBuff;
RetVal = send(ServerSocket, SendBuff, strlen(SendBuff), 0);
if(RetVal == SOCKET_ERROR)
{
cout << "发送数据失败" << endl;
closesocket(ServerSocket);
WSACleanup();
return -1;
}
ZeroMemory(RECVBuff, BUF_SIZE);
recv(ServerSocket, RECVBuff, BUF_SIZE, 0);
cout << "从服务器接收数据" << RECVBuff << endl;
}
closesocket(ServerSocket);
WSACleanup();
}
三.服务端(Server)
#include <iostream>
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
int RetVal;
WORD SocketVersion = MAKEWORD(2, 2);
WSADATA wsd;
if(WSAStartup(SocketVersion, &wsd) != 0)
{
cout << "绑定Socket库失败" <<endl;
}
SOCKET ServerSocket;
ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(ServerSocket == INVALID_SOCKET)
{
cout << "创建服务器套接字失败" << endl;
WSACleanup();
return -1;
}
SOCKADDR_IN ServerAddr;
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(2345);
ServerAddr.sin_addr.S_un.S_addr = INADDR_ANY;
RetVal = bind(ServerSocket, (SOCKADDR*)&ServerAddr, sizeof(SOCKADDR_IN));
if(RetVal == SOCKET_ERROR)
{
cout << "套接字绑定失败" << endl;
closesocket(ServerSocket);
WSACleanup();
return -1;
}
RetVal = listen(ServerSocket, 2);
if(RetVal == SOCKET_ERROR)
{
cout << "套接字监听失败" << endl;
closesocket(ServerSocket);
WSACleanup();
return -1;
}
SOCKET ClientSocket;
SOCKADDR_IN ClientAddr;
int ClientAddrLen = sizeof(ClientAddr);
ClientSocket = accept(ServerSocket, (SOCKADDR*)&ClientAddr, &ClientAddrLen);
if(ClientSocket == INVALID_SOCKET)
{
cout << "接收客户端请求失败" << endl;
closesocket(ServerSocket);
WSACleanup();
return -1;
}
char ReceiveBuff[BUFSIZ];
char SendBuff[BUFSIZ];
while(true)
{
ZeroMemory(ReceiveBuff, BUFSIZ);
RetVal = recv(ClientSocket, ReceiveBuff, BUFSIZ, 0);
if(RetVal == SOCKET_ERROR)
{
cout << "接收数据失败" << endl;
closesocket(ServerSocket);
closesocket(ClientSocket);
WSACleanup();
return -1;
}
cout << "接收自客户端数据:" << ReceiveBuff << endl;
cout << "向客户端发送数据";
cin >> SendBuff;
send(ClientSocket, SendBuff, strlen(SendBuff), 0);
}
closesocket(ServerSocket);
closesocket(ClientSocket);
WSACleanup();
return 0;
}