C socket problem

This is a discussion on C socket problem within the C Programming forums, part of the General Programming Boards category; When i run this, I get no compilation problems, When I try sending the first string it sends ok, but ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    C socket problem

    When i run this, I get no compilation problems, When I try sending the first string it sends ok, but when I try to send the second string i get this error: Command unrecognized "***"


    Input: helo
    Received: 501 5.0.0 helo requires domain address

    Input: helo
    Received: 500 5.5.1 Command unrecognized: "@"

    Input: helo
    Received: 500 5.5.1 Command unrecognized: "f@p2@W@"
    500 5.5.1 Command unrecognized: "@"



    Its odd because the first string sends succesfully. Does anyone have any ideas??
    Thanks.

    Code:
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <arpa/inet.h>
    
    void main(int argc, char *argv[])
    {
    int i = 1;
    int sockfd;
    printf("Connecting to %s on port %s\n", argv[1], argv[2]);
    
    int numbytes;
    int numbytes1;
    char in[200];
    char buf[200];
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    int vin = atoi(argv[2]);
    address.sin_port = htons(vin);
    // address.sin_port = inet_addr(argv[2]);
    address.sin_addr.s_addr = inet_addr(argv[1]);
    memset(&(address.sin_zero), '\0', 8);
    
    sockfd = socket(AF_INET, SOCK_STREAM,0);
    
    i= 0;
    if (connect(sockfd, (struct sockaddr *)&address, sizeof(struct sockaddr))== -1)
    {
    printf("Couldnt connect ");
    }
    while (i != -1)
    {
    
    	if((numbytes= recv(sockfd, buf,200-1,0))==-1)
    	{
    	printf("Error in receive");
    	i = -1;
    	}
    	buf[numbytes] = '\0';
    	printf("Received: %s",buf);
    	printf("\nInput: ");
    	strcpy(in, "\0");
    	scanf("%s", in);
    	if (send(sockfd, in, 100-1,0)== -1)
    	{
    	printf("Couldnt send");
    	i = -1;
    	}
    
    }
    close(sockfd);
    }

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    >if (send(sockfd, in, 100-1,0)== -1)
    You are sending (100 - 1) bytes stored in array 'in'. Which means that even if the user entered 5 characters, you would still be sending 99 bytes, the first (5 + 1) bytes being valid, while the rest being junk or meaningless. Try replacing (100-1) with strlen(in) and check if it helps.

    Apart from this, observe the following:

    >void main()
    Is wrong. return an integer instead.

    >sockfd = socket(AF_INET, SOCK_STREAM,0);
    Check the return value of this for success.

    >if (connect(sockfd, (struct sockaddr *)&address, sizeof(struct sockaddr))== -1)
    Quit the program here itself when there is an error instead of continueing.

    >if((numbytes= recv(sockfd, buf,200-1,0))==-1)
    (200-1), try reading byte-byte instead, since you don't know how much amount of data you are going to receive from the other end, which may not be a constant.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    >>if((numbytes= recv(sockfd, buf,200-1,0))==-1)
    (200-1), try reading byte-byte instead, since you don't know how much amount of data you are going to receive from the other end, which may not be a constant.<<


    the third param of recv() is the length (size) of the buffer. Has nothing to do with the amount of info that will be recv(), unless the buffer is not big enough for the incomming data or exceeds the buffer size set with setsocopt()

    So setting it at sizeof(buf) - 1 is IMHO correct.

    My app sends the data size first as a int and so knows how much to expect. But I deal with multiple data types with differing sizes (ie images).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    You're forgetting a few things....

    Before you do anything with winsock, you should call WSAStartup().

    Code:
    WSAStartup(MAKEWORD(1,1), &wsData); //check this for errors.
    wsData is a variable declared as WSADATA ( WSADATA wsData; ).

    Then initialize your socket
    Code:
    sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    then connect.

    Also... when you say address.sin_port = inet_address(argv[2]), thats almost right.

    try using address.sin_port = atoi(argv[2]);

    After you're done...
    Code:
    closesocket(socket_name);
    WSACleanup();

    spoon_

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by spoon_
    You're forgetting a few things....

    Before you do anything with winsock, you should call WSAStartup().

    Also... when you say address.sin_port = inet_address(argv[2]), thats almost right.
    try using address.sin_port = atoi(argv[2]);
    No one mentioned Winsock. The simple fact that those header files included look distinctly Unix (particularly the one called unistd.h), kinda proves that Winsock isn't really in the picture here.

    And this
    // address.sin_port = inet_addr(argv[2]);
    is commented out in the original code. TeMpEsT-9 has used the correct method the convert the string in argv[2] to an int, then to a network short via htons().

    Nice try, though
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    39

    Lightbulb

    Regarding the size in recv(), you could put the recv() in while loop till it returns 0 or less. Something like this:

    Code:
    char    data_str[OPTIMIZED_INITIAL_SIZE]; 
    int     bytes_read     = 1;
    int     bytes_expected = sizeof( data_str ) - 1;
    string  *result        = NULL;
       
    result = (char *)malloc( OPTIMIZED_INITIAL_SIZE );
    if ( result == NULL )
    {
       /* problemas... :)  */
       return(1);
    }
    
    while( bytes_read > 0 )
    {
       memset( data_str, 0, sizeof( data_str ) );
       bytes_read = recv( sock_fd, data_str, bytes_expected, 0 );
       
       /* append the  data_str to result doing realloc() if needed */
       
    }
    
    /* here, result contains the complete reply from the server */
    Also, you are getting reply-codes 501 and 500 from the SMTP server, which indicate incorrect syntax. Try doing a printf() just before send() to check exactly what are you sending.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    5
    Thanks guys! Sorry, I havent made a reply, I just went on vacation to nebraska... but anyway, thanks, Its working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 02:15 AM
  2. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 03:46 AM
  3. Problem with network code
    By cornholio in forum Linux Programming
    Replies: 1
    Last Post: 12-20-2005, 12:21 AM
  4. sockets problem programming
    By kavejska in forum C Programming
    Replies: 0
    Last Post: 07-25-2005, 07:01 AM
  5. Client/Server Socket Receive Problem
    By mariabair in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-25-2003, 09:01 AM

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