Problems with inputs from recvfrom

This is a discussion on Problems with inputs from recvfrom within the C Programming forums, part of the General Programming Boards category; Hi, I have a big problem and i hope you can help me! Iḿ trying to receive some Data from ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Problems with inputs from recvfrom

    Hi,

    I have a big problem and i hope you can help me! Iḿ trying to receive some Data from a Socket using the <sys/socket.h>.

    The Function is:

    Code:
    ssize_t recvfrom(int socket, void *restrict buffer, size_t length,
           int flags, struct sockaddr *restrict address,
           socklen_t *restrict address_len)
    buffer points to the buffer where the message should be stored.

    First I declare The buffer with:
    Code:
    unsigned char p_buffer[2048]
    Then I try to receive it:
    Code:
    len = recvfrom(mysocket, p_buffer, 2048, 0, (struct sockad*)&p_info, &p_size);
    After that all I print the data:
    Code:
    	unsigned char *p = p_buffer;
    
    	while(len--)
    	{
    		printf("%.2x ", *p);
    		p++;
    	}
    Everything works really fine until now... .

    I want to return the Data to a java program using the JNI
    Chapter 5 Continued: Strings and Arrays

    My return looks like this:
    Code:
    return (*env)->NewStringUTF(env, *p);
    The Problem is, when I print the data from Java it looks like crap... .


    Iḿ not really familiar with C and the data types.
    The package is stored in a Char Array, unsigned char p_buffer[2048], but when I print for example:
    Code:
     printf("I AM: %x ", p_buffer[4])
    I get o.e. EF but.
    I think I want to store this Hex elements in a new char array?!

    I Hope somebody can help ! All this pointers and things are really confusing me !


    Thanks and sorry for my bad English

    Kalom

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Without seeing all of your code, I'm going to make a couple of assumptions here. I also have no experience with JNI, so I could be off.

    You've got this:
    Code:
    unsigned char *p = p_buffer;
    
    while(len--)
    {
    	printf("%.2x ", *p);
    	p++;
    }
    When you're done with this loop, p is pointing to the element in the array one after the end of your actual data. If you don't reset p to point back at the beginning of the buffer, you could see this behavior.

    Also, NewStringUTF is probably expecting a NULL-terminated string, and you did not NULL-terminate it.

    You should do something like this
    Code:
    len = recvfrom(mysocket, p_buffer, sizeof(p_buffer) - 1, 0, (struct sockad*)&p_info, &p_size);
    p_buffer[len] = 0; //null-terminate the buffer

    Also, assuming p is a char *, I think you should be calling NewStringUTF like so:

    Code:
    return (*env)->NewStringUTF(env, p);
    Last edited by rags_to_riches; 01-13-2011 at 04:14 AM.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    Hi,

    Thanks for you help! The result changed. When I print the received packet in Java I get some cryptic characters. Sometimes it seems like I only received one char.

    Here is the full code

    Code:
    JNIEXPORT jstring JNICALL Java_test_getpackage(JNIEnv *env, jclass class, jint mysocket)
    {
    	unsigned char p_buffer[2048];
    	
    	struct sockaddr_ll p_info;
    	int p_info_size = sizeof(p_info);
    	int len;
    
    	
    	len = recvfrom(mysocket, p_buffer, sizeof(p_buffer) - 1, 0, (struct sockaddr*)&p_info, &p_info_size);
    	
    	p_buffer[len] == 0; //null-terminate the buffer
    
    
    	unsigned char *p = p_buffer;
    	
    	jstring s;
    	s = (*env)->NewStringUTF(env, p);
    	return s;
    }
    It would be grate if it is possible to return the Hex chars in the jstring. Is there a way to return them as a string?

    Sometimes I receive only one Char, so I think there are more problems inside of my code.

    Big Thanks!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > p_buffer[len] == 0; //null-terminate the buffer
    It would, if you used =

    > Sometimes I receive only one Char, so I think there are more problems inside of my code.
    But are you (eventually) receiving everything that you sent?
    TCP is a stream, the only guarantee is that everything sent is received in the same order. If you have 'frame' information (like a newline), then you need to extract this yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >It would be grate if it is possible to return the Hex chars in the jstring. Is there a way to return them as a string?
    Think there is function which convert C string to java string. And its NewStringUTF. And your calling the right function. But i dont see anywhere the stirng s being declared? I'm no expert in Java but think that would be the function.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Quote Originally Posted by Salem View Post
    > p_buffer[len] == 0; //null-terminate the buffer
    It would, if you used =
    DOH! My bad! I fixed the code in the post.
    Last edited by rags_to_riches; 01-13-2011 at 04:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ 2010 express problems
    By dnj23 in forum Windows Programming
    Replies: 6
    Last Post: 08-10-2010, 06:16 AM
  2. Most Common problems in C
    By Bayint Naung in forum C Programming
    Replies: 18
    Last Post: 06-02-2010, 08:20 PM
  3. How do I validate these inputs?
    By Kyeong in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 02:20 PM
  4. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 08:25 AM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM

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