![]() |
| | #1 |
| Registered User Join Date: Jan 2006
Posts: 6
| Casting a struct for sending to socket I am trying to send a struct to a UDP socket, but I'm not entirely sure how to cast the structure. Code: struct CONNECT_REQUEST {
char headersize ;// = 0x06; //static EIB header size
char protocolversion;// = 0x10; //protocol version (1.0)
char servicetype[1];// = 0x0205; //service type in this case a connect_request
char totallength[1];// = 0x0018; //total length (24 byte)
char controllength;// = 0x08; //HPAI header for control endpoint
char protocoltype;// = 0x01; //0x01 UDP, 0x02 TCP
char IPcontrol[3];//; //four chars for conrtol endpoint IP
char portcontrol[1];// //two chars for control endpoint port must be gleaned during discovery
char datalength ;// = 0x08; //HPAI header length for data endpoint
char IPdata[3]; // //fours chars for data endpoint IP use same IP as control endpoint
char portdata[1];// = 0x0e57; //set data port to 3671 default for EIB
char connectheader;// = 0x02; //connection type structue length
char connecttype;// = 0x04; //connection type in this case tunnelling may need 2 extra bytes.
};
struct CONNECT_REQUEST connect; //creat struct called connect of type CONNECT_REQUEST
Code: /* construct CONNECT_REQUEST structure */
connect.headersize = 0x06; //static EIB header size
connect.protocolversion = 0x10; //protocol version (1.0)
connect.servicetype[1] = 0x02,0x05; //service type in this case a connect_request
connect.totallength[1] = 0x00,0x18; //total length (24 byte)
connect.controllength = 0x08; //HPAI header for control endpoint
connect.protocoltype = 0x01; //0x01 UDP, 0x02 TCP
connect.IPcontrol[0] = oca, connect.IPcontrol[1] = ocb, connect.IPcontrol[2] = occ, connect.IPcontrol[2] = ocd;//set IP endpoint
connect.portcontrol[0] = recv_str[12]&255, connect.portcontrol[13]&255; //set port number for control endpoint
connect.datalength = 0x08; //HPAI header length for data endpoint
connect.IPdata[0] = oca, connect.IPdata[1] = ocb, connect.IPdata[2] = occ, connect.IPdata[3] = ocd;//set data end point IP
connect.portdata[1] = 0x0e,0x57; //set data port to 3671 default for EIB
connect.connectheader = 0x02; //connection type structue length
connect.connecttype = 0x04; //connection type in this case tunneling 04
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) //Creat UDP socket
DieWithError("UDP socket creation failed\n");
/* construct UDP address structure */
memset(&udp_addr, 0, sizeof(udp_addr)); //set udp_addr struct to 000's using memset
udp_addr.sin_family = AF_INET; //set family type to AF_INET
udp_addr.sin_addr.s_addr = htonl((oca,'.',ocb,'.',occ,'.',ocd)); //set address as any
udp_addr.sin_port = htons(mc_port); //set the port as the same multi cast port
if ((sendto(sock, &connect, sizeof(connect), 0, (struct sockaddr *) &udp_addr, sizeof(mc_addr))) != sizeof(connect)) //send data to socket
DieWithError("sendto() sent incorrect number of bytes");
close(sock);
exit(0);
}
Any help would be greatly appreciated |
| chrisjmoss is offline | |
| | #2 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| Code: char servicetype[1];// = 0x0205; //service type in this case a connect_request char totallength[1];// = 0x0018; //total length (24 byte) ... char IPcontrol[3];//; //four chars for conrtol endpoint IP ... char portcontrol[1];// //two chars for control endpoint port must be gleaned during discovery ... char IPdata[3]; // //fours chars for data endpoint IP use same IP as control endpoint char portdata[1];// = 0x0e57; //set data port to 3671 default for EIB
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. Last edited by Dino; 04-08-2008 at 07:28 AM. |
| Dino is offline | |
| | #3 |
| Registered User Join Date: Jan 2006
Posts: 6
| Code: char IPcontrol[3]; //four chars for conrtol endpoint IP char IPdata[3]; //fours chars for data endpoint IP use same IP as control endpoint char portdata[1]; //set data port to 3671 default for EIB Last edited by chrisjmoss; 04-08-2008 at 07:41 AM. |
| chrisjmoss is offline | |
| | #4 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| In this case, Code: char IPcontrol[3]; Code: char IPcontrol[4]; Code: IPcontrol[0] (the 1st element) IPcontrol[1] (the 2nd element) IPcontrol[2] (the 3rd element) IPcontrol[3] (the 4th element)
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #5 |
| Registered User Join Date: Jan 2006
Posts: 6
| Todd thanks for the help thus far, I was being a bit of a div and got confused with array size and indexing, but I have sorted that out now. Code: char servicetype[2];// = 0x0205; //service type in this case a connect_request char totallength[2];// = 0x0018; //total length (24 byte) char controllength;// = 0x08; //HPAI header for control endpoint char protocoltype;// = 0x01; //0x01 UDP, 0x02 TCP char IPcontrol[4];//; //four chars for conrtol endpoint IP char portcontrol[2];// //two chars for control endpoint port must be gleaned during discovery char datalength ;// = 0x08; //HPAI header length for data endpoint char IPdata[4]; // //fours chars for data endpoint IP use same IP as control endpoint char portdata[2];// Code: struct CONNECT_REQUEST connect; //creat struct called connect of type CONNECT_REQUEST
if ((sendto(sock, &connect, sizeof(connect), 0, (struct sockaddr *) &udp_addr, sizeof(udp_addr))) != sizeof(connect)) //send data to socket
DieWithError("sendto() sent incorrect number of bytes");
|
| chrisjmoss is offline | |
| | #6 |
| Jack of many languages Join Date: Nov 2007 Location: Katy, Texas
Posts: 1,929
| "issues" is a bit vague. We have no error messages to work with here.
__________________ Mac and Windows cross platform programmer. Ruby lover. Memorable Quotes From Recent Posts: I can't remember. |
| Dino is offline | |
| | #7 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > connect.portdata[1] = 0x0e,0x57; Also, the comma operator does not incrementally assign each value to each successive array element. Also, check that your struct is actually the size you want. Using a struct like this to communicate with an external API is generally non-portable. Code: unsigned char message[SOME_SIZE]; message[MSG_HEADER_SIZE] = 0x06; message[MSG_PROTOCOL] = 0x10; message[MSG_SERVICE_LSB] = 0x05; message[MSG_SERVICE_MSB] = 0x02; // and so on
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| trying to send a struct over a socket | Anddos | C++ Programming | 11 | 06-27-2009 02:56 PM |
| Assignment HELP!! | cprogrammer22 | C Programming | 35 | 01-24-2009 02:24 PM |
| Looking for constructive criticism | wd_kendrick | C Programming | 16 | 05-28-2008 09:42 AM |
| Concatenating in linked list | drater | C Programming | 12 | 05-02-2008 11:10 PM |
| Binary Search Tree | penance | C Programming | 4 | 08-05-2005 05:35 PM |