why do i get so much junk when i try to receive what the smtp server is sending? try it the smtp is valid
Code:#include <winsock.h> #include <iostream.h> int SendData(char sendBuf[],SOCKET theSocket,int nRet); int RecvData(char recvBuf[],SOCKET theSocket,int nRet); int main(int argc, char** argv) { WORD version = MAKEWORD(1,1); WSADATA wsaData; int nRet; char hostip[50]="smtp.wwexp.com"; //Start up Winsock as before WSAStartup(version, &wsaData); //Store information about the server LPHOSTENT lpHostEntry; lpHostEntry = gethostbyname(hostip);//Specifying server by its name if (lpHostEntry == NULL) { cout<<"Error at gethostbyname()"<<endl; WSACleanup(); return (1); } //Create the socket SOCKET theSocket; theSocket = socket(AF_INET, //Go over TCP/IP SOCK_STREAM, //Socket type IPPROTO_TCP); //Protocol if (theSocket == INVALID_SOCKET) { cout<<"Error at socket()"<<endl; WSACleanup(); return (1); } // Use SOCKADDR_IN to fill in address information SOCKADDR_IN saServer; saServer.sin_family = AF_INET; saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list); // ^ Address of the server being inserted into the address field saServer.sin_port = htons(25);//port to connect to // Connect to the server nRet = connect(theSocket, (LPSOCKADDR)&saServer, //Server address sizeof(struct sockaddr)); //Length of address structure if (nRet == SOCKET_ERROR) { cout<<"Error at connect()"<<endl; WSACleanup(); return (1); } char sendBuf[256]; char recvBuf[256]; RecvData(recvBuf,theSocket,nRet); cout<<recvBuf<<endl; char socketcommand[100]=""; strcat(socketcommand,"Helo "); strcat(socketcommand,hostip); strcpy(sendBuf,socketcommand); SendData(sendBuf,theSocket,nRet); RecvData(recvBuf,theSocket,nRet); cout<<recvBuf<<endl; //Successfully connected! closesocket(theSocket); //Shutdown Winsock WSACleanup(); return (0); } int SendData(char sendBuf[],SOCKET theSocket,int nRet) { nRet = send(theSocket, //Pretend this is connected sendBuf, //Our string buffer strlen(sendBuf),//Length of the data in the buffer 0); //Most often is 0, but see end of tutorial for options if (nRet == SOCKET_ERROR){ cout<<"Error on send()"<<endl; return (1); } return (0); } int RecvData(char recvBuf[],SOCKET theSocket,int nRet) { nRet = recv(theSocket, // Connected socket recvBuf, // Receive buffer sizeof(recvBuf),// Size of the buffer 0); // More flags if (nRet == SOCKET_ERROR) { cout<<"Error on recv()"<<endl; return (1); } return (0); }



LinkBack URL
About LinkBacks



