Server端接收到Client端信息后不会返回给Client端
// TCPEchoServer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <WinSock2.h>
#include <strsafe.h>
#pragma comment(lib, "Ws2_32.lib")
#define GRS_ALLOC(sz) HeapAlloc(GetProcessHeap(), 0 ,sz)
#define GRS_CALLOC(sz) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sz)
#define GRS_CREALLOC(p, sz) HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, p, sz)
#define GRS_SAFEFREE(p) if(NULL != p){HeapFree(GetProcessHeap(), 0, p); p = NULL;}
#define GRS_USEPRINTF() TCHAR pBuf[1024] = {}; char pBufA[1024] = {};
#define GRS_PRINTF(...)\
StringCchPrintf(pBuf, 1024, __VA_ARGS__);\
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), pBuf, lstrlen(pBuf), NULL, NULL);
#define GRS_PRINTFA(...)\
StringCchPrintfA(pBufA, 1024, __VA_ARGS__);\
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), pBufA, lstrlenA(pBufA), NULL, NULL);
#define SOCK_VERH 2
#define SOCK_VERL 2
#define GRS_SERVER_PORT 5634
int _tmain(int argc, _TCHAR* argv[])
{
GRS_USEPRINTF();
WORD wVer = MAKEWORD(SOCK_VERH, SOCK_VERL);
WSADATA wd;
int err = ::WSAStartup(wVer, &wd); //因为多个WinSock版本库可能共存于一个系统,所以应用程序必须明确指定加载合适的WinSock版本库,这个过程称为WinSock初始化,一般用WSAStartup
//高字节副版本号,低字节主版本号
if(0 != err)
{
GRS_PRINTF(_T("无法初始化Scoket2环境,错误码为:%d!\n"), WSAGetLastError());
return 1;
}
if(LOBYTE(wd.wVersion) != SOCK_VERH || HIBYTE(wd.wVersion) != SOCK_VERL)
{
GRS_PRINTF(_T("无法初始化%d.%d版本环境!\n"), SOCK_VERH, SOCK_VERL);
::WSACleanup();
return 2;
}
GRS_PRINTF(_T("WinSock库初始化成功!\n\t当前系统中支持最高的WinSock版本为%d.%d\n\t当前应用程序使用的WinSock版本为%d.%d\n"),
LOBYTE(wd.wHighVersion), HIBYTE(wd.wHighVersion),
LOBYTE(wd.wVersion), HIBYTE(wd.wVersion));
SOCKET skServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKET skClient = INVALID_SOCKET;
SOCKADDR_IN saClient = {};
int isaLen = sizeof(SOCKADDR_IN);
VOID* pskBuf = NULL;
VOID* pskBufTmp = NULL;
int iBufLen = 1024;
int iBufLenTmp = 0;
int iRecv = 0;
SOCKADDR_IN sa = {AF_INET};
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = htons(GRS_SERVER_PORT);
if(0 != bind(skServer, (SOCKADDR*)&sa, sizeof(SOCKADDR_IN)))
{
GRS_PRINTF(_T("绑定到指定端口出错!\n"));
goto CLEAN_UP;
}
if(0 != listen(skServer, SOMAXCONN))
{
GRS_PRINTF(_T("SOCKET进入监听模式出错!\n"));
goto CLEAN_UP;
}
while(INVALID_SOCKET != (skClient=accept(skServer, (SOCKADDR*)&saClient, &isaLen)))
{
GRS_PRINTFA("客户端[%s : %u]连接进来了.....\n", inet_ntoa(saClient.sin_addr), ntohs(saClient.sin_port));
GRS_PRINTF(_T("\t开始接受客户端数据......\n"));
pskBuf = GRS_CALLOC(iBufLen);
pskBufTmp = pskBuf;
do
{
iRecv = recv(skClient, (char*)pskBufTmp, iBufLen-iBufLenTmp, 0);
if(SOCKET_ERROR == iRecv || 0 == iRecv)
{
break;
}
if(iRecv >= (iBufLen - iBufLenTmp))
{
iBufLenTmp = iBufLen;
iBufLen += 1024;
pskBuf = GRS_CREALLOC(pskBuf, iBufLen);
pskBufTmp = (void*)((BYTE*)pskBuf + iBufLenTmp);
continue;
}
}
while (1);
if(SOCKET_ERROR != iRecv && 0 != iRecv)
{
GRS_PRINTF(_T("\t接收客户端数据成功,开始返回......\n"));
send(skClient, (const char*)pskBuf, iBufLen, 0);
}
else
{
GRS_PRINTF(_T("\t接收客户端数据失败,关闭连接......\n"));
}
GRS_SAFEFREE(pskBuf);
pskBufTmp = NULL;
iBufLenTmp = 0;
iBufLen = 1024;
shutdown(skClient, SD_BOTH);
closesocket(skClient);
}
CLEAN_UP:
_tsystem(_T("PASUE"));
if(INVALID_SOCKET != skServer)
{
closesocket(skServer);
}
::WSACleanup();
return 0;
} //Server端的代码
// TCPEchoClient.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <strsafe.h>
#include <WinSock2.h>
#pragma comment(lib, "Ws2_32.lib")
#define GRS_ALLOC(sz) HeapAlloc(GetProcessHeap(), 0 ,sz)
#define GRS_CALLOC(sz) HeapAlloc(GetProcessheap(), HEAP_ZERO_MEMORY, sz)
#define GRS_CREALLOC(p, sz) HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, p, sz)
#define GRS_SAFEFREE(p) if(NULL != p){HeapFree(GetProcessHeap(), 0, p); p = NULL;}
#define GRS_USEPRINTF() TCHAR pBuf[1024] = {}; char pBufA[1024] = {};
#define GRS_PRINTF(...)\
StringCchPrintf(pBuf, 1024, __VA_ARGS__);\
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), pBuf, lstrlen(pBuf), NULL, NULL);
#define GRS_PRINTFA(...)\
StringCchPrintfA(pBufA, 1024, __VA_ARGS__);\
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), pBufA, lstrlenA(pBufA), NULL, NULL);
#define SOCK_VERH 2
#define SOCK_VERL 2
#define GRS_SERVER_PORT 5634
int _tmain(int argc, _TCHAR* argv[])
{
GRS_USEPRINTF();
WORD wVer = MAKEWORD(SOCK_VERH, SOCK_VERL);
WSADATA wd;
int err = ::WSAStartup(wVer, &wd); //因为多个WinSock版本库可能共存于一个系统,所以应用程序必须明确指定加载合适的WinSock版本库,这个过程称为WinSock初始化,一般用WSAStartup
//高字节副版本号,低字节主版本号
if(0 != err)
{
GRS_PRINTF(_T("无法初始化Scoket2环境,错误码为:%d!\n"), WSAGetLastError());
return 1;
}
if(LOBYTE(wd.wVersion) != SOCK_VERH || HIBYTE(wd.wVersion) != SOCK_VERL)
{
GRS_PRINTF(_T("无法初始化%d.%d版本环境!\n"), SOCK_VERH, SOCK_VERL);
::WSACleanup();
return 2;
}
GRS_PRINTF(_T("WinSock库初始化成功!\n\t当前系统中支持最高的WinSock版本为%d.%d\n\t当前应用程序使用的WinSock版本为%d.%d\n"),
LOBYTE(wd.wHighVersion), HIBYTE(wd.wHighVersion),
LOBYTE(wd.wVersion), HIBYTE(wd.wVersion));
SOCKET skClient = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
SOCKADDR_IN sa = {AF_INET};
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
sa.sin_port = htons(GRS_SERVER_PORT);
WCHAR pszSendMsg[] = _T("Hello Internetssssssssssssssssssssssssss!");
size_t szLen = 0;
StringCchLength(pszSendMsg, 30, &szLen);
WCHAR pszRecvBuf[1024] = {};
if(0 != connect(skClient, (SOCKADDR*)&sa, sizeof(SOCKADDR_IN)))
{
GRS_PRINTF(_T("连接服务器出错......\n"));
goto CLEAN_UP;
}
send(skClient, (char*)pszSendMsg, szLen * sizeof(WCHAR), 0);
recv(skClient, (char*)pszRecvBuf, 1024 * sizeof(WCHAR), 0);
GRS_PRINTF(_T("ECHO:%s\n"), pszRecvBuf);
CLEAN_UP:
if(INVALID_SOCKET != skClient)
{
shutdown(skClient, SD_BOTH);
closesocket(skClient);
}
_tsystem(_T("PASUE"));
::WSACleanup();
return 0;
} //Client端的代码
Server端接收到Client端信息后不会返回给Client端,