Thanks for the reply. I tried everything that you said and checked that the length passed to the function was correct this time but it still returns NULL. Error returned by WSAGetLastError() is as follows and description from msdn:
WSANO_DATA
11004
Valid name, no data record of requested type.
The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for. The usual example for this is a host name-to-address translation attempt (using gethostbyname or WSAAsyncGetHostByName) which uses the DNS (Domain Name Server). An MX record is returned but no A record—indicating the host itself exists, but is not directly reachable.
I am posting the whole code. Maybe the error is elsewhere. I'll read the website where I got this again just in case:
Code:
SOCKET client;
WSADATA wsaData;
sockaddr_in server;
int wsaret=WSAStartup(0x101,&wsaData);
if(wsaret!=0)
{
return 0;
}
string ip_address = "127.0.0.1";
const char *ip_address_char= ip_address.c_str();
client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(client==INVALID_SOCKET) {
return -1;
}
int length = strlen(ip_address.c_str());
hostent *server_ip_address = gethostbyaddr(ip_address.c_str(),strlen(ip_address.c_str()),AF_INET);
int err = WSAGetLastError();
server.sin_addr.S_un.S_addr /*=*((unsigned long*)server_ip_address->h_addr);*/= (u_long) server_ip_address->h_addr_list[0];
server.sin_family = AF_INET;
server.sin_port = htons((u_short)20248);
if(connect(client,(struct sockaddr*)&server,sizeof(server)))
{
closesocket(client);
return -1;
}
const char *output_message = "test";
char input_message[512];
send(client,output_message,sizeof(output_message),0);
int y = 0;
while(y=recv(client,input_message,512,0))
{
//f.Write(buff,y);
cout << input_message << "/n";
}
closesocket(client);
WSACleanup();
return 0;
Thanks for the help everybody. I really appreciate it.
Amish