LNK2019错误解决不了,求解答
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论