Some Winsock Errors

Posts 1–14 of 14 · Page 1 of 1
Some Winsock Errors
I have recently gotten into winsock programming, and i gotta tell you, i feel like such a hacker when i get replies from the servers, lol.

Anyways, i made a program that is supposed to send me an email, and everything is good, it connects to the smtp.gmail.com server then the HELO command works perfect, but then when i send the "MAIL FROM:<blahblah@server.com>" they say, "502 5.5.1 Unrecognized Command" and idk why and i searched on google and nothing has worked. Here is my code (not the full thing just what you need):


Code:
int main()
{
	WSAData WsaDat;
	if(WSAStartup(MAKEWORD(2,2), &WsaDat) != 0)
	{
		cout<<"Initialization of Winsock failed.";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	
	if(Socket == INVALID_SOCKET)
	{
		cout<<"Invalid Socket.";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	struct hostent *host;
    if((host = gethostbyname("smtp.gmail.com")) == NULL)
	{
		cout<<"Cannot connect to host";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	SOCKADDR_IN sockaddr;
	sockaddr.sin_port = htons(587);
	sockaddr.sin_family = AF_INET;
	sockaddr.sin_addr.s_addr = *((unsigned long*) host->h_addr);

	if(connect(Socket, (SOCKADDR*) (&sockaddr), sizeof(sockaddr)) != 0)
	{
		cout<<"Failed to connect.";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	char buffer[1000];
	memset(buffer, 0, 1000);
	recv(Socket, buffer, 1000, 0);
	cout<<buffer;
    
	sprintf(buffer, "HELO %s", "smtp.gmail.com\r\n");
	send(Socket, buffer, sizeof(buffer), 0);
	recv(Socket, buffer, 200, 0);
	cout<<buffer;

	sprintf(buffer, "MAIL FROM:<@exaMpLe.com>\r\n");
	send(Socket, mailfrom, strlen(mailfrom), 0);
	recv(Socket, mailfrom, 200, 0);
	cout<<mailfrom;


	sprintf(buffer, "RCPT TO:<%s>\r\n", to);
	send(Socket, buffer, strlen(buffer), 0);
	recv(Socket, buffer, 200, 0);
	

	sprintf(buffer, "STARTTLS\r\n");
	send(Socket, buffer, strlen(buffer), 0);
	recv(Socket, buffer, 200, 0);
	
	


	cin.get();
	shutdown(Socket, SD_SEND);
	closesocket(Socket);
	WSACleanup();

	return 0;

}
Quote Originally Posted by 258456 View Post
I have recently gotten into winsock programming, and i gotta tell you, i feel like such a hacker when i get replies from the servers, lol.

Anyways, i made a program that is supposed to send me an email, and everything is good, it connects to the smtp.gmail.com server then the HELO command works perfect, but then when i send the "MAIL FROM:<blahblah@server.com>" they say, "502 5.5.1 Unrecognized Command" and idk why and i searched on google and nothing has worked. Here is my code (not the full thing just what you need):


Code:
int main()
{
	WSAData WsaDat;
	if(WSAStartup(MAKEWORD(2,2), &WsaDat) != 0)
	{
		cout<<"Initialization of Winsock failed.";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	
	if(Socket == INVALID_SOCKET)
	{
		cout<<"Invalid Socket.";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	struct hostent *host;
    if((host = gethostbyname("smtp.gmail.com")) == NULL)
	{
		cout<<"Cannot connect to host";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	SOCKADDR_IN sockaddr;
	sockaddr.sin_port = htons(587);
	sockaddr.sin_family = AF_INET;
	sockaddr.sin_addr.s_addr = *((unsigned long*) host->h_addr);

	if(connect(Socket, (SOCKADDR*) (&sockaddr), sizeof(sockaddr)) != 0)
	{
		cout<<"Failed to connect.";
		WSACleanup();
		system("PAUSE");
		return 0;
	}

	char buffer[1000];
	memset(buffer, 0, 1000);
	recv(Socket, buffer, 1000, 0);
	cout<<buffer;
    
	sprintf(buffer, "HELO %s", "smtp.gmail.com\r\n");
	send(Socket, buffer, sizeof(buffer), 0);
	recv(Socket, buffer, 200, 0);
	cout<<buffer;

	sprintf(buffer, "MAIL FROM:<@exaMpLe.com>\r\n");
	send(Socket, mailfrom, strlen(mailfrom), 0);
	recv(Socket, mailfrom, 200, 0);
	cout<<mailfrom;


	sprintf(buffer, "RCPT TO:<%s>\r\n", to);
	send(Socket, buffer, strlen(buffer), 0);
	recv(Socket, buffer, 200, 0);
	

	sprintf(buffer, "STARTTLS\r\n");
	send(Socket, buffer, strlen(buffer), 0);
	recv(Socket, buffer, 200, 0);
	
	


	cin.get();
	shutdown(Socket, SD_SEND);
	closesocket(Socket);
	WSACleanup();

	return 0;

}
The most likely problem in these cases is in which state your commands are sent. I made an irc bot a long time ago, and I stumbled on the same kind of problems.

I was sending commands to the server, and they were 100% valid. It turned out that the server did not accept the command if the data continued after the \r\n characters. Even 2 bytes of \0\0 after the \r\n would make the server ignore my commands.

I think that the same is happening here, or you're forgetting to add or remove a character.

Also, I've heard that the gmail's server no longer supports anonymous logins/mails. It's a long time ago since I took a look at the SMTP but I think that the server waits for AUTH or TLS.

EDIT:

After some searching I think the second is your problem:

Usually, servers reject RCPT TO commands that imply relaying unless authentication credentials have been accepted.
Oh that's interesting. I think it's reasonable for authentication because you don't anonymous spammers, lol.

So, how would i authenticate my credentials, like what are the commands for it?
Quote Originally Posted by 258456 View Post
Oh that's interesting. I think it's reasonable for authentication because you don't anonymous spammers, lol.

So, how would i authenticate my credentials, like what are the commands for it?
I have no idea, :O Maybe the server supports a HELP/--h/-h/?/help command, and if those don't work lookup on the SMTP.
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.

here is my code for the 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?
Quote Originally Posted by 258456 View Post
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.
Wow, ok, that's some interesting stuff, that kind of makes sense now. So let's say I start my server program on my laptop. Then I give my client program to other people, will it still connect to my server on my computer and broadcast the message to them?




EDIT========================

I just started my server on my laptop, then i started my client program on my desktop and it said that it "failed to establish connection with server".

So it turns out that if i make a server then i can only connect to it on the same computer. (I have yet another question, sorry for annoying you =) ) Is it possible for me to make a server in which i can connect to on any computer, for free? So basically making my client able to connect to my server from another computer?
start > run > cmd on your laptop then type ipconfig
it should say ipv4 address: 192.168.*.*(mine being 192.168.2.15)
This is the ip addressed assigned to your computer within your router, now if you have your desktop send the messages/connect to that ip address it'll send em to your laptop.

Edit: if you want other people outside of your local network to test you'll have to use the ip address as found on www.whatismyip.com
so i see what you are saying Hell_Demon, you are saying that instead of using gethostbyname("localhost") i should use my ip address. If so how can i implement my ip address into my code. I don't understand where to put it.
anybody have a solution to my last post?
inet_addr()
so should i replace gethostbyname function and use inet_addr() instead? Thanks by the way.




EDIT=============

Or should i replace it in the s.addr member of my socketaddr stuct?
This is how you'd implend you servers' ip address into your clients:

Code:
	sockaddr_in sockAddr;

	sockAddr.sin_family = AF_INET;
	sockAddr.sin_port = htons(808);
	sockAddr.sin_addr.S_un.S_addr = inet_addr("10.0.0.150");

	if(connect(sock,(sockaddr*)(&sockAddr),sizeof(sockAddr))  == SOCKET_ERROR )
I haven't tested this code and c+p'd out of a very old project of mine. However I think it should work.
this is my client code and it is still not working...

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= inet_addr("174.134.42.8");

	// Attempt to connect to server
	if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) == SOCKET_ERROR)
	{
		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;
	char *no = "No";
	char *yes = "Yes";
	if(strcmp(buffer, no) == 0)
	{
		std::cout<<"\nVirus is now turned off.";
		system("PAUSE");
	}
	if(strcmp(buffer, yes) == 0)
	{
		std::cout<<"\nVirus is now turned on.";
		system("PAUSE");
	}
	

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

	// Close our socket entirely
	closesocket(Socket);

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

I don't know why it's not working.

NEVERMIND IT WORKED!!!!!!!!!! THANKS!!!!!!!!!
Posts 1–14 of 14 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us