i have a basic udp program i'm unable to print out the payload. I'm not sure exactly why and was wondering if a new perspective would be able to help out. When the program runs the client sends and the server says its receiving but i can't print out the payload. any help would be appreciated as i'm sure it something suttle.
forgive the barrage of code, but truthfully nothing complicated in it, thanks.
server code:Code:Client code: /*xrtp.c basic xrtp functions*/ #include <xrtp\xrtp.h> #include <winsock.h> #include <stdlib.h> #include <stdio.h> int xrtp_init(xrtp_packet_t* packet,char *ipaddy,int port){ /*clean out junk from the packet*/ memset(&packet,'\0', sizeof packet); /*initialize WinSock libraie(s) v2.2*/ WSADATA start; if (WSAStartup(MAKEWORD(2,2),&start) !=0){ printf("WinSock API not initalized"); fflush(stdout); getchar(); return -1; } /*create sockets for sending data to destination*/ if ((xrtp_sendSocket=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP))==INVALID_SOCKET){ printf("unable to create sending socket");fflush(stdout); getchar(); return -1; } if ((xrtp_recvSocket=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP))==INVALID_SOCKET){ printf("unable to create sending socket");fflush(stdout); getchar(); return -1; } /* you can xrtp_init(0,0) and it'll default to local host: 4050 for testing*/; if ( ipaddy == '\0') ipaddy = "127.0.0.1"; if ( port == 0) port = 4950; /*capture the destination ip address to send to*/ xrtp_destStruct.sin_family = AF_INET; xrtp_destStruct.sin_port = htons(port); xrtp_destStruct.sin_addr.s_addr = inet_addr(ipaddy); memset(xrtp_destStruct.sin_zero,'\0',sizeof(xrtp_destStruct.sin_zero)); /*let them know we here*/ printf("XRTP client initialized on win32\n"); return 0; }/*end of xrtp_init()*/ /*assign rtp header info for the session*/ size_t xrtp_header_init(xrtp_packet_t *rtp_packet){ rtp_packet->Header.v=htons(2);/*version 2*/ rtp_packet->Header.p=htons(0);/*no additional padding @ end of payload*/ rtp_packet->Header.x=htons(0);/*payload type not followed by header extension*/ rtp_packet->Header.cc=htons(0);/*no contributing sources for now*/ rtp_packet->Header.m=htons(1);/* = 1 to indicate 1 frame of speex payload*/ rtp_packet->Header.pt=htons(97);/*payload type is dynamic given by SDP*/ rtp_packet->Header.sn=htons(345);/*sequence number has to increment in callback*/ rtp_packet->Header.ts=htons(1230);/*time stamp to be incremented in callback = speex sample rate */ rtp_packet->Header.ssrc=htons(0);/*no syncronization sources*/ rtp_packet->Header.csrc=htons(0);/*no contirbuting sources*/ return sizeof(rtp_packet->Header); } size_t xrtp_marshall(xrtp_packet_t *packet,char *msg,size_t msg_len){ /*serialiizes data from msg buffer xrtp_payload just to be sure or strncpy*/ size_t msg_ndx,bytes_copied=0; for(msg_ndx=0;msg_ndx<msg_len;msg_ndx++){ packet->payload[msg_ndx]= msg[msg_ndx]; bytes_copied+=1; } /*if we count how many bytes may not need to delimit*/ packet->payload[msg_ndx]='\0'; return bytes_copied; } size_t send_xrtp_packet(xrtp_packet_t xrtppacket,size_t packet_len){ /*pack the packet and send to its destination*/ size_t bytes_sent = sendto(xrtp_sendSocket,(void*)&xrtppacket,packet_len,0, (struct sockaddr*)&xrtp_destStruct, sizeof (xrtp_destStruct) ); /*comment out in production code: need acurate way to measure payload size*/ printf("sending %d bytes to %s to port %d\n", bytes_sent, inet_ntoa(xrtp_destStruct.sin_addr),ntohs(xrtp_destStruct.sin_port)); return bytes_sent; } size_t recv_xrtp_packet(xrtp_packet_t xrtpbuf,size_t b_len){ /*recv needs a pointer to the size of the reciving struct*/ size_t sender_s = sizeof (xrtp_recvStruct); /*get the packet put it to the xrtp buffer we have for it*/ size_t bytes_recv=recvfrom(xrtp_recvSocket,(void*)&xrtpbuf,b_len, 0,(struct sockaddr*)&xrtp_recvStruct, &sender_s); /*print statement saying how many bytes recv and from where*/ return bytes_recv; } int xrtp_destroy(){ /*are these still in the same namespace? should be we'll see log to file*/ closesocket(xrtp_sendSocket); closesocket(xrtp_recvSocket); WSACleanup(); getchar(); return 0; }how the server is called:Code:/* basic functions for the xrtp server_t that receives packetts*/ #include <xrtp\xrtp_server.h>/*which also calls xrtp.h*/ #include <stdio.h> #include <stdlib.h> #include <winsock.h> int xrtp_server_init(xrtp_packet_t* buffer,int port_num){ /*clean out garbege in buffer*/ memset(&buffer,'\0',sizeof buffer); /*initialize socket api and check to make sure version correct*/ /*all these returns should be enumerated to mean something and logged*/ WSADATA info; if (WSAStartup(MAKEWORD(2,2), &info) != 0){ perror("Socket Startup:"); getchar();return -1; } /*get host ip so can use for server ip for now*/ memset(&xrtp_ServerhostInfo,0,sizeof (xrtp_ServerhostInfo) ); char host[BUFSIZ]; gethostname(host,BUFSIZ);//GET COMP NAME xrtp_ServerhostInfo = gethostbyname(host);//ERROR CHECK IN PROD CODE-GET IP /*zero out address structure if anything was still left there*/ memset(&xrtp_serverRecvStruct,0,sizeof(xrtp_serverRecvStruct) ); xrtp_serverRecvStruct.sin_family = AF_INET; xrtp_serverRecvStruct.sin_port = htons(port_num); memcpy(&xrtp_serverRecvStruct.sin_addr,xrtp_ServerhostInfo->h_addr, xrtp_ServerhostInfo->h_length); memset(xrtp_serverRecvStruct.sin_zero, '\0', sizeof(xrtp_serverRecvStruct.sin_zero)); /*create a socket for binding and sending back to xrtp client*/ if ((xrtp_serverSocket = socket(PF_INET, SOCK_DGRAM,IPPROTO_UDP))==INVALID_SOCKET){ perror("socket"); return INVALID_SOCKET; } /*bind the socket to the local address so now a server*/ if (bind(xrtp_serverSocket, (struct sockaddr *)&xrtp_serverRecvStruct, sizeof (xrtp_serverRecvStruct) ) == SOCKET_ERROR){ perror("bind"); return SOCKET_ERROR; } /*broadcast server ip*/ printf("xrtp_server @ %s on port %d\n",inet_ntoa(xrtp_serverRecvStruct.sin_addr), ntohs(xrtp_serverRecvStruct.sin_port)); return 0; }/*end of initializing the server*/ /*recive the data from the client-how to know how much to recieve*/ size_t xrtp_server_recv_to(xrtp_packet_t recvBuffer,size_t b_len){ /*recv needs a pointer to the size of the reciving struct or internet struct*/ size_t sendersIpstruct_s = sizeof (xrtp_sendersIpstruct); /*get the packet put it to the xrtp buffer we have for it and store senders IP*/ size_t sender_bytes_recv=recvfrom(xrtp_serverSocket,(void*)&recvBuffer,b_len, /*now store senders unique ip in the recv struct we have for him*/ 0,(struct sockaddr*)&xrtp_sendersIpstruct, &sendersIpstruct_s); /*comment out later in production code*/ printf("recieving: %d bytes from %s\n", sender_bytes_recv,inet_ntoa(xrtp_sendersIpstruct.sin_addr)); return sender_bytes_recv; } /*converts packet header to hostbyte order*/ size_t xrtp_header_convert(xrtp_packet_t *rtp_packet){ /*(*rtp_packet).Header.v=ntohs(2))*/ rtp_packet->Header.v=ntohs(2);/*version 2*/ rtp_packet->Header.p=ntohs(0);/*no additional padding @ end of payload*/ rtp_packet->Header.x=ntohs(0);/*payload type not followed by header extension*/ rtp_packet->Header.cc=ntohs(0);/*no contributing sources for now*/ rtp_packet->Header.m=ntohs(1);/* = 1 to indicate 1 frame of speex payload*/ rtp_packet->Header.pt=ntohs(97);/*payload type is dynamic given by SDP*/ rtp_packet->Header.sn=ntohs(125);/*sequence number has to increment in callback*/ rtp_packet->Header.ts=ntohs(1230);/*time stamp to be incremented in callback = speex sample rate */ rtp_packet->Header.ssrc=ntohs(0);/*no syncronization sources*/ rtp_packet->Header.csrc=ntohs(0);/*no contirbuting sources*/ } /*just clean up the servers mess not the client obvioulsy*/ void xrtp_server_destroy(){ closesocket(xrtp_serverSocket); WSACleanup(); /*comment out in produxtion code*/ printf("ENTER TO EXIT"); getchar(); return; }Code:/*simple datagram server*/ #include <xrtp/xrtp.h> #define MYPORT 4950 int main(void){ /*i'm thinking not recieving has something to do with the same xrtp_packet_t?*/ xrtp_packet_t buff; xrtp_server_init(&buff,MYPORT); for(;;){ xrtp_server_recv_to(buff,32 ); xrtp_header_convert(&buff); /*prints out only garbege: we cleaned out the buffer in initialiazton so... and converted the header so we should be able to just print them out...*/ printf("%d\t%s\n",buff.Header.sn,buff.payload); } /*never reached*/ xrtp_server_destroy(); }


