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] = {};
char sendbuf[MAX_PATH] = {};
int err= ; while (true)
{
unsigned long recvLen=sizeof(recvbuf),recLen=sizeof(recvbuf),stLen=sizeof(recvbuf);
char unresvbuf[MAX_PATH] = {};
// memset(recvbuf,0x00,sizeof(recvbuf));
err = recv(server,recvbuf,MAX_PATH,); 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] = {};
char strDst[]={};
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),);
if (err == SOCKET_ERROR)
{
printf("Send msg error\r\n");
//return -1;
break;
}else
{
printf("Send Msg:%s\r\n",sendbuf);
}
}
return ;
} int main(int argc, _TCHAR* argv[])
{
WSADATA wsadata;
SOCKET Serversocket,Clientsocket;
struct sockaddr_in LocalAddr,clientaddr;
HANDLE hThread;
int err = ;
if (WSAStartup(MAKEWORD(,),&wsadata) != )
{
printf("Wsa init error\r\n");
return -;
}
Serversocket = socket(AF_INET,SOCK_STREAM,);
if (Serversocket == INVALID_SOCKET)
{
printf("Serversocket Error\r\n");
return -;
}
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,); err = bind(Serversocket,(struct sockaddr*)&LocalAddr,sizeof(LocalAddr));
if (err != )
{
printf("Bind Port error\r\n");
return -;
} err = listen(Serversocket,);
if (err != )
{
printf("Listen error\r\n");
return -;
} 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,,createproc,(LPVOID)Clientsocket,,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 ;
}
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] = {};
char recvbuf[MAX_PATH] = {};
int locallen = ;
int ret = ; if (WSAStartup(MAKEWORD(,),&wsadata) != )
{
printf("wSA STARTinit error\r\n");
return -;
}
localsocket = socket(AF_INET,SOCK_STREAM,);
if (localsocket == INVALID_SOCKET)
{
printf("localsocket error\r\b");
return -;
} 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 -;
} while (true)
{
memset(sendbuf,,sizeof(sendbuf));
memset(recvbuf,,sizeof(recvbuf));
char sendres[MAX_PATH] = {};
char unsendres[MAX_PATH] = {};
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),);
if (ret == INVALID_SOCKET)
{
printf("Ret send msg error\r\n");
break;
} char com[MAX_PATH] = {};
char strDst[MAX_PATH]={};
ret = recv(localsocket,recvbuf,MAX_PATH,);
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 ;
}
VC SOCKET 压缩通信学习的更多相关文章
- 孙鑫视频VC++深入详解学习笔记
孙鑫视频VC++深入详解学习笔记 VC++深入详解学习笔记 Lesson1: Windows程序运行原理及程序编写流程 Lesson2: 掌握C++基本语法 Lesson3: MFC框架程序剖析 Le ...
- Dubbo底层采用Socket进行通信详解
由于Dubbo底层采用Socket进行通信,自己对通信理理论也不是很清楚,所以顺便把通信的知识也学习一下. n 通信理论 计算机与外界的信息交换称为通信.基本的通信方法有并行通信和串行通信两种. 1 ...
- 使用Socket进行通信
客户端通常可使用Socket的构造器来连接到指定服务器,Socket通常可使用如下两个构造器. Socket(lnetAddress/String remoteAddress , int port ...
- java socket报文通信(一)socket的建立
java socket报文通信(一) socket的建立 今天来和大家分享一下java中如何使用socket进行通信.先来啰嗦两句,看看Tcp/ip和udp: TCP是Transfer Contro ...
- java socket线程通信
关于socket线程通信的一些知识整理 一般我们需要要让两台机子进行通信,需要创建一个Server 类,一个Client类,还需要创建一个线程类 server public class Server ...
- C# 通信学习笔记
C# 通信学习笔记 DNS 是域名系统 (Domain Name System) 的缩写,是因特网的一项核心服务,它作为可以将域名和IP地址相互映射的一个分布式数据库,能够使人更方便的访问互联网,而不 ...
- 《java入门第一季》之Socket编程通信和TCP协议通信图解
Socket编程通信图解原理: TCP协议通信图解
- socket 编程通信实例
socket 编程通信实例:TCPserver: , ServerThread, ; WSADATA wsaData; ,), ; } ; } } ; g ...
- Socket通讯简易学习
Socket打开通信通道,告诉本地机器,愿意在该通道上接受客户请求——监听,等待客户请求——接受请求,创建专用链接进行读写——处理完毕,关闭专用链接——关闭通信通道(当然其中监听到关闭专用链接可以重复 ...
随机推荐
- MySQL-库的操作
05-库的操作 本节重点: 掌握库的增删改查 一.系统数据库 执行如下命令,查看系统库 show databases; nformation_schema: 虚拟库,不占用磁盘空间,存储的是数 ...
- n++ ++n
n = 100 + m++ n = 100 +m; ++m n = 100 + ++m; ++m; n = 100 +m ;
- windowsphone8.1学习笔记之磁贴
磁贴,wp系统独有.个人觉得还是蛮给力的. 好了废话不多说,我们来创建自己的辅助磁贴.创建磁贴是通过SecondaryTile对象来的,所以先说说该对象的一些重要属性. Arguments属性:传值用 ...
- jQuery改变CSS使DIV显示
HTML: <div id="mazey" style="display:none;">www.mazey.net</div> jQue ...
- ftp 协议分析
File Transfer Protocol(文件传输协议) 使用SOCKET实现 FTP的客户端协议规则: .h #pragma once #include <string> #incl ...
- selenium 用autoIT上传下载文件
一.下载安装AutoIT 下载并安装AutoIT,下载链接:https://www.autoitscript.com/site/autoit/ AutoIT安装成功后,可以在开始菜单下看到AutoIT ...
- K-M-P
分段K-M-P 只需要记录上一段已经匹配了的长度,再接着从匹配下一段就可以了.
- shuit模块
shuit模块 #高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中,可以部分内容 def copyf ...
- Verilog HDL设计规范及经验谈(转载)
1. 规范很重要 工作过的朋友肯定知道,公司里是很强调规范的,特别是对于大的设计(无论软件还是硬件),不按照规范走几乎是不可实现的.逻辑设计也是这样:如果不按规范做的话,过一个月后调试时发现 ...
- The Maximum Unreachable Node Set 【17南宁区域赛】 【二分匹配】
题目链接 https://nanti.jisuanke.com/t/19979 题意 给出n个点 m 条边 求选出最大的点数使得这个点集之间 任意两点不可达 题目中给的边是有向边 思路 这道题 实际上 ...