Connecting to another computer with winsock

I have been learning the basics of winsock over the past week and i have hit a hurdle. I have successfully managed to create a client and server based on this tutorial, http://www.win32developer.com/tutorial/winsock/winsock_tutorial_1.shtm

Client:
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <winsock2.h>
using namespace std;

int main()
{
	// Initialise Winsock
	WSADATA WsaDat;
	if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
	{
		std::cout<<"Winsock error - Winsock initialization failed\r\n";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	// Create our socket
	SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(Socket==INVALID_SOCKET)
	{
		std::cout<<"Winsock error - Socket creation Failed!\r\n";
		WSACleanup();
		system("PAUSE");
		return 0;
	}
	cout <<"Enter Ip Address : ";
    char ipaddress[20];
    cin >> ipaddress;
    cout <<"Enter port number : ";
    int portnumber;
    cin >> portnumber;
	// Resolve IP address for hostname
	struct hostent *host;
	if((host=gethostbyname(ipaddress))==NULL)
	{
		std::cout<<"Failed to resolve hostname.\r\n";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	// Setup our socket address structure
	SOCKADDR_IN SockAddr;
	SockAddr.sin_port=htons(portnumber);
	SockAddr.sin_family=AF_INET;
	SockAddr.sin_addr.s_addr=*((unsigned long*)host->h_addr);

	// Attempt to connect to server
	if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr))!=0)
	{
		std::cout<<"Failed to establish connection with server\r\n";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	while(1)
	{
        char buffer[1000];
        memset(buffer,0,999);
        int inDataLength=recv(Socket,buffer,1000,0);
        std::cout<<buffer << std::endl;
	}
	// Shutdown our socket
	shutdown(Socket,SD_SEND);

	// Close our socket entirely
	closesocket(Socket);

	// Cleanup Winsock
	WSACleanup();
	system("PAUSE");
	return 0;
}


Server:
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <winsock2.h>

int main()
{
	WSADATA WsaDat;
	if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
	{
		std::cout<<"WSA Initialization failed!\r\n";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(Socket==INVALID_SOCKET)
	{
		std::cout<<"Socket creation failed.\r\n";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	SOCKADDR_IN serverInf;
	serverInf.sin_family=AF_INET;
	serverInf.sin_addr.s_addr=INADDR_ANY;
	serverInf.sin_port=htons(8888);

	if(bind(Socket,(SOCKADDR*)(&serverInf),sizeof(serverInf))==SOCKET_ERROR)
	{
		std::cout<<"Unable to bind socket!\r\n";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	listen(Socket,1);

	SOCKET TempSock=SOCKET_ERROR;
	while(TempSock==SOCKET_ERROR)
	{
		std::cout<<"Waiting for incoming connections...\r\n";
		TempSock=accept(Socket,NULL,NULL);
	}
	Socket=TempSock;

	std::cout<<"Client connected!\r\n\r\n";
    while(1)
    {
        std::cout << "Enter your message : ";
        char message[100];
        std::cin.getline(message,100);
        send(Socket,message,strlen(message),0);
    }

	// Shutdown our socket
	shutdown(Socket,SD_SEND);

	// Close our socket entirely
	closesocket(Socket);

	// Cleanup Winsock
	WSACleanup();
	system("PAUSE");
	return 0;
}


This works using "127.0.0.1" as the port as well as over a local network using "Name-PC". Yet when i tested this out with a friend the client couldn't connect to the server ( The programs were allowed through the firewalls). I have googled this problem and found nothing, so i'm guessing the solution is either really simple, or i'm barking up the wrong tree. Any help is appreciated.

Is it local network?
Or you are trying to do that via the Internet?
Did You recover the error that was returned by the connect function?...

You should not use gethostbyname() any more...

Try using getaddrinfo() instead.
It copies everything in the right format into the sockaddr-structure contained by its own addrinfo structure (in the needed network order formats)...


Are You really sure, that the Ports you are using are opened on your router -if You are using one-, too?...
Last edited on
Over a local network works fine, its just the internet with which it isnt working
Did you try to ping his host?
Not yet, does anyone know a way i can test this out over the internet without having a friend running the client?
Topic archived. No new replies allowed.