Thread: first winsock...

  1. #1

    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
    #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]="";	
    	//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;
    		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;
    		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;
    		return (1);
    	char sendBuf[256];
    	char recvBuf[256];
    	char socketcommand[100]="";
    	strcat(socketcommand,"Helo ");
    	//Successfully connected!
    	//Shutdown Winsock
    	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
    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
    Try initializing the recvBuf to 0 before you do a recv() in it:
    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:
    cout << sendBuf << endl;
    just before doing send()

    Also, in which recv() are you getting this garbage???
    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, 08: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, 01: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, 12:11 PM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM