connecting with socket

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?
Sorry for reporting you. There was a lot of spam and I wasn't looking closely enough.
You did not show any code, man.

Did you follow Beej's networking tutorial?
So the server\listener is 'connecting' but the client is not? Have you checked your firewall logs?
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;
}
one question
if my pc acts as a server and a client what will happen?
does it connect?
Last edited on
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.
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.
No i mean my pc's ip
Try using "localhost" as the IP address.
you mean
addr.sin_addr.s_addr = inet_addr("localhost");?
I got the error number of client's connect func
and it's connect Error:no error
how it could be?
Last edited on
thank you guys
i got the solution
i can't run the server program and client program on same PC
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
yeah, but with local IP [127.0.0.1], not international ip.
Ah, yes that is true. Sorry for the misunderstanding.
Topic archived. No new replies allowed.