first winsock...

This is a discussion on first winsock... within the C++ Programming forums, part of the General Programming Boards category; why do i get so much junk when i try to receive what the smtp server is sending? try it ...

  1. #1
    Unregistered
    Guest

    first winsock...

    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);
    }

  2. #2
    Unregistered
    Guest
    i tired you method but i dstill get some kind of garbage when it is displayed.... so weird sideways charecter

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    Try initializing the recvBuf to 0 before you do a recv() in it:
    Code:
    memset( recvBuf, 0, 256 );
    Also, as vVv pointed out, you are recv()ing for max of sizeof(recvBuf) bytes which basically is: sizeof(char *) i.e. 4.

    Everything else looks ok. If you still get the garbage, check what exactly are you sending in the SendData() proc. Do a:
    Code:
    cout << sendBuf << endl;
    just before doing send()

    Also, in which recv() are you getting this garbage???
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 07:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Winsock - Where do i start?
    By Brain Cell in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-14-2005, 12:39 PM
  4. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 11:11 AM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21