本文主要是介绍Socket 基础 非阻塞式TCP socket,实现文件传输jpg,mov等格式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Socket 基础
非阻塞式TCP socket
实现文件传输,
实测 传输5M的jpg , 30M的 NEF(单反原图) 以及 1G以上的mov文件,均正常接收
客户端可多开,服务器多线程实现服务器一对多
服务器部分:
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <stdio.h>
#include <thread>
#define BUF_SIZE 1024
#include <iostream>
void connThread(SOCKET sock)
{
char chBuf[1024];
int nRes = 0;
std::FILE* f;
errno_t err;
err = fopen_s(&f, "G:\\qzher_3249.MOV", "rb");
//err = fopen_s(&f, "G:\\qzher_3165.NEF", "rb");
//err = fopen_s(&f, "G:\\qzher_3052.jpg", "rb");
UINT64 nFileLen = 0;
if (err == 0)
{
fseek(f, 0L, SEEK_END);
nFileLen = ftell(f);
printf("文件大小为:%d 字节\n", nFileLen);
fseek(f, 0, SEEK_SET);
}
else
{
printf("The file was not opened\n");
return;
}
UINT64 nRead = 0;
UINT64 nSendLen = 0;
clock_t start, finish;
double dDuration = 0.0;
start = clock();
int i = 0;
while (true)
{
ZeroMemory(chBuf, BUF_SIZE);
if ((nFileLen – nSendLen) >= BUF_SIZE )
{
nRead = fread(chBuf, sizeof(char), BUF_SIZE, f);
//nRemainLen -= BUF_SIZE;
}
else // 剩下的
{
if (nFileLen <= nSendLen)
{
break;
}
nRead = fread(chBuf, sizeof(char), nFileLen – nSendLen, f);
//nSendLen = 0;
}
for (i = 0; i < nRead
这篇关于Socket 基础 非阻塞式TCP socket,实现文件传输jpg,mov等格式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!