LoopBack Address dosen't work for me!
Hi, i'm trying to make a WinSock network program run on a single computer using the LoopBack address but it dosen't seem to work
(It dosent return true on connect). I have WinXP on my machine.
Here the code:
#pragma comment(lib, "WSOCK32.lib")
#include <iostream>
#include <winsock.h>
#include <string>
using namespace std;
int main()
{
string ip;
int port;
//Define ip and port
ip = "127.0.0.1";
port = 80;
//Define containers for the data and version
WSADATA sockData;
WORD version;
SOCKET conn;
//Define container for exceptions
int error;
//Make version
version = MAKEWORD(1,1);
//Start winsock API with version and data as buffer (&)
error = WSAStartup(version, &sockData);
//Start the socket
conn = socket(AF_INET,
SOCK_STREAM,
IPPROTO_TCP);
//Once socket is established give values to nodes
SOCKADDR_IN Winsock;
Winsock.sin_family = AF_INET;
//Winsock connection port
Winsock.sin_port = htons(port);
//Winsock connection IP
Winsock.sin_addr.s_addr = inet_addr(ip.c_str());
//Connect on the socket
if(connect(conn, (SOCKADDR*) &Winsock, sizeof(Winsock)) == SOCKET_ERROR)
{
//if an error return the error number
cout << "An exception has occured (" << WSAGetLastError() << ")\n";
//Cleanup socket
WSACleanup();
system("PAUSE");
return EXIT_FAILURE;
}
cout << "Socket Connection established\n";
//Close socket connection
closesocket(conn);
cout << "Socket Connection closed\n";
system("pause");
return EXIT_SUCCESS;
}
What do you mean it doesn't work? You won't be able to connect to anything unless you're running a server. LoopBack means it refers to your machine - the machine still has to have something running to accept the connection.
What did you mean by "connect to a server"??? The loopback address isn't for
testing your app withont being on the internet???
testing your app withont being on the internet???
You still need a server application running on your machine for your client application to connect to.
You need to create a program that listens on the port your client program is trying to connect to. This page has some info on doing it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement