LNK2019错误解决不了,求解答

刘洋
  • 1
新手上路,请多包涵

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);

}

评论
阅读 238
撰写回答

登录后参与交流、获取后续更新提醒

宣传栏