where to from here

So I've completed all the exercises in this topic:
http://www.cplusplus.com/forum/articles/12974/
If anyone wants to check them out and gimme some tips it'll be greatly appreciated I've uploaded them here (in the last exercise please change the path name to the file names.txt in main.cpp, my compiler was kinda wierd i had to give it a complete path):
http://www.sendspace.com/file/pbk6ap
But my main question now is: where should I go from here. Something I want to learn is making a server client program which continuously exchanges data but I don't really know where to start and what software to use. Up to now I've been using bloodshed's dev c++ (the old one) which from what I can tell is a pretty simple compiler unable to develop gui applications for windows.
P.S. Sorry in advance if my terminology is wrong =).




Last edited on
Let's start with your IDE. Try this one out: http://wxdsgn.sourceforge.net/ it's virtually identical to Bloodshed but it's more up to date. To make a Server and a Client (they are two separate applications) you should keep it simple and start with the sockets library. Here is some documentation on it: http://www.gnu.org/software/libc/manual/html_node/Sockets.html

If you're more the visual type then here is some stuff I wrote about a year or two ago:
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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <vector>
#include <fstream>

#define _WIN32_WINNT 0x05010300 //Windows XP

#include <winsock2.h>
#include <ws2tcpip.h>

void pause()
{
    std::cin.sync();
    std::cin.ignore();
}


DWORD WorkerThread(LPVOID Arg); //NOT CURRENTLY USED
DWORD DisplayThread(LPVOID Arg);
void Broadcast(std::string Arg);

std::vector<HANDLE*> HandleVec;
std::vector<SOCKET*> SocketVec;

int main(int argc, char *argv[])
{
    WSAData wsaData;
    
    WSAStartup(MAKEWORD(2,2), &wsaData);
    
    addrinfo *result = NULL;
    addrinfo hints;
        ZeroMemory(&hints, sizeof(hints));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
        hints.ai_flags = AI_PASSIVE;
        
    SOCKET Listen = INVALID_SOCKET;
    SOCKET Client = INVALID_SOCKET;
    HANDLE NewThread = NULL;
    DWORD ExitCode = 0;
        
    if(getaddrinfo(NULL, "27015", &hints, &result) != 0)
    {
        std::cout << "FAILED TO GET ADDRESS\n" << WSAGetLastError();
        WSACleanup();
        pause();
        return 1;
    }
    
    Listen = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    
    if(Listen == INVALID_SOCKET)
    {
        std::cout << "FAILED TO CREATE SOCKET\n" << WSAGetLastError();
        WSACleanup();
        pause();
        return 2;
    }
    
    if(bind(Listen, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR)
    {
        std::cout << "FAILED TO BIND SOCKET\n" << WSAGetLastError();
        WSACleanup();
        pause();
        return 3;
    }
    
    freeaddrinfo(result);
    
    if(listen(Listen, SOMAXCONN) == SOCKET_ERROR)
    {
        std::cout << "FAILED TO listen\n" << WSAGetLastError();
        WSACleanup();
        closesocket(Listen);
        pause();
        return 3;
    }
    
    
    do
    {
        Client = accept(Listen, NULL, NULL);

    
        if(Client != INVALID_SOCKET)
        {
            NewThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &DisplayThread, (LPVOID) Client, 0, NULL);
            HandleVec.push_back(&NewThread);
            SocketVec.push_back(&Client);
        }
        
        for(int i = 0; i < HandleVec.size(); ++i)
        {
            if(GetExitCodeThread(HandleVec[i], &ExitCode) != STILL_ACTIVE)
            {
                CloseHandle(*HandleVec[i]);
                HandleVec.erase(HandleVec.begin() + i);
            }
        }
        
            
    }while(1);
    
    pause();
    WSACleanup();
    closesocket(Listen);

    return EXIT_SUCCESS;
}




DWORD WorkerThread(LPVOID Arg) //NOT CURRENTLY USED
{
    SOCKET Client = (SOCKET) Arg;
    int DEFAULT_BUFLEN = 512;
    
    char RData[DEFAULT_BUFLEN];
    int Result = 1;
    int RBuffer = DEFAULT_BUFLEN;
    DWORD ExitCode = 0;
    
    HANDLE New = NULL;
    
    New = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &DisplayThread, Arg, 0, NULL);
    
    do
    {
        std::cin.getline(RData, 256, '\n');
        
        if(send(Client, RData, RBuffer, 0) == SOCKET_ERROR)
        {
            std::cout << "\nCOULD NOT SEND\n";
            
            closesocket(Client);
            CloseHandle(New);
            
            return 1;
        }
        
    }while(Result != 0);
    
    closesocket(Client);
    CloseHandle(New);
    return 0;   
}

DWORD DisplayThread(LPVOID Arg)
{
    SOCKET Client = (SOCKET) Arg;
    int DEFAULT_BUFLEN = 512;
    
    char RData[DEFAULT_BUFLEN];
    int Result = 0;
    int RBuffer = DEFAULT_BUFLEN;

    ZeroMemory(&RData, sizeof(RData));
    Result = recv(Client, RData, RBuffer, 0);

    std::cout << RData << std::endl;

    send(Client, "close", RBuffer, 0);
    
    return 5;   
}

These are multi-threaded and Windows only.

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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>

#define _WIN32_WINNT 0x05010300 //Windows XP

#include <winsock2.h>
#include <ws2tcpip.h>

void pause()
{
    std::cin.sync();
    std::cin.ignore();
}

DWORD RECIEVING(LPVOID);	//Seperate Thread Used To Recieve Messages

int main(int argc, char *argv[])
{
    WSADATA wsaData;
    
    int Result = WSAStartup(MAKEWORD(2, 2), &wsaData); //Initialise COM
    int DEFAULT_BUFLEN = 512; 
    
    addrinfo* result = NULL;
    addrinfo hints;
        ZeroMemory(&hints, sizeof(hints));
        hints.ai_family = AF_UNSPEC;		//IP4 Or IP6
        hints.ai_socktype = SOCK_STREAM;	//Socket Type Streaming
        hints.ai_protocol = IPPROTO_TCP;	//Use TCP Protocol
	
    SOCKET Connection = INVALID_SOCKET;		
    
    DWORD Size = 10;
    int Running = 1;
    int RBuffer = DEFAULT_BUFLEN;
    char SData[Size]; // = "Connecting";
    char RData[DEFAULT_BUFLEN];
    
    GetComputerName(SData, &Size);
    
    DWORD ThreadID;
      
    if(getaddrinfo("COMPUTER_NAME", "27015", &hints, &result) != 0)
    {
        std::cout << "FAILED TO GET ADDRESS\n";
    }
    
    Connection = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    
    if(Connection == INVALID_SOCKET)
    {
        std::cout << "ERROR CREATING SOCKET\n" << WSAGetLastError() << std::endl;
        pause();
        WSACleanup();
        return 1;
    }
    
    if(connect(Connection, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR)
    {
        std::cout << "SOCKET ERROR\n" << WSAGetLastError();
        pause();
        WSACleanup();
        return 2;
    }
    
    if(send(Connection, SData, (int)strlen(SData), 0) == SOCKET_ERROR)
    {
        std::cout << "COULD NOT SEND DATA\n" << WSAGetLastError();
        pause();
        WSACleanup();
        return 3;
    }
    
    //HANDLE NewThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &RECIEVING, (LPVOID)Connection, 0, &ThreadID);
    
    //do
    //{       
       //std::cin.getline(RData, 255, '\n');
        
        if(send(Connection, RData, RBuffer, 0) == SOCKET_ERROR)
        {
            std::cout << "SOCKET ERROR\n" << WSAGetLastError();
            pause();
            WSACleanup();
            
            return 4;
        }
        
        recv(Connection, RData, RBuffer, 0);
        
        if((std::string) RData == "close")
        {
            Running = 0;
        }
        
    
    //}while(Running == 1);
    
    freeaddrinfo(result);
    WSACleanup();
    //CloseHandle(NewThread);
    
    return EXIT_SUCCESS;
}


DWORD RECIEVING(LPVOID Arg)
{
    SOCKET Connection = (SOCKET) Arg;
    
    int DEFAULT_BUFLEN = 512;
    int RBuffer = DEFAULT_BUFLEN;
    char* SData = "This Is A Test";
    char RData[DEFAULT_BUFLEN];
    int Result = 0;
    
    do
    {
        ZeroMemory(&RData, sizeof(RData));
        Result = recv(Connection, RData, RBuffer, 0);
        
        if(Result > 0)
        {
            std::cout << RData << std::endl;
        }
        
    }while(Result > 0);
    
    return 1;
}


I haven't gone over the code in a long time and I'm self taught so don't image that any of this is in any way perfect.

EDIT: I almost forgot you need to link against either libws2_32.a or ws2_32.lib depending on your compiler.
Last edited on
Hmm that large chunk of code is great and all but as it seems I'm very stupid and can't really learn from it plus it doesn't compile. I'm looking for a tutorial/book/documentation that will hand feed me and tell me about sockets and how to use them in c++ as well as how the program will comunicate with windows. Thanks though anyway =)
Last edited on
Topic archived. No new replies allowed.