connecting with socket

Apr 9, 2015 at 9:23am
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
Sorry for reporting you. There was a lot of spam and I wasn't looking closely enough.
Apr 9, 2015 at 3:51pm
You did not show any code, man.

Did you follow Beej's networking tutorial?
Apr 9, 2015 at 4:42pm
So the server\listener is 'connecting' but the client is not? Have you checked your firewall logs?
Apr 9, 2015 at 6:55pm
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
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
Apr 10, 2015 at 1:42pm
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
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 10:43am
No i mean my pc's ip
Apr 11, 2015 at 9:48pm
Try using "localhost" as the IP address.
Apr 12, 2015 at 6:50am
you mean
addr.sin_addr.s_addr = inet_addr("localhost");?
Apr 12, 2015 at 7:11am
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
Apr 14, 2015 at 1:25pm
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
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
Apr 14, 2015 at 4:50pm
yeah, but with local IP [127.0.0.1], not international ip.
Apr 14, 2015 at 4:53pm
Ah, yes that is true. Sorry for the misunderstanding.
Topic archived. No new replies allowed.