
Originally Posted by
258456
OK, thanks a bunch schim, you are very well versed in winsock. Since you are here, I have a question about client to server communication.
r
here is my code fothe client:
Code:
#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
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;
}
// Resolve IP address for hostname
struct hostent *host;
if((host=gethostbyname("localhost"))==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(8888);
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;
}
// Display message from server
char buffer[1000];
memset(buffer,0,999);
int inDataLength=recv(Socket,buffer,1000,0);
std::cout<<buffer;
// Shutdown our socket
shutdown(Socket,SD_SEND);
// Close our socket entirely
closesocket(Socket);
// Cleanup Winsock
WSACleanup();
system("PAUSE");
return 0;
}
And here is my server code:
Code:
#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
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";
char *szMessage="Welcome to the server!\r\n";
send(Socket,szMessage,strlen(szMessage),0);
// Shutdown our socket
shutdown(Socket,SD_SEND);
// Close our socket entirely
closesocket(Socket);
// Cleanup Winsock
WSACleanup();
system("PAUSE");
return 0;
}
My question is about the gethostbyname() function in my client. Why is my server's host name "localhost". Also, how would i know what my server's host name is?
"localhost" is simply a way for for the IP to refer to "myself" if a server like google were to call gethostbyname("localhost") it would also return it's own address. The address returned in question is: 127.0.0.1 and it is called the "loopback" address. Everything sent to this address will be sent back to the sender (the computer, not the application). This is convenient when debugging, since client1 can send data to 127.0.0.1 and server1 can receive it on 127.0.0.1. So you can test both sides of the network locally. Instead of having the server or client on a remote computer.
E.g to test this behavior you can ping 127.0.0.1 in the command prompt.
Another test to run would be to type: "nslookup 127.0.0.1".
The second question is harder. All websites have a hostname, these hostnames are linked with ip addresses by dns (domain name server) servers. The dns servers in turn have their hostnames linked to an ip address by another dns server, this goes on until you arrive at the root servers. There are 119 dns root servers (or simply rootservers) in use. These servers are responsible for linking all other dns servers together, they do not link with websites.
websites are often held by local dns servers (local as in by country) which is why you have country codes in url's. The .com is a different case, there are some rootservers that
do support websites to be registered.
The dns servers operate quite simply: the dns server either returns the address of the requested hostname(website address) or the address of another dns server which can help you if it doesn't know the address.
Again the nslookup command can be used here (which is as you can see, a shorter version of domain name server lookup)
Eg: "nslookup google.com" will return the address of the dns servers that google uses to link it's name with an ip address.
To answer your question: You'll have to register your hostname in a dns server, which costs money, however you can just hardcode your ip address into your client applications, that can't change.