LNK2019错误解决不了,求解答

发布于 2022-09-12 12:59:08 字数 2703 浏览 28 评论 0

include "Winsock.h"

include<stdlib.h>

include "stdio.h"

include <iostream>

include<thread>

pragma comment(lib, "wsock32.lib")

define RECV_PORT 2494 //set-up server port

define ServerIPAddr 16 //one more than what you see

define predix "F:\vs_repos\GetCameraImageFrame\store\"

SOCKET Server;
sockaddr_in ServerAddr;
char server_IP[ServerIPAddr] = "192.168.1.4"; //server_IP
//char server_IP[ServerIPAddr] = "127.0.0.1";

using namespace std;

DWORD StartSock() //start-up socket project
{

WSADATA WSAData;
if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)    // load  winsock2.0
    return -1;
return 1;

}

//create socket
DWORD CreateSocket()
{ //the socket function will return a socket object; SOCK_STREAM realted TCP

Server = socket(AF_INET, SOCK_STREAM, 0);
if (Server == SOCKET_ERROR)
{
    WSACleanup();
    return -1;
}
return 1;

}

DWORD CallServer() //request connection
{

//set address structure
ServerAddr.sin_family = AF_INET;        //Ipv4
ServerAddr.sin_addr.s_addr = inet_addr(server_IP);
ServerAddr.sin_port = htons(RECV_PORT);
if (connect(Server, (struct  sockaddr*)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
{
    WSACleanup();
    closesocket(Server);
    return -1;
}
return 1;

}

int SendFile(char* filename)
{

define BuffLen 800*1024

FILE * pFile;
char sbuff[BuffLen] = {};
string str1 = predix + string(filename);
fopen_s(&pFile, str1.c_str(), "rb+");
int DataLength;
if (pFile)
{
    int j = send(Server, filename, strlen(filename) + 1, 1);
    Sleep(100);
    while (true)
    {
        memset(sbuff, 0, BuffLen);
        DataLength = fread(sbuff, 1, BuffLen, pFile);
        cout << DataLength << endl;
        if (DataLength < BuffLen)    //based on the principle: logical data can be storaged continuously
        {
            int i = send(Server, sbuff, DataLength + 1, 1);
            break;
        }
        else
        {
            send(Server, sbuff, DataLength, 1);
        }
    }
    return 1;
}
else
{
    WSACleanup();
    closesocket(Server);
    return -1;
}

}

int client(char* filename) //the function will send the photo file in the way of data stream
{

if (-1 == StartSock())
    return -1;
if (-1 == CreateSocket())
    return -1;
if (-1 == CallServer())
    return -1;
if (-1 == SendFile(filename))
    return -1;
Sleep(1000);
WSACleanup();
closesocket(Server);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文