how can program access strings saved in a vector in server app?

i have a server that saves the clients ip addresses to a vector.
how can another program access this vector and display the contains in a listbox?

here is the server that stores the vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  WSADATA wsaData; 
        SOCKET ListeningSocket, NewConnection; 
        SOCKADDR_IN ServerAddr, SenderInfo;  quantity
        int Port = 7171;
        char recvbuff[1024];
        int ByteReceived, i, nlen;

        ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (ListeningSocket == INVALID_SOCKET)
        {
            printf("Server: Error at socket, error code: %ld.\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }

        ServerAddr.sin_family = AF_INET; 
        ServerAddr.sin_port = htons(Port);
        ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);

        if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
        {
            printf("Server: bind failed! Error code: %ld.\n", WSAGetLastError());
            closesocket(ListeningSocket);
            WSACleanup();
            return 1;
        }

        if (listen(ListeningSocket, 5) == SOCKET_ERROR)
        {
            printf("Server: listen: Error listening on socket %ld.\n", WSAGetLastError());
            closesocket(ListeningSocket);
            WSACleanup();
            return 1;
        }
        else
        {
            printf("Server: listening for connections...\n\n");
        }
        std::vector<std::string> vClientIPs; // client ip string vector
        while(1)
        {
            NewConnection = SOCKET_ERROR;
                while(NewConnection == SOCKET_ERROR)
                {
                    NewConnection = accept(ListeningSocket, NULL, NULL);
                    printf("Server: New client got connected, ready to receive and send data...\n\n");
                    ByteReceived = recv(NewConnection, recvbuff, sizeof(recvbuff), 0);

                    if ( ByteReceived > 0 )
                    {
                        getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
                        printf("Server: IP(s) used by Server: %s\n", inet_ntoa(ServerAddr.sin_addr)); 
                        vClientIPs.psuh_back(std::string(inet_ntoa(ServerAddr.sin_addr))); //insert client ip
                        printf("Server: port used by Server: %d\n\n", htons(ServerAddr.sin_port));
                        memset(&SenderInfo, 0, sizeof(SenderInfo));
                        nlen = sizeof(SenderInfo);
                        getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
                        printf("Server: IP used by Client: %s\n", inet_ntoa(SenderInfo.sin_addr));
                        printf("Server: Port used by Client: %d\n", htons(SenderInfo.sin_port));
                        printf("Server: Bytes received: %d\n", ByteReceived);
                        printf("Server: Message from client: \"");

                        for(i=0;i < ByteReceived;i++)
                        {
                            printf("%c", recvbuff[i]);
                        }
                        printf("\"");
                        }
                        else if ( ByteReceived == 0 )
                        {
                            printf("Server: Connection closed!\n");
                        }
                        else
                        {
                            printf("Server: recv failed with error code: %d\n", WSAGetLastError());
                        }
                    }
                }
how is this code called. When a button is pressed?

vClientIPs shouldn't be a local variable. Instead pass a reference to it to the function that contains the code above
this is all of the code. its a simple console application
there is no function
Last edited on
wait, I overlooked the 'another program'.

you can send a specific message from the other program to the server.
That would mean that after the recv on line 47 you need to determine what type of message was received. You may use the very first byte as the indicator for a specific message type.

0 - Ping: send back the received message
1 - Connected clients request: send back the content of the vectort
2 - etc.

By the way: before you store the client ip in the vector you should check if the client ip isn't already present
Topic archived. No new replies allowed.