函数不能嵌套,那么如何在C++中执行此操作
function cannot be nested then how to do this in C++?
这是我的程序
#define WIN32_LEAN_AND_MEAN
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <process.h>
#include<atlstr.h>
#include<Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;
#define DEFAULT_PORT "100"
class Base
{
public :
struct addrinfo addr_;
unsigned short port_;
CString hostname_;
CRITICAL_SECTION sect_;
HANDLE threadHandle_;
HANDLE threadHandle1_;
bool connected_;
public :
SOCKET sock_;
A()
{
}
public:
virtual int ConnectToMachine(void)=0; //This will make socket connection with the machine.
virtual int SendRequest(SOCKET sock)=0; //This will send the request to the machine.
virtual char* ReceiveResponse(SOCKET sock)=0; //This will receive the response from the machine that is binary data.
static DWORD WINAPI ServerConnectThread(Base *my);
static UINT ThreadFunc(LPVOID param) ;
static UINT receive();
static DWORD WINAPI startReceive(LPVOID param){
Base *_this=(Base*)param;
_this->receive();
return 0;
}
};
#define DEFAULT_BUFLEN 512
class Derived :public Base
{
public:
int ConnectToMachine(void)
{
int conResult,iResult;
struct addrinfo *result = NULL,*ptr = NULL;
u_long iMode = 0;
DWORD nTimeout = 5000; // 5 seconds
int port=22;
WSADATA wsaData;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed with error: %dn", iResult);
}
ZeroMemory( &addr_, sizeof(addr_) );
addr_.ai_family = AF_UNSPEC;
addr_.ai_socktype = SOCK_STREAM;
addr_.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
conResult = getaddrinfo("192.168.1.7", DEFAULT_PORT, &addr_, &result);
if ( conResult != 0 ) {
printf("getaddrinfo failed with error: %dn", conResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
sock_ = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (sock_ == INVALID_SOCKET) {
printf("socket failed with error: %ldn", WSAGetLastError());
WSACleanup();
return 1;
}
conResult = ioctlsocket(sock_, FIONBIO, &iMode);
if (conResult != NO_ERROR)
printf("ioctlsocket failed with error: %ldn", conResult);
conResult = setsockopt(sock_, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(DWORD));
if (conResult != NO_ERROR)
{
printf("nSetsocopt fail with error :%dn",WSAGetLastError());
return 0;
}
// Connect to server.
conResult = connect(sock_, ptr->ai_addr, (int)ptr->ai_addrlen);
if (conResult == SOCKET_ERROR) {
closesocket(sock_);
sock_ = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (sock_ == INVALID_SOCKET) {
printf("Unable to connect to server!n");
WSACleanup();
return 1;
}
DWORD dwThreadId;
threadHandle_=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,this,0,&dwThreadId);
}
int SendRequest(SOCKET sock)
{
int sendResult;
char data[6]="CM00";
data[4]=0x0F;
data[5]=0x0D;
sendResult = send( sock, data, (int)strlen(data), 0 );
if (sendResult == SOCKET_ERROR)
{
printf("send failed with error: %dn", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ldn",sendResult);
}
char* ReceiveResponse(SOCKET sock )
{
int recvResult;
char recvBuf[1024];
char common[138];
int j;
int w =0;
int recvbuflen = DEFAULT_BUFLEN;
do {
recvResult = recv(sock, recvBuf, recvbuflen, 0);
if ( recvResult > 0 )
{
//wLog->WriteErrorLog(recvbuf);
for(j=0;j<=recvResult;j++)
{
common[w] = recvBuf[j];
w++;
}
printf("Bytes received: %dn",recvResult);
memset(recvBuf, 0, sizeof(recvBuf));
}
else if ( recvResult == 0 )
printf("Connection closedn");
else
printf("recv failed with error: %dn", WSAGetLastError());
} while( recvResult> 0 );
return common;
}
static UINT Derived::receive(void)
{
while(1)
{
SendRequest(sock_);
ReceiveResponse(sock_);
Sleep(10000);
}
}
DWORD WINAPI Base::ServerConnectThread(LPVOID lpdwThreadParam)
{
SOCKET ThreadSocket = INVALID_SOCKET;
ThreadSocket=(SOCKET)lpdwThreadParam;
while(1)
{
SendRequest(ThreadSocket);
ReceiveResponse(ThreadSocket);
Sleep(10000);
}
}
static UINT ThreadFunc(LPVOID param) {
Base* obj = (Base*)param;
obj->ServerConnectThread(); //how to pass socket
}
};
我必须在连接功能中创建的套接字传递给服务器连接线程,您能否指导我如何做到这一点
TL;DR
使用成员变量:
class Base
{
...
public:
int the_socket_that_i_created_in_connect_function;
};
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 在C++中对T*类型执行std::move的意外行为
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在没有信号的情况下从C++执行QML插槽
- 如何确认我的constexpr表达式实际上已经在编译时执行
- C++17中的并行执行策略
- QML按钮点击功能执行顺序
- 程序在执行程序的其余部分之前退出
- 为什么catch中的代码没有被执行
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 将执行、作业和WinAPI相乘
- 对字符数组中的元素执行逐位操作
- 为什么g++在未执行的代码处标记强制转换错误
- 如何使用原子指针执行双缓冲