8 Replies - 450 Views - Last Post: 06 December 2017 - 03:23 PM Rate Topic: -----

#1 johnrees200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-November 17

UDP Socket Programming - Packets from Client to other function

Posted 04 December 2017 - 05:21 PM

Hello, Guten Tag!

I am puzzling with some code. I have to store the packet, which I have received from the client in function int receivePacket() (outside of main())
Received Packet is: Query (For instance, SELECT ID from sensors)

/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
    DieWithError("socket() failed");

/* Construct address structure */
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(SRC_PORT);

memset(&clntAddr, 0, sizeof(clntAddr));
clntAddr.sin_family = AF_INET;
clntAddr.sin_addr.s_addr = inet_addr("192.168.1.1");
clntAddr.sin_port = htons(DST_PORT);

/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
    DieWithError("bind() failed");

 for (;;)/>/>/>/>/> /* Run forever */
 {
 printf("Listening on UDP:5000 \n");

/* Set the size of the in-out parameter */
cliAddrLen = sizeof(clntAddr);

/* Block until receive message from a client */
if ((recvMsgSize = recvfrom(sock, recBuffer, MAXSIZE, (struct sockaddr *) &clntAddr, &cliAddrLen)) < 0)
    DieWithError("recvfrom() failed");

printf("Handling Client: %s\n", inet_ntoa(clntAddr.sin_addr));

printf("Received:");
for (i = 0; recBuffer[i] != '\0'; ++i)
    printf("%c", recBuffer[i]);
printf("\n");


}

int receivePacket(unsigned char* rxBuf)
{
*****************************
*****************************
return 0;
} 


Could someone help me to get the packets here? So that I can access and use these packets in other functions.

Thank you
Regards

Is This A Good Question/Topic? 0
  • +

Replies To: UDP Socket Programming - Packets from Client to other function

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6115
  • View blog
  • Posts: 21,051
  • Joined: 05-May 12

Re: UDP Socket Programming - Packets from Client to other function

Posted 04 December 2017 - 07:45 PM

Why not simply call your recievePacket() function? E.g. replace your lines 33-36 with a function call. You should not only pass the recBuffer but recvMsgSize as well since your recievePacket() wouldn't know where the packet data ends.
Was This Post Helpful? 0
  • +
  • -

#3 johnrees200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-November 17

Re: UDP Socket Programming - Packets from Client to other function

Posted 05 December 2017 - 04:18 PM

Thank you for the reply. Yes, I used function call receivePacket(), Passed recBuffer as an argument (I take off recvMesgSize, Because it's not needed).

But I am going to call the function receivePacket() again, where I wrote

unsigned char rxBuf[RX_BUF_SIZE];
            if (receivePacket(rxBuf) > 0)
            {
                // check the packet type
                switch(rxBuf[0])
                {
                    case PT_QUERY:
                        // parse and schedule the received query data packet
                        scheduleQuery(qCore, rxBuf);
                        break;
                    case PT_QUERY_RESULT:
                        {
                            // get the id for which this is a result
                            int queryId = rxBuf[1];
                            // get the corresponding query object
                            int qIdx = findRunningQuery(qCore, queryId);
                            if (qIdx > -1)
                            {
                                // add the results to this query
                                readResultPacket(qCore, rxBuf, &qCore->queries[qIdx]);
                            }
                            break;
                        }
                     case PT_CANCEL_QUERY:
	                     {
	             			  cancelQuery(qCore,rxBuf[1]);
	             			  break;
	                     }
                }
            }


Confused over, what am I supposed to write at int receivePacket(unsigned char* rxBuf) to get rxBuf[0].
Please ignore scheduleQuery and all other functions

Thank you
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6115
  • View blog
  • Posts: 21,051
  • Joined: 05-May 12

Re: UDP Socket Programming - Packets from Client to other function

Posted 05 December 2017 - 04:36 PM

I think you need to explain what receivePacket() is supposed to do. What does the return value signify? What does a return value of 0 mean? What does a return value greater than zero mean? Is recievePacket() supposed to manipulate the contents of the buffer passed to it?

Personally, I think that recvMsgSize is needed because how will receivePacket() know how much of the data within the buffer is valid. For example, let say that the buffer is 256 bytes longs, but recv() only got 10 bytes. Will recievePacket() know that only the first 10 bytes of the buffer contains valid data?
Was This Post Helpful? 0
  • +
  • -

#5 johnrees200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-November 17

Re: UDP Socket Programming - Packets from Client to other function

Posted 05 December 2017 - 05:07 PM

receivePacket() receives from Client (i.e recBuffer) and manipulate it into bytes.

What I have done is:
int receivePacket(unsigned char* rxBuf)
{
	unsigned int len = sizeof(rxBuf);
	for (i = 0; i < len; ++i )
	{
    printf("%x", rxBuf[i]);
	}
    return 0;
}



After converting into bytes.
Then checks each byte switch(rxBuf[0]), then implements relevant functions.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6115
  • View blog
  • Posts: 21,051
  • Joined: 05-May 12

Re: UDP Socket Programming - Packets from Client to other function

Posted 05 December 2017 - 07:34 PM

rxBuf already points to byte data. You are not doing any manipulation. You are simply (incorrectly) iterating over the bytes and printing out their hex values.

sizeof(rxBuf) will be either 4 or 8 bytes depending on whether you are compiling for 32 or 64 bits. This is because you are getting the size of a pointer. It will not return the size of the buffer that you are pointing to. This is why I was saying that you should pass in the size.

I am not seeing any code I'm that function that switches based in the value of rxBuf[0]. Did you mean to put the code from post #3 into the recievePacket() function?
Was This Post Helpful? 0
  • +
  • -

#7 johnrees200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-November 17

Re: UDP Socket Programming - Packets from Client to other function

Posted 05 December 2017 - 11:32 PM

Thank you for the reply. Sorry, I misunderstood the whole concept (what I have to do exactly) :hang:/> ://> . I will get back to you, if I don't get anything.

Thank you
Regards
Was This Post Helpful? 0
  • +
  • -

#8 johnrees200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-November 17

Re: UDP Socket Programming - Packets from Client to other function

Posted 06 December 2017 - 05:17 AM

Handshake:
Client -- > SNK? --> Server
Server --> YES --> Client
Handshake stays that way and works perfectly.

if ((recvMsgSize = recvfrom(sock,  recBuffer, MAXSIZE, 0,(struct sockaddr *) &clntAddr, &cliAddrLen)) < 0)
	
printf("Handling Client: %s\n", inet_ntoa(clntAddr.sin_addr));
		
printf("Received:");
for(i = 0; recBuffer[i] != '\0'; ++i)
	printf("%c",recBuffer[i]);
printf("\n");
		
/* Send response datagram back to the client */
unsigned char respBuffer[] = {255, 255, 255, 255, 4, 'Y', 'E', 'S'};
if (sendto(sock,  respBuffer, sizeof(respBuffer), 0, (struct sockaddr *) & clntAddr, sizeof(clntAddr)) < 0)


Then
Client --> Query (For instance, SELECT ID from sensors) --> Server
Server --> Query result --> Client

How to handle this? Receiving Query after the handshake? Do I need to use Thread or fork?

Thank you
Regards
Was This Post Helpful? 0
  • +
  • -

#9 johnrees200  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 28-November 17

Re: UDP Socket Programming - Packets from Client to other function

Posted 06 December 2017 - 03:23 PM

Is it possible to use receivefrom() and sendto() again? (As we already using for handshake). Server should receive Query from the client and send query result back to the client. How to handle this?

Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1