VC SOCKET 压缩通信学习
Server................
// Server.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "zlib.h" #include <WinSock2.h> #include <Windows.h> #include <stdio.h> #pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"ZLib.lib") #define PORT 4000 #define LOCAL_HOST "192.168.1.100" DWORD WINAPI createproc(LPVOID lpParment) { SOCKET server = (SOCKET)lpParment; char recvbuf[MAX_PATH] = {0}; char sendbuf[MAX_PATH] = {0}; int err= 0; while (true) { unsigned long recvLen=sizeof(recvbuf),recLen=sizeof(recvbuf),stLen=sizeof(recvbuf); char unresvbuf[MAX_PATH] = {0}; // memset(recvbuf,0x00,sizeof(recvbuf)); err = recv(server,recvbuf,MAX_PATH,0); uncompress((BYTE*)unresvbuf,&recLen,(BYTE*)recvbuf,stLen); if (err == SOCKET_ERROR) { printf("Recv msg error\r\n"); break; }else { printf("Recv msg :%s\r\n",unresvbuf); } char com[MAX_PATH] = {0}; char strDst[1024]={0}; memset(sendbuf,0x00,sizeof(sendbuf)); gets(sendbuf); unsigned long srcLen=sizeof(sendbuf),bufLen=sizeof(sendbuf),dstLen=sizeof(sendbuf); compress((BYTE*)com,&bufLen,(BYTE*)sendbuf,srcLen); // printf("压缩长度:%d\r\n 压缩以后的内容:%s\r\n==============\r\n",bufLen,com); // uncompress((BYTE*)strDst,&dstLen,(BYTE*)com,bufLen); // printf("解压缩长度:%d\r\n解压缩以后的长度:%s\r\n",dstLen,strDst); err = send(server,com,(int)strlen(com),0); if (err == SOCKET_ERROR) { printf("Send msg error\r\n"); //return -1; break; }else { printf("Send Msg:%s\r\n",sendbuf); } } return 0; } int main(int argc, _TCHAR* argv[]) { WSADATA wsadata; SOCKET Serversocket,Clientsocket; struct sockaddr_in LocalAddr,clientaddr; HANDLE hThread; int err = 0; if (WSAStartup(MAKEWORD(2,2),&wsadata) != 0) { printf("Wsa init error\r\n"); return -1; } Serversocket = socket(AF_INET,SOCK_STREAM,0); if (Serversocket == INVALID_SOCKET) { printf("Serversocket Error\r\n"); return -1; } LocalAddr.sin_family = AF_INET; LocalAddr.sin_port = htons(PORT); LocalAddr .sin_addr.S_un.S_addr = inet_addr(LOCAL_HOST); memset(LocalAddr.sin_zero,0x00,8); err = bind(Serversocket,(struct sockaddr*)&LocalAddr,sizeof(LocalAddr)); if (err != 0) { printf("Bind Port error\r\n"); return -1; } err = listen(Serversocket,10); if (err != 0) { printf("Listen error\r\n"); return -1; } while (true) { /* char com[MAX_PATH] = {0}; char xxoo[MAX_PATH] = "Nimagebi...caonima"; unsigned long srcLen=sizeof(xxoo),bufLen=sizeof(xxoo),dstLen=sizeof(xxoo); char strDst[1024]={0}; compress((BYTE*)com,&bufLen,(BYTE*)xxoo,srcLen); printf("/nAfter Compressed Length:%d\r\nCompressed String:%s\r\n==============\r\n",bufLen,com); uncompress((BYTE*)strDst,&dstLen,(BYTE*)com,bufLen); printf("/nAfter UnCompressed Length:%d\r\nUnCompressed String:%s\r\n",dstLen,strDst); */ int addrlen = sizeof(clientaddr); Clientsocket = accept(Serversocket,(struct sockaddr*)&clientaddr,&addrlen); if (Clientsocket == INVALID_SOCKET) { printf("Clientsocket Error\r\n"); //return -1; break; } printf("Have client connect herf:%s ---> %d\r\n",inet_ntoa(clientaddr.sin_addr),clientaddr.sin_port); hThread = CreateThread(NULL,0,createproc,(LPVOID)Clientsocket,0,NULL); //新建立线程,如果不简历,相互通信几句就卡死了。奇怪。 if (hThread == NULL) { printf("New Create thread error\r\n"); break; }else { printf("New create Thread susccess\r\rn"); } CloseHandle(hThread); } closesocket(Clientsocket); closesocket(Serversocket); WSACleanup(); return 0; }
Client
// Client.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "zlib.h" #include<winsock2.h> #include <windows.h> #include <stdio.h> #pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"Zlib.lib") #define PORT 4000 #define Local_host "192.168.1.100" int main(int argc,char** argv) { WSADATA wsadata; SOCKET localsocket; sockaddr_in localaddr; char sendbuf[MAX_PATH] = {0}; char recvbuf[MAX_PATH] = {0}; int locallen = 0; int ret = 0; if (WSAStartup(MAKEWORD(1,1),&wsadata) != 0) { printf("wSA STARTinit error\r\n"); return -1; } localsocket = socket(AF_INET,SOCK_STREAM,0); if (localsocket == INVALID_SOCKET) { printf("localsocket error\r\b"); return -1; } localaddr.sin_addr.s_addr = inet_addr(Local_host); localaddr.sin_port = htons(PORT); localaddr.sin_family = AF_INET; ret = connect(localsocket,(sockaddr*)&localaddr,sizeof(localaddr)); if (ret == SOCKET_ERROR) { printf("Connect error\r\n"); return -1; } while (true) { memset(sendbuf,0,sizeof(sendbuf)); memset(recvbuf,0,sizeof(recvbuf)); char sendres[MAX_PATH] = {0}; char unsendres[MAX_PATH] = {0}; unsigned long SendLen=sizeof(sendbuf),sufLen=sizeof(sendbuf),stLen=sizeof(sendbuf); gets(sendbuf); compress((BYTE*)sendres,&sufLen,(BYTE*)sendbuf,stLen); // uncompress((BYTE*)unsendres,&stLen,(BYTE*)sendres,SendLen); // printf("解压缩以后的内容:%s",unsendres); ret = send(localsocket,sendres,(int)strlen(sendres),0); if (ret == INVALID_SOCKET) { printf("Ret send msg error\r\n"); break; } char com[MAX_PATH] = {0}; char strDst[MAX_PATH]={0}; ret = recv(localsocket,recvbuf,MAX_PATH,0); unsigned long srcLen=sizeof(recvbuf),bufLen=sizeof(recvbuf),dstLen=sizeof(recvbuf); uncompress(( BYTE*)com,&dstLen,( BYTE*)recvbuf,bufLen); if (ret == INVALID_SOCKET) { printf("Recv msg error\r\n"); break; }else { //printf("Recv buf is :%s\r\n",recvbuf); printf("Recv msg susscess :%s\r\n",com); } } closesocket(localsocket); WSACleanup(); return 0; }