Socket ip input.

Pages: 12
Hello everyone.
I had an idea to make a C++ server for a chat system kind of like vent.
when I have the ip set to 127.0.0.1 it works fine and I can connect to it.
But I was trying to figure out for awhile how to make it so I can input an address. I know It is simple but I just can't seem to get it to work.
I looked up multiple things and none worked when I tried to get them to work.

Thanks in advance for any help that is given. :)
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
#include <iostream>
#include <string>
#include <winsock2.h>

int main()
{
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &wsa);

    SOCKET Listen = socket (AF_INET, SOCK_STREAM, NULL);
    SOCKET Connect = socket (AF_INET, SOCK_STREAM, NULL);

    SOCKADDR_IN Server;

    string ip;
    string ipa;

    std::cout << "Please enter the ip: ";
    std::cin >> ip;

    Server.sin_addr.s_addr = inet_addr(ip);
    Server.sin_family = AF_INET;
    Server.sin_port = htons(100);

    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));

    listen(Listen, SOMAXCONN);

    int size = sizeof(Server);

    std::cout<<"Listening...";

    for(;;)
    {
        if(Connect = accept(Listen, (SOCKADDR*)&Server, &size))
        {
            std::cout<<"\nconnection was reached";
            break;
        }
    }
    WSACleanup();
    std::cin.get();
    return 0;
}


This is my first real C++ project.
Last edited on
It's probably an issue with the firewall settings on the remote client. Make sure the port you are trying to connect to, port 100 in this case, is open and accessible from your host machine.
Well that not really the problem. I'll post a screenshot when I am able to explain more but the string

http://puu.sh/9kgG2/b7db920ed0.png
Last edited on
Line 17,18

Use std::string
You have to name a protocol when you call the "socket()" function, you'll probably want 'SOCK_STREAM' for this one.
Okay I'll try those in a second busy day.
Thanks
ar2007 you mean this?

1
2
3
4

    std::cout << "Please enter the ip: ";
    std::cin >> ip;
I have the utmost confidence that this is not at all what he means. He gave you the Line numbers to correct in his post...
Yeah my bad copied the wrong line. I ment
change
1
2
    string ip;
    string ipa;

To
1
2
    std::string ip;
    std::string ipa;

correct?
Yup, that will correct one errors that you captured in that screen shot. After you correct that Line 23 should be changed to: Server.sin_addr.s_addr = inet_addr(ip.c_str()); since "inet_addr()" requires a C style string. Also, where do you plan to use your 'ipa' variable?
I was going to have that for the port.
From your google link I went to: http://msdn.microsoft.com/en-us/library/aa921391.aspx

u_short instead of string for ipa?

Or in line 25 Server.sin_port = htons(100);
That would be my suggestion, yes.
Sorry for asking so meany questions just pushing my self to understand and learn.
Sorry I just realized the absurdity of my answer format. I mean you should do the first thing, change 'ipa' from a string to a u_short. The Google link was provided because MSDN has a habit of moving things so direct links can be unreliable at times.
Alright. and yeah I understand they constantly move stuff around because some of my links i had saved are broke.

u_short ipa;

code blocks is driving me crazy it does not open in a project ..

http://puu.sh/9kwzP/6aef44e8c0.jpg
Last edited on
No, u_short is not part of the std namespace. Your declaration should be: u_short ipa;
Current code.
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
#include <iostream>
#include <string>
#include <winsock2.h>

int main()
{
    WSAData wsa;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &wsa);

    SOCKET Listen = socket (AF_INET, SOCK_STREAM, NULL);
    SOCKET Connect = socket (AF_INET, SOCK_STREAM, NULL);

    SOCKADDR_IN Server;

    std::string ip;
    u_short ipa;

    std::cout << "Please enter the ip: ";
    std::cin >> ip;
    std::cout << "Please enter the port: ";
    std::cin >> ipa;

    Server.sin_addr.s_addr = inet_addr(ip.c_str());
    Server.sin_family = AF_INET;
    Server.sin_port = htons(ipa);

    bind(Listen, (SOCKADDR*)&Server, sizeof(Server));

    listen(Listen, SOMAXCONN);

    int size = sizeof(Server);

    std::cout<<"Listening...";

    for(;;)
    {
        if(Connect = accept(Listen, (SOCKADDR*)&Server, &size))
        {
            std::cout<<"\nconnection was reached";
            break;
        }
    }
    WSACleanup();
    std::cin.get();
    return 0;
}


Later I will make it so it will only take an ip once I figure out how xD
Last edited on
Just got an idea for the server. have it get the host's ip and all they have to do is pick the port.
Is the possible?
Last edited on
Pages: 12