Jun 8, 2014 at 12:49pm UTC
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 Jun 8, 2014 at 12:55pm UTC
Jun 8, 2014 at 1:25pm UTC
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.
Jun 8, 2014 at 1:42pm UTC
Last edited on Jun 8, 2014 at 1:46pm UTC
Jun 8, 2014 at 2:03pm UTC
Line 17,18
Use std::string
Jun 8, 2014 at 2:03pm UTC
You have to name a protocol when you call the "socket()" function, you'll probably want 'SOCK_STREAM' for this one.
Jun 8, 2014 at 2:13pm UTC
Okay I'll try those in a second busy day.
Thanks
Jun 8, 2014 at 4:45pm UTC
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...
Jun 8, 2014 at 5:11pm UTC
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?
Jun 8, 2014 at 5:12pm UTC
I was going to have that for the port.
Jun 8, 2014 at 5:32pm UTC
That would be my suggestion, yes.
Jun 8, 2014 at 5:34pm UTC
Sorry for asking so meany questions just pushing my self to understand and learn.
Jun 8, 2014 at 5:36pm UTC
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.
Jun 8, 2014 at 5:40pm UTC
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 Jun 8, 2014 at 5:45pm UTC
Jun 8, 2014 at 5:43pm UTC
No, u_short is not part of the std namespace. Your declaration should be: u_short ipa;
Jun 8, 2014 at 5:47pm UTC
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 Jun 8, 2014 at 5:48pm UTC
Jun 8, 2014 at 7:42pm UTC
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 Jun 8, 2014 at 7:56pm UTC