Windows Socket(基于TCP连接的)编程的程序代码简单结构.
服务端:
1. 初始化Windows Socket库.
2. 创建Socket. (socket)
3. 绑定Socket. (bind: socket绑定本机的一个IP地址和端口)
4. 监听.(listen: 设置监听, 同时设置监听数量)
5. Accept. (accept: 服务端每接收到一个客户端的Socket,则创建一个线程, 满足一个服务端连接多个客户端.
6. 接收、发送数据.(有很多模型选择)
客户端
1. 初始化Windows Socket库.
2、创建Socket.(socket)
3、连接Socket.(connect: 连接服务端的IP地址和端口)
4、接收、发送数据.(有很多模型选择)
这是一个最简答的模型了: 就是Server与Client建立通信并可以收发数据的最简单模型.
//
// Server
// Server.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#pragma comment(lib,"ws2_32.lib")
DWORD WINAPI ClientThread(LPVOID lpParameter)
{
SOCKET clientSocket = (SOCKET)lpParameter;
int nRet = 0;
char chRecvBuffer[MAX_PATH];
while(true)
{
memset(chRecvBuffer, 0x00, sizeof(chRecvBuffer));
nRet = recv(clientSocket, chRecvBuffer, MAX_PATH, 0);
if(0 == nRet || SOCKET_ERROR == nRet)
{
std::cout << "客户端退出!" << std::endl;
break;
}
std::cout<< "接收到客户信息为:" << chRecvBuffer << std::endl;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsAData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in localAddr, clientAddr;
int nRet = 0;
int nAddrLen = 0;
HANDLE hThread = NULL;
// 初始化Win Socket
if(0 != WSAStartup(MAKEWORD(2,2), &wsAData))
{
std::cout << "Init Windows Socket Failed. "<< GetLastError() << std::endl;
return -1;
}
// 创建Socket
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == serverSocket)
{
std::cout << "Create Socket Failed:" << GetLastError() << std::endl;
return -1;
}
memset(&localAddr, 0x00, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);; // 这你也可以显式的指定你的本地的IP地址(例如你的电脑有多个IP地址)
// localAddr.sin_addr.s_addr = inet_addr("172.20.3.30");
localAddr.sin_port = htons(6123);
// 绑定地址和端口
nRet = bind(serverSocket, (struct sockaddr*)&localAddr, sizeof(localAddr));
if(0 != nRet)
{
std::cout << "Bind Socket Failed:" << GetLastError() << std::endl;
return -1;
}
// 设置监听
nRet = listen(serverSocket, 10);
if(0 != nRet)
{
std::cout << "listen Socket Failed::" << GetLastError() << std::endl;
return -1;
}
std::cout << "Server Begin Accept." << std::endl;
while(true)
{
nAddrLen = sizeof(clientAddr);
// accept
clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &nAddrLen);
if(INVALID_SOCKET == clientSocket)
{
std::cout << "Accept Failed:" << GetLastError() << std::endl;
break;
}
std::cout << "New Client:" << inet_ntoa(clientAddr.sin_addr) << ":" << clientAddr.sin_port << std::endl;
hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)clientSocket, 0, NULL);
if(NULL == hThread)
{
std::cout << "Create Thread Failed!" << std::endl;
break;
}
}
// 这里要销毁你的所有client的资源
// closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 0;
}
//
// Client
//
// Client.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#pragma comment(lib,"ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsAData;
SOCKET clientSocket;
struct sockaddr_in serverAddr;
int nRet = 0;
char chSendBuffer[MAX_PATH];
// 初始化Win Sock
if(0 != WSAStartup(MAKEWORD(2,2), &wsAData))
{
std::cout << "Init Windows Socket Failed::" << GetLastError() << std::endl;
return -1;
}
// 创建Socket
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(INVALID_SOCKET == clientSocket)
{
std::cout <<"Create Socket Failed:"<< GetLastError() << std::endl;
return -1;
}
memset(&serverAddr, 0x00, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("172.20.3.30");
serverAddr.sin_port = htons(6123);
// 连接服务器
nRet = connect(clientSocket,(struct sockaddr*)&serverAddr, sizeof(serverAddr));
if(SOCKET_ERROR == nRet)
{
std::cout << "Connect Error:" << GetLastError() << std::endl;
return -1;
}
else
{
std::cout << "Connect Successful." << std::endl;
}
while ( true )
{
std::cin.getline(chSendBuffer, sizeof(chSendBuffer));
nRet = send(clientSocket, chSendBuffer, (int)strlen(chSendBuffer), 0);
if(SOCKET_ERROR == nRet)
{
std::cout << "Send Data Error:" << GetLastError() << std::endl;
break;
}
}
closesocket(clientSocket);
WSACleanup();
return 0;
}
//
转自: http://www.cppblog.com/bujiwu/archive/2009/01/11/71707.aspx