Thread: recv() problem:(

  1. #1
    Registered User piotrek_no_1's Avatar
    Join Date
    May 2005
    Location
    Poland
    Posts
    4

    recv() problem:(

    I've reade Beej's guide to network programming and i've decided to write a mail sende. I want it to use a local polish server which ip number is 213.180.130.20. The whole idea goes like this:
    1)i connect to the ip number on port 25 and receive it's welcoming message (date and that stuff)
    2)i ask the user to input his email and destination email
    3)i ask the user to input the data
    4)after that the program tries to send the message.

    And this is the place whrere everything goes wrong...after i send the first part of message i cannor recv() but the server should respond.

    The server is 100% good as i've chceked it in telnet.

    here's the source code:

    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <string.h>
    #include <stdio.h>
    
    
    #define PRT_NUM   25
    #define RCP_PRT   4000
    #define BACKLOG   10
    
    int main(){
    	char ip_num[32]={"213.180.130.20"};
    	WSAData wsad;
    	static SOCKET sockfd,new_fd;
    	struct sockaddr_in my_addr;
        struct sockaddr_in rcp_addr;
    	int sin_size;
    	int ret_war=0;
    	size_t len=0;
    
    	char *helo_msg="HELO user.com";
    	char *quit_msg="QUIT";
    
    	char mail_name1[50];
    	char mail_name2[50];
    	char data[100];
    	char checked_mail_name1[61];
        char checked_mail_name2[61];
    	char checked_data[59];
        char msg_buf[100];
    
    
    	if(WSAStartup(MAKEWORD(1,1),&wsad)!=0){
    		fprintf(stderr,"Nie mozna otworzyć połączenia!\n(WSAStartup!=0)");
    		exit(1);
    	}
    	else{fprintf(stdout,"WSAStartup przygotowane...\n");}
    
    	sockfd=socket(AF_INET,SOCK_STREAM,0);
    	if(sockfd==INVALID_SOCKET){
    		fprintf(stderr,"Nie mozna otworzyć polaczenia!\n(socket==-1)");
    		exit(1);
    	}
    	else{fprintf(stdout,"Gniazdo przygotowane...\n");}
       
    	my_addr.sin_family=AF_INET;
    	my_addr.sin_port  =htons(PRT_NUM); 
    	my_addr.sin_addr.s_addr=INADDR_ANY;
    	memset(&(my_addr.sin_zero),'\0',8);
    	
    	rcp_addr.sin_family =AF_INET;
    	rcp_addr.sin_port   =htons(PRT_NUM);;
    	rcp_addr.sin_addr.s_addr=inet_addr(ip_num);
    	memset(&(rcp_addr.sin_zero),'\0',8);
       
    	fprintf(stdout,"Trwa laczenie...\n");
    	if(connect(sockfd,(struct sockaddr *)&rcp_addr,sizeof(struct sockaddr))==-1){
    		fprintf(stdout,"Nie mozna nawiazac palaczenia!(connect<0)\n");
    		exit(1);
    	}
    	
    	memset(msg_buf,'\0',100);
    	ret_war=recv(sockfd,msg_buf,99,0);
    	if(ret_war<0){
    		fprintf(stdout,"Nie mozna nawiazac polaczanie!(recv<0)\n");
    		exit(1);
    	}
    	fprintf(stdout,"Nawiazano polaczenie z serwerem...\n");
    	printf("%s",msg_buf);
    
    	printf("\nPodaj nazwe swojego maila : ");
    	scanf("%s",&mail_name1);
    	printf("Podaj nazwe maila docelowego:");
    	scanf("%s",&mail_name2);
    	printf("Podaj tresc wiadomosci:");
    	scanf("%s",&data);
        printf("Przetwarzanie danych...");
        
    	sprintf(checked_mail_name1,"MAIL FROM: <%s>",mail_name1);
    	sprintf(checked_mail_name2,"RCPT TO: <%s>",mail_name2);
    	sprintf(checked_data,"DATA: %s\n.\n",data);
    
    	printf("\nWysyłanie danych na serwer...");
         
    
     
         len=strlen(helo_msg);
    	 len=send(sockfd,&helo_msg[0],len,0);
    	 if(len==SOCKET_ERROR){
    			printf("\nWiadomosc nie wyslana!\n");
    			exit(1);
    		}
    
        memset(msg_buf,'\0',100);
    	ret_war=recv(sockfd,msg_buf,99,0);
    	printf("%s",msg_buf);
    	
    	
    	len=strlen(checked_mail_name1);
    	if(len!=send(sockfd,checked_mail_name1,len,0)||len==SOCKET_ERROR){
    			printf("\nWiadomosc nie wyslana!");
    			exit(1);
    		}
        memset(msg_buf,'\0',100);
    	ret_war=recv(sockfd,msg_buf,99,0);
    	printf("%s",msg_buf);
    	
    	len=strlen(checked_mail_name2);
    	if(len!=send(sockfd,checked_mail_name2,len,0)||len==SOCKET_ERROR){
    			printf("\nWiadomosc nie wyslana!");
    			exit(1);
    	    }
        memset(msg_buf,'\0',100);
    	ret_war=recv(sockfd,msg_buf,99,0);
    	printf("%s",msg_buf);
    
    	len=strlen(checked_data);
    	if(len!=send(sockfd,checked_data,len,0)||len==SOCKET_ERROR){
    		    printf("\nWiadomosc nie wyslana!");
    			exit(1);
    		}
    	memset(msg_buf,'\0',100);
    	ret_war=recv(sockfd,msg_buf,99,0);
    	printf("%s",msg_buf);
    	
    	len=strlen(quit_msg);
    	if(len!=send(sockfd,quit_msg,len,0)||len==SOCKET_ERROR){
    			printf("\nWiadomosc nie wyslana!");
    			exit(1);
    		}
    	memset(msg_buf,'\0',100);
    	ret_war=recv(sockfd,msg_buf,99,0);
    	printf("%s",msg_buf);
    
        printf("\nWiadomosc wyslana pomyslnie!");
      
      WSACleanup();
      getchar();
      return 0;
    }
    the error messages are in polish so don't bother with them

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think you need a new line after each command.

  3. #3
    Registered User piotrek_no_1's Avatar
    Join Date
    May 2005
    Location
    Poland
    Posts
    4
    that's not the only error...i've done what u said and it still doesn't work...
    waiting for othe suggestions

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The easiest way to debug network protocol issues is to download Ethereal and see what your client is doing differently compared to a working client. Maybe the problem is related to the use of scanf. Print out each command before sending it.
    Last edited by anonytmouse; 05-12-2005 at 09:04 AM.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Each command you send to the server should be ended with \r\n
    Check out RFC821

    You also are just receiving 99 bytes of the welcome message...It could be longer than that..
    Set a timeout for the socket, and recv() in a while loop to be sure to receive the entire message.

  6. #6
    Registered User piotrek_no_1's Avatar
    Join Date
    May 2005
    Location
    Poland
    Posts
    4
    ok i'll fix that but the program is working now. thanks for all your help anonytmouse and knutso. bye

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Sockets - recv() problem
    By Mercurius in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-15-2006, 07:49 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. stream socket problem
    By WL in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 11:07 PM