Client:

#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h> #pragma comment(lib, "Ws2_32.lib")
#pragma warning(disable: 4996)
#define Port 6000 SOCKET Socket, Sub;
WSADATA Winsock;
sockaddr_in Addr;
sockaddr_in IncomingAddress;
int AddressLen = sizeof(IncomingAddress); BOOL send_function(BYTE* dib, int dib_size) { WSAStartup(MAKEWORD(, ), &Winsock); // Start Winsock if (LOBYTE(Winsock.wVersion) != || HIBYTE(Winsock.wVersion) != ) // Check version
{
WSACleanup();
return ;
} Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); ZeroMemory(&Addr, sizeof(Addr));
Addr.sin_family = AF_INET;
Addr.sin_port = htons(Port);
bind(Socket, (sockaddr*)&Addr, sizeof(Addr)); if (listen(Socket, ) == SOCKET_ERROR)
{
printf("listening error\n");
}
else
{
printf("listening ok\n");
} if (Sub = accept(Socket, (sockaddr*)&IncomingAddress, &AddressLen))
{
char* ClientIP = inet_ntoa(IncomingAddress.sin_addr);
int ClientPort = ntohs(IncomingAddress.sin_port);
printf("Client conncted!\n");
printf("IP: %s:%d\n", ClientIP, ClientPort); printf("Sending data .. \n"); char bufsize[];
sprintf(bufsize, "%d", dib_size); send(Sub, (char*)bufsize, , );
send(Sub, (char*)dib, dib_size, ); closesocket(Sub);
closesocket(Socket);
WSACleanup();
}
} int main()
{
//HWND hwnd = GetWindow(GetForegroundWindow(), GW_HWNDLAST);
//RECT rc;
//GetWindowRect(hwnd, &rc);
//HDC hdcScreen = GetDC(NULL);
//HDC hdc = CreateCompatibleDC(hdcScreen);
//HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen,
//rc.right - rc.left, rc.bottom - rc.top);
//SelectObject(hdc, hbmp);
//Print to memory hdc
//PrintWindow(hwnd, hdc, NULL);
HBITMAP Bitmap = (HBITMAP)LoadImage(NULL,
"panda.bmp", // file containing bitmap
IMAGE_BITMAP, // type = bitmap
, , // original size
LR_LOADFROMFILE);
auto hcopy = (HBITMAP)CopyImage(Bitmap, IMAGE_BITMAP, , , LR_CREATEDIBSECTION); BITMAP bm;
GetObject(hcopy, sizeof(bm), &bm); BITMAPINFOHEADER bi = { sizeof(bi) };
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biBitCount = bm.bmBitsPixel;
bi.biPlanes = bm.bmPlanes;
bi.biSizeImage = bm.bmWidthBytes * bm.bmHeight; LONG lineSize = bi.biWidth * bi.biBitCount / ;
BYTE* pLineData = new BYTE[lineSize];
BYTE* pStart;
BYTE* pEnd;
BYTE* pData = (BYTE*)bm.bmBits;
LONG lineStart = ;
LONG lineEnd = bi.biHeight - ;
while (lineStart < lineEnd)
{
pStart = pData + (lineStart * lineSize);
pEnd = pData + (lineEnd * lineSize);
// Swap the top with the bottom
memcpy(pLineData, pStart, lineSize);
memcpy(pStart, pEnd, lineSize);
memcpy(pEnd, pLineData, lineSize);
// Adjust the line index
lineStart++;
lineEnd--;
} int dib_size = sizeof(bi) + bi.biSizeImage;
memcpy(pData, &bi, sizeof(bi));
send_function(pData, dib_size); //cleanup
DeleteObject(hcopy);
delete pLineData;
getchar();
return ;
}

Server:

#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
#include <gdiplus.h> using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib") #pragma comment(lib, "Ws2_32.lib")
#pragma warning(disable: 4996) SOCKET Socket;
WSADATA Winsock;
sockaddr_in Addr;
int Addrlen = sizeof(Addr);
HBITMAP hbitmap; int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = ; // number of image encoders
UINT size = ; // size of the image encoder array in bytes ImageCodecInfo* pImageCodecInfo = NULL; GetImageEncodersSize(&num, &size);
if (size == )
return -; // Failure pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
return -; // Failure GetImageEncoders(num, size, pImageCodecInfo); for (UINT j = ; j < num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return ; // Success
}
}
free(pImageCodecInfo);
return -; // Failure
} void receive_function(BYTE* dib, int dib_size)
{
BITMAPINFOHEADER* bi = (BITMAPINFOHEADER*)dib;
BYTE* bits = dib + sizeof(bi);
hbitmap = CreateBitmap(bi->biWidth, bi->biHeight, bi->biPlanes, bi->biBitCount, bits); /*OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hbitmap);
CloseClipboard();
printf("Clipboard set!");*/
} int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); WSAStartup(MAKEWORD(, ), &Winsock); // Start Winsock if (LOBYTE(Winsock.wVersion) != || HIBYTE(Winsock.wVersion) != ) // Check version
{
WSACleanup();
return ;
} Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); ZeroMemory(&Addr, sizeof(Addr)); // clear the struct
Addr.sin_family = AF_INET; // set the address family
Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
Addr.sin_port = htons(); // set the port if (connect(Socket, (sockaddr*)&Addr, sizeof(Addr)) < )
{
printf("Connection failed !\n");
getchar();
return ;
} printf("Connection successful !\n"); printf("Receiving data .. \n"); int dib_size = ;
char Filesize[]; if (recv(Socket, Filesize, , )) // File size
{
dib_size = atoi(Filesize);
}
printf("File size: %d\n", dib_size); BYTE* dib = new BYTE[dib_size];
if (recv(Socket, (char*)dib, dib_size, ))
{
receive_function(dib, dib_size);
}
CLSID myClsId;
int retVal = GetEncoderClsid(L"image/bmp", &myClsId);
Bitmap *image = new Bitmap(hbitmap, NULL);
image->Save(L"output.bmp", &myClsId, NULL);
delete image; GdiplusShutdown(gdiplusToken); getchar();
return ;
}

C++使用socket传输图片的更多相关文章

  1. socket传输图片用shutdownoutput()之后无法继续传输数据

    socket传输图片用shutdownoutput()之后无法继续传输数据前言java的socket是一个全双工套接字,任何的输入流或输出流的close()都会造成Socket关闭.使用java服务器 ...

  2. Android网络(3):HttpClient作client,Tomcat Servlet作server的交互演示样例

    前面相继介绍了Android网络编程里的Socket传输图片.HttpURLConnection,今天看HttpClient. 第一部分:JavaEE版的Eclipse配置Tomcat [备注:开发后 ...

  3. java socket通信-传输文件图片--传输图片

    ClientTcpSend.java   client发送类 package com.yjf.test; import java.io.DataOutputStream; import java.io ...

  4. PC端使用opencv获取webcam,通过socket把Mat图像传输到android手机端

    demo效果图: PC端 android端 大体流程 android端是服务器端,绑定IP和端口,监听来自PC端的连接, pc端通过socket与服务器andorid端传输图片. 主要代码 andro ...

  5. C# Socket 简易的图片传输

    关于网络的数据传输我就是个小白,所以今天学习一下简易的Socket图片传输. 客户端和服务器的连接咱们上次已经学过了,咱们先从简易的文件传输入手.下面开始代码分析了. Server.cs using ...

  6. 基于 GCDAsyncSocket,简单实现类似《你猜我画》的 socket 数据传输

    一.前言 Socket Socket 是对 TCP/IP 协议的封装,其中IP协议对应为网络层,TCP 协议对应为传输层,而我们常用的HTTP协议,是位于应用层,在七层模型中HTTP协议是基于 TCP ...

  7. PC_android通信之传输图片并显示在手机端【转】

    从服务器 (PC 端 ) 发送图片到客户端 (android 手机端 ) ,并在手机页面上显示该图片.(注:本文旨在实现功能,并未考虑效率,有待后续跟进.) 1.服务器端 int port=9090; ...

  8. Socket 传一幅图片给另一个终端

    练习Socket传文件,先添加一个组件,简化socket发送和接收文件, 获取IP和端口的类 public static class AddressHelper { /// <summary&g ...

  9. socket读写返回值的处理

    在调用socket读写函数read(),write()时,都会有返回值.如果没有正确处理返回值,就可能引入一些问题 总结了以下几点 1当read()或者write()函数返回值大于0时,表示实际从缓冲 ...

随机推荐

  1. RN 图片处理 resizeMode

    Image组件必须在样式中声明图片的宽和高.如果没有声明,则图片将不会被呈现在界面上.    我们一般将Image定义的宽和高乘以当前运行环境的像素密度称为Image的实际宽高. 当Image的实际宽 ...

  2. Vue组件中的data属性

    Vue中的data属性专门用来以对象方式存放数据,它有两种用法. var vm=new Vue({ data:{a:1,b:2,}, }) var vm=new Vue({ data(){return ...

  3. shell一文入门通

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/hebtu666/article/deta ...

  4. appium环境搭建步骤

    前提条件是:搭建selenium环境(侵权删) 1.安装jdk,配置环境变量: JAVA_HOME C:\Program Files\Java\jdk1.8.0_60(jdk的存放路径) CLASSP ...

  5. SQL注入-预防

    输入验证: 检查用户输入的合法性,确信输入的内容只包含合法的数据.数据检查应当在客户端和服务器端都执行服务器端验证,是为了弥补客户端验证机制脆弱的安全性. 输入验证最好使用“白名单”校验的方式. 输入 ...

  6. 用grok拆分java日志

    1.假设一行日志内容如下: [root@VM_0_92_centos opt]# cat error.log -- ::,[ERROR ajp-nio--exec-](cn.com.al1.compo ...

  7. 编译FFMPEG错误对策

    在MINGW32下编译ffmpeg-2.1.1.tar.bz2 step1: ./configure   --prefix=/home/Administrator/install --extra-cf ...

  8. (已解决)Could not open '/var/lib/nova/mnt/*/volume-*': Permission denied

    [问题描述] 创建boot_from_volume的虚机时,磁盘后端为NFS,创建失败. [错误日志] nova-compute模块 Could not open '/var/lib/nova/mnt ...

  9. [Python3] 041 文件 持久化

    目录 文件 持久化 1. pickle 1.1 例子1 1.2 例子2 1.3 注意 2. shelve 2.1 举例 2.2 特性 2.3 强制写回 2.4 使用 with 管理上下文环境 文件 持 ...

  10. js中‘0’到底是 true 还是 false

    if ('0') alert("'0' is true");  if ('0' == false) alert("'0' is false");结果是,两次都 ...