Apr 9, 2015 at 9:23am UTC
hey there
I programmed a socket code that binds to a port with port forwarding.
when i open the program, it binds successfully and open port checkers find my external ip and port opened.
but my client program even can't connect to that port.
anybody knows what's the problem?
Apr 9, 2015 at 9:32am UTC
Sorry for reporting you. There was a lot of spam and I wasn't looking closely enough.
Apr 9, 2015 at 3:51pm UTC
You did not show any code, man.
Did you follow Beej's networking tutorial?
Apr 9, 2015 at 4:42pm UTC
So the server\listener is 'connecting' but the client is not? Have you checked your firewall logs?
Apr 9, 2015 at 6:55pm UTC
even the external port checker sites can find it but my app... no.
yeah i checked firewall.
here's the code:
server
#include <iostream>
#include <conio.h>
#pragma comment(lib,"Ws2_32.lib")
#include <winsock.h>
using namespace std;
int main()
{
SOCKADDR_IN addr,caddr;
SOCKET s,d;
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2, 0), &wsadata) == 0)
cout << "WSA Connected.\n";
else
cout << "WSA Error.\n";
addr.sin_family = AF_INET;
addr.sin_port = htons(81);
addr.sin_addr.s_addr = inet_addr("192.168.1.100");
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (0 == bind(s, (LPSOCKADDR)&addr, sizeof(addr)))
cout << "binded.\n";
else
cout << "Bind Error.\n";
listen(s, 1);
int clen = sizeof(caddr);
d=accept(s, (sockaddr *)&caddr, &clen);
send(d, "Hello", 5, NULL);
_getch();
return 0;
}
client
#include <iostream>
#include <conio.h>
#pragma comment(lib,"Ws2_32.lib")
#include <winsock.h>
using namespace std;
int main()
{
SOCKADDR_IN addr, caddr;
SOCKET s;
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2, 0), &wsadata) == 0)
cout << "WSA Connected.\n";
else
cout << "WSA Error.\n";
addr.sin_family = AF_INET;
addr.sin_port = htons(81);
addr.sin_addr.s_addr = inet_addr("my ip");
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (0 == connect(s, (LPSOCKADDR)&addr, sizeof(addr)))
cout << "Connected.\n";
else
cout << "Connect Error.\n";
char b[5] = {NULL};
recv(s, b, 5, NULL);
cout << b;
_getch();
return 0;
}
Apr 9, 2015 at 8:21pm UTC
one question
if my pc acts as a server and a client what will happen?
does it connect?
Last edited on Apr 9, 2015 at 8:21pm UTC
Apr 10, 2015 at 1:42pm UTC
The PC is not the server or the client, the programs are. Also, I'm pretty sure I see the problem on your client side, "my ip" is not a legitimate address.
Apr 10, 2015 at 4:24pm UTC
If you are not required to use winsocks, I recommend using an actual networking library. Boost, SFML, and Lacewing all provide decent networking libraries that make your life easier.
Apr 11, 2015 at 9:48pm UTC
Try using "localhost" as the IP address.
Apr 12, 2015 at 6:50am UTC
you mean
addr.sin_addr.s_addr = inet_addr("localhost");?
Apr 12, 2015 at 7:11am UTC
I got the error number of client's connect func
and it's connect Error:no error
how it could be?
Last edited on Apr 12, 2015 at 7:11am UTC
Apr 14, 2015 at 1:25pm UTC
thank you guys
i got the solution
i can't run the server program and client program on same PC
Apr 14, 2015 at 1:28pm UTC
False. Running server and client on the same computer is the most common way of testing. Even some of Windows' internal processes communicate with sockets as part of IPC (they also suggest it on MSDN as a valid option).
Last edited on Apr 14, 2015 at 1:32pm UTC
Apr 14, 2015 at 4:50pm UTC
yeah, but with local IP [127.0.0.1], not international ip.
Apr 14, 2015 at 4:53pm UTC
Ah, yes that is true. Sorry for the misunderstanding.