need help changing my program into a windows service

i want to create a application that can run as a service and also another program that can communicate with it. Basically i want the program to send messages to the service and the service just echoes them back to the program

sample output of program would be something like this:
Please input your message: hello
Receive response from server: hello

i have a very simple client server console application program in UDP that does this - server echoes back the messages the clients sends. so my question is can i change the server to become a service (that will always be running) and change the client so it still communicates with the service? and if so how is this done?

i have never used code to create a service before

heres the program i want to change:
server
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
        
void InitWinsock()
    {
        WSADATA wsaData;
        WSAStartup(MAKEWORD(2, 2), &wsaData);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        SOCKET socketS;
    
        InitWinsock();
        struct sockaddr_in local;
        struct sockaddr_in from;
        int fromlen = sizeof(from);
        local.sin_family = AF_INET;
        local.sin_port = htons(1234);
        local.sin_addr.s_addr = INADDR_ANY;
    
        socketS = socket(AF_INET,SOCK_DGRAM,0);
        bind(socketS,(sockaddr*)&local,sizeof(local));
        while (1)
        {
            char buffer[1024];
            ZeroMemory(buffer, sizeof(buffer));
            printf("Waiting...\n");
            if (recvfrom(socketS,buffer,sizeof(buffer),0,(sockaddr*)&from,&fromlen)!=SOCKET_ERROR)
            {
                printf("Received message from %s: %s\n",inet_ntoa(from.sin_addr), buffer);
                sendto(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen);
            }
            Sleep(500);
        }
        closesocket(socketS);
    
        return 0;
    }


client:
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
    void InitWinsock()
    {
        WSADATA wsaData;
        WSAStartup(MAKEWORD(2, 2), &wsaData);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        SOCKET socketC;
    
        InitWinsock();
        struct sockaddr_in serverInfo;
        int len = sizeof(serverInfo);
        serverInfo.sin_family = AF_INET;
        serverInfo.sin_port = htons(1234);
        serverInfo.sin_addr.s_addr = inet_addr("127.0.0.1");
    
        socketC = socket(AF_INET,SOCK_DGRAM,0);
        while (1)
        {
            char buffer[1024];
            ZeroMemory(buffer, sizeof(buffer));
            printf("Please input your message: ");
            scanf("%s", buffer);
            if (strcmp(buffer,"exit") == 0)
                break;
            if (sendto(socketC, buffer, sizeof(buffer), 0, (sockaddr*)&serverInfo, len) != SOCKET_ERROR)
            {
                if (recvfrom(socketC, buffer, sizeof(buffer), 0, (sockaddr*)&serverInfo, &len) != SOCKET_ERROR)
                {
                    printf("Receive response from server: %s\n", buffer);
                }
            }
        }
        closesocket(socketC);
    
        return 0;
    }
Last edited on
ok so i took some code from http://msdn.microsoft.com/en-us/library/windows/desktop/bb540475%28v=vs.85%29.aspx and i got the service to work.

I was wondering is there a way to send messages to the service that doesn't include UDP like i used in my code? or is a UDP or TCP connection required to send/receive messages?
Topic archived. No new replies allowed.