• 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 "); break; }else { printf("Recv msg :%s ",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 压缩以后的内容:%s ============== ",bufLen,com); // uncompress((BYTE*)strDst,&dstLen,(BYTE*)com,bufLen); // printf("解压缩长度:%d 解压缩以后的长度:%s ",dstLen,strDst); err = send(server,com,(int)strlen(com),0); if (err == SOCKET_ERROR) { printf("Send msg error "); //return -1; break; }else { printf("Send Msg:%s ",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 "); return -1; } Serversocket = socket(AF_INET,SOCK_STREAM,0); if (Serversocket == INVALID_SOCKET) { printf("Serversocket Error "); 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 "); return -1; } err = listen(Serversocket,10); if (err != 0) { printf("Listen error "); 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 Compressed String:%s ============== ",bufLen,com); uncompress((BYTE*)strDst,&dstLen,(BYTE*)com,bufLen); printf("/nAfter UnCompressed Length:%d UnCompressed String:%s ",dstLen,strDst); */ int addrlen = sizeof(clientaddr); Clientsocket = accept(Serversocket,(struct sockaddr*)&clientaddr,&addrlen); if (Clientsocket == INVALID_SOCKET) { printf("Clientsocket Error "); //return -1; break; } printf("Have client connect herf:%s ---> %d ",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 "); break; }else { printf("New create Thread susccess n"); } 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
    ");
            return -1;
        }
        localsocket = socket(AF_INET,SOCK_STREAM,0);
        if (localsocket == INVALID_SOCKET)
        {
            printf("localsocket error
    ");
            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
    ");
            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
    ");
                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
    ");
                break;
    
            }else
            {
                //printf("Recv buf is :%s
    ",recvbuf);
                printf("Recv msg susscess :%s
    ",com);
            }
    
        }
        closesocket(localsocket);
        WSACleanup();
        return 0;
    }
  • 相关阅读:
    可变长参数列表
    《Android深入透析》之广播(Broadcast)
    android设计模式资源集合
    Android内存性能优化
    Java 对象的生命周期
    Android 打印java堆栈的几种方法
    Android下打印调试堆栈方法
    UML类图几种关系的总结
    【转】Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)
    查看Android设备的CPU架构信息
  • 原文地址:https://www.cnblogs.com/killbit/p/4265984.html
Copyright © 2020-2023  润新知