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打开通信通道,告诉本地机器,愿意在该通道上接受客户请求——监听,等待客户请求——接受请求,创建专用链接进行读写——处理完毕,关闭专用链接——关闭通信通道(当然其中监听到关闭专用链接可以重复 ...
随机推荐
- X明X源面试题《二》
一.解释5种访问修饰符答:public-访问不受限制.private-访问范围为它所属的类.protected-访问范围为它所属的类或从该类派生的类.internal-访问范围为当前程序集.prote ...
- 关于java后台如何接收xml格式的数据
业务场景:用户发送下单请求,格式为xml格式,服务器接收数据完成下单,并返回结果给客户. 请求格式: <request> <head> <sign></sig ...
- 内核源码阅读vim+cscope+ctags+taglist
杜斌博客:http://blog.db89.org/kernel-source-read-vim-cscope-ctags-taglist/ 武特博客:http://edsionte.com/tech ...
- HTML页面布局
接下来的下面代码,只是给了一个大的前端编写布局,如果你已经是牛人了,就当没看到,如果是一些初学者,不妨拿去用用,里面也写了一些常用的css样式,现在虽然有很多牛逼的前段框架,用起来也非常得心应手,但是 ...
- Linux Centos使用脚本安装Docker
1.使用sudo或root权限登录Centos. 2.确保yum包更新到最新. sudo yum update 3.执行Docker安装脚本 # 执行这个脚本会添加docker.repo源并安装Doc ...
- return和yield的区别
# return 返回给调用者值,并结束此函数.#yiled 返回给调用者值,并将指针停留着当前位置.
- matlab学习笔记之五种常见的图形绘制功能
分类: 离散数据图形绘制 函数图形绘制 网格图形绘制 曲面图形绘制 特殊图形绘制 本文重点介绍matlab五种图形绘制方法的后三种. 一.网格图形绘制 以绘制函数z=f(x,y)三维网格图为例,下面为 ...
- Insert Buffering
14.5.13.4 Insert Buffering Database applications often insert new rows in the ascending order of the ...
- hexo+yilia页脚添加总访问量
个人博客:https://www.yuehan.online 现在博客:http://www.wangyurui.top 脚本方法使用不蒜子计数 安装脚本 要使用不蒜子(官网:http://busua ...
- 印象笔记Mac端快捷键