Home > Enterprise >  C access SOCKET from outside created thread
C access SOCKET from outside created thread

Time:02-17

I create a thread from the main thread

void xui(PLDR_DATA_TABLE_ENTRY ModuleHandle){  
ExCreateThread(&moduleHandle, 0, &threadId, ConnectSock , (LPTHREAD_START_ROUTINE)ConnectSock, NULL, 0x02);
}

the thread called

DWORD WINAPI ConnectSock() {
SOCKET Sock = NetDll_socket(XNCALLER_SYSAPP, AF_INET, SOCK_STREAM, IPPROTO_TCP);
    BYTE IPAddress[0x4] =  { 34, 231, 248, 251 };
    WORD Port = 6667;
    DWORD SocketError;
    XNetStartupParams xnsp;
    WSADATA WsaData;
    BOOL SockOpt = TRUE;
    DWORD sendRecvSize = 1024;
    sockaddr_in httpServerAdd;
    httpServerAdd.sin_addr.S_un.S_addr = *(PDWORD)IPAddress;
    //httpServerAdd.sin_addr.s_addr = inet_addr("tha-row.net");
    httpServerAdd.sin_port = htons(Port);
    httpServerAdd.sin_family = AF_INET;
    ZeroMemory(&xnsp, sizeof(xnsp));

    // Configure our xnsp variable
    xnsp.cfgSizeOfStruct = sizeof(XNetStartupParams);
    xnsp.cfgFlags = XNET_STARTUP_BYPASS_SECURITY;
    // Safely startup XNet
    if ((SocketError = NetDll_XNetStartup(XNCALLER_SYSAPP, &xnsp)) != S_OK) {

    }

    // Safely startup WSA
    if ((SocketError = NetDll_WSAStartupEx(XNCALLER_SYSAPP, MAKEWORD(2, 2), &WsaData, 2)) != S_OK) {

    }

    // Safely create socket
    //Sock = NetDll_socket(XNCALLER_SYSAPP, AF_INET, SOCK_STREAM, IPPROTO_TCP);
    //Sock = socket(AF_INET, SOCK_STREAM, 0);
    // Disable network encryption
    if (NetDll_setsockopt(XNCALLER_SYSAPP, Sock, SOL_SOCKET, 0x5801, (PCSTR)&SockOpt, 4) != S_OK) {

    }

    // Configure socket send/recv size
    NetDll_setsockopt(XNCALLER_SYSAPP, Sock, SOL_SOCKET, SO_SNDBUF, (PCSTR)&sendRecvSize, 4);
    NetDll_setsockopt(XNCALLER_SYSAPP, Sock, SOL_SOCKET, SO_RCVBUF, (PCSTR)&sendRecvSize, 4);

    // Create connection timeout
    struct timeval tv;
    tv.tv_sec = 15;
    tv.tv_usec = 0;
    setsockopt(Sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval));

    if (NetDll_connect(XNCALLER_SYSAPP, Sock, (struct sockaddr*)&httpServerAdd, sizeof(httpServerAdd)) == SOCKET_ERROR) {
        std::string wsacon("subtitle_add \"fai!");
        wsacon  = "\" 3";
        const char* Nwsacon = wsacon.c_str();
        ConsoleCommand2(Nwsacon);
    }
    int ReadBytes = 0;
    ofstream ircdata;
    char buff[1024];
    while (true)
    {
        ZeroMemory(buff, 1024);

        int bytesReceived = NetDll_recv(XNCALLER_SYSAPP,Params->sock, buff, 1024, 0);
        if (bytesReceived == SOCKET_ERROR)
        {

        }
        if (bytesReceived > 0) {
        }
    }
    NetDll_closesocket(XNCALLER_SYSAPP, Sock);
}

how can i access "Sock" from outside the new created thread and send a message like

string string2send = "string to send\n";
                            NetDll_send(XNCALLER_SYSAPP, Sock,string2send.c_str(), string2send.length(), 0);

note this is not the entire code of the socket i shortened it as much as i can to get my point across..

I just want to send to the socket from another thread

if i have

SOCKET Sock = NetDll_socket(XNCALLER_SYSAPP, AF_INET, SOCK_STREAM, IPPROTO_TCP);

as a global variable Sock still equals 0 outside of the thread

CodePudding user response:

You have several choices

  • make Sock global
  • Pass in a Sock pointer in the thread start parameters
  • call some method in the connect function passing the Sock as a paramter and start your send thread in that function, passing Sock to it (like in option 2)

in the first two cases you need some synchronization primitives to not use the Socket before its connected.

BTW this is an unusual 'shape' for code dealing with a TCP connection. Normally one thread deals with one connection

CodePudding user response:

so this worked for me thanks to @Remy and @pm100

void Player::ExternalMsg(){
string Z = "IRCX\r\n";
    NetDll_send(XNCALLER_SYSAPP, Sock2, Z.c_str(), Z.length(), 0);
}
void SendSockInfo(SOCKET s) {
    Sock2 = s;
}

after I made a successful connection

SendSockInfo(sock);
  • Related