Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the following is the code I had problems with. Scenario: Once I clicked the command button, it triggers the function Command_Msg() which will send the array of unsigned chars through UDP. However, it isn't working. Even though there are no errors when compiled, nothing is being sent to the receiver side. Any suggestions?

C
const char* address = "192.168.15.119";
int port = 2222;



void CVNS_EmuDlg::OnBnClickedCmd()
{
    // TODO: Add your control notification handler code here

    UpdateData(TRUE);
    //SetTimerRead(true);
    command_Msg();

    UpdateData(FALSE);
}




void CVNS_EmuDlg::command_Msg()
{
    SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if(s == -1)
    {
        printf("Socket Initialiation Error");
    }
    SOCKADDR_IN serveraddr;
    struct hostent *hostentry;
	    
    memset(&serveraddr,0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(port);
    //serveraddr.sin_addr.s_addr = inet_addr(pipaddr);
    serveraddr.sin_addr.s_addr = inet_addr(address);
    char sbuf[1024], rbuf[1024];
    int len = sizeof(SOCKADDR_IN);


	//reserved
	ether.Data[0] = BYTE(ether.Trans.reserved);
	
	//assign command message ID
	ether.Trans.MsgID = ether.Trans.CMD.MCD_ID;

	char display[80];
	
	sprintf_s(display, "%d", ether.Trans.MsgID);
	txtCmdID = display;
		
	ether.Data[1] = BYTE(ether.Trans.MsgID);


	//counter
	ether.Trans.msg_count = counter;
	sprintf_s(display, "%d", ether.Trans.msg_count);
	Count = display;

	//count 2 bytes
	unsigned int c1, c2;

	c1 = ((0x0000FF00 & ether.Trans.msg_count) << 8);
	c2 = 0x000000FF & ether.Trans.msg_count;
    
	ether.Data[2] = BYTE(c1);
	ether.Data[3] = BYTE(c2);	


	//validity
	ether.Trans.validity =1;

	unsigned int v1, v2;
	
	v1 = ((0x0000FF00 & ether.Trans.validity) << 8);
	v2 = 0x000000FF & ether.Trans.validity;

	ether.Data[4] = BYTE(v1);
	ether.Data[5] = BYTE(v2);

	

	//request ID
	double val = _tstof((LPCTSTR)txtID);

	ether.Trans.CMD.Req_ID = (unsigned int)(val);
	
	//count 2 bytes
	unsigned int r1, r2;
	
	r1 = ((0x0000FF00 & ether.Trans.CMD.Req_ID) <<  8);
	r2 = 0x000000FF & ether.Trans.CMD.Req_ID;

	ether.Data[6] = BYTE(r1);
	ether.Data[7] = BYTE(r2);
		

	//Command
	unsigned int cx1, cx2;
	cx1 =  ((0x0000FF00 & ether.Trans.CMD.Command) <<  8);
	cx2 = 0x000000FF & ether.Trans.CMD.Command;

	ether.Data[8] = BYTE(cx1);
	ether.Data[9] = BYTE(cx2);

	switch (ether.Trans.CMD.Command)
	{
	case 46:
		{
			//Northing
			 ether.Trans.CMD.UTM_Northing = _tstof((LPCTSTR)Northing);
			 unsigned int wnum, utmN1, utmN2, utmN3, utmN4;
			 wnum = (unsigned int)(ether.Trans.CMD.UTM_Northing);

			 utmN1 = ((0xFF000000 & wnum) <<  24);
			 utmN2 = ((0x00FF0000 & wnum) << 16);
			 utmN3= ((0x0000FF00 & wnum) <<  8);
			 utmN4 = 0x000000FF & wnum;

			 ether.Data[10] = BYTE(utmN1);
			 ether.Data[11] = BYTE(utmN2);
			 ether.Data[12] = BYTE(utmN3);
			 ether.Data[13] = BYTE(utmN4);


			 //Easting
			 unsigned int Enum, utmE1, utmE2, utmE3, utmE4;
			 Enum = (unsigned int)(ether.Trans.CMD.UTM_Easting);

			 utmE1 = ((0xFF000000 & Enum) <<  24);
			 utmE2 = ((0x00FF0000 & Enum) <<  16);
			 utmE3= ((0x0000FF00 & Enum) <<  8);
			 utmE4 = 0x000000FF & Enum;

			 ether.Data[14] = BYTE(utmE1);
			 ether.Data[15] = BYTE(utmE2);
			 ether.Data[16] = BYTE(utmE3);
			 ether.Data[17] = BYTE(utmE4);


			 //Zone
			 double z = _tstof((LPCTSTR)Zone);
			 ether.Trans.CMD.UTM_Zone = (unsigned int) (z);
			 ether.Data[18] = BYTE(ether.Trans.CMD.UTM_Zone);


			 //hemisphere
			 ether.Data[19] = BYTE(ether.Trans.CMD.UTM_Hemi);


			 //Position Accuracy
			 unsigned int PA, PA1, PA2, PA3, PA4;
			 PA = (unsigned int)(ether.Trans.CMD.UTM_Pos);

			 PA1 = ((0xFF000000 & PA) <<  24);
			 PA2 = ((0x00FF0000 & PA) <<  16);
			 PA3= ((0x0000FF00 & PA) <<  8);
			 PA4 = 0x000000FF & PA;

			 ether.Data[20] = BYTE(PA1);
			 ether.Data[21] = BYTE(PA2);
			 ether.Data[22] = BYTE(PA3);
			 ether.Data[23] = BYTE(PA4);
			 

			 ether.Data[24] = 0;

			 dat = reinterpret_cast<char>(ether.Data);
			 sendto(s,  dat, strlen(dat), 0,(SOCKADDR*)& dat, sizeof(dat));

		}
		break;

	case 47:
		{
			//Latitude
			 ether.Trans.CMD.Geo_Lat = _tstof((LPCTSTR)txtLat);
			 unsigned int wnum1, Lat1, Lat2, Lat3, Lat4;
			 wnum1 = (unsigned int)(ether.Trans.CMD.Geo_Lat);

			 Lat1 = ((0xFF000000 & wnum1) <<  24);
			 Lat2 = ((0x00FF0000 & wnum1) <<  16);
			 Lat3= ((0x0000FF00 & wnum1) <<  8);
			 Lat4 = 0x000000FF & wnum1;

			 ether.Data[10] = BYTE(Lat1);
			 ether.Data[11] = BYTE(Lat2);
			 ether.Data[12] = BYTE(Lat3);
			 ether.Data[13] = BYTE(Lat4);


			 //Longtitude
			 ether.Trans.CMD.Geo_Long = _tstof((LPCTSTR)txtLong);
			 unsigned int wnum2, Long1, Long2, Long3, Long4;
			 wnum2 = (unsigned int)(ether.Trans.CMD.Geo_Long);

			 Long1 = ((0xFF000000 & wnum2) <<  24);
			 Long2 = ((0x00FF0000 & wnum2) <<  16);
			 Long3= ((0x0000FF00 & wnum2) <<  8);
			 Long4 = 0x000000FF & wnum2;

			 ether.Data[14] = BYTE(Long1);
			 ether.Data[15] = BYTE(Long2);
			 ether.Data[16] = BYTE(Long3);
			 ether.Data[17] = BYTE(Long4);



			 //Position Accuracy
			 ether.Trans.CMD.Geo_Pos = _tstof((LPCTSTR)txtPos);
			 unsigned int wnum3, Pos1, Pos2, Pos3, Pos4;
			 wnum3 = (unsigned int)(ether.Trans.CMD.Geo_Pos);

			 Pos1 = ((0xFF000000 & wnum3) <<  24);
			 Pos2 = ((0x00FF0000 & wnum3) <<  16);
			 Pos3= ((0x0000FF00 & wnum3) <<  8);
			 Pos4 = 0x000000FF & wnum3;

			 ether.Data[18] = BYTE(Pos1);
			 ether.Data[19] = BYTE(Pos2);
			 ether.Data[20] = BYTE(Pos3);
			 ether.Data[21] = BYTE(Pos4);

			 ether.Data[22] = 0;
			 ether.Data[23] = 0;
			 ether.Data[24] = 0;
			 ether.Data[25] = 0;
			 
		
			 dat = reinterpret_cast<char const="">(ether.Data);
			 sendto(s,  dat, strlen(dat), 0,(SOCKADDR*)& dat, sizeof(dat));
		}
		break;


		case 48:
		{
			//MGRS Northing
			ether.Trans.CMD.MGRS_Northing = _tstof((LPCTSTR)North_MGRS);
			 unsigned int wnum4, N1, N2, N3, N4;
			 wnum4 = (unsigned int)(ether.Trans.CMD.UTM_Northing);

			 N1 = ((0xFF000000 & wnum4) <<  24);
			 N2 = ((0x00FF0000 & wnum4) <<  16);
			 N3= ((0x0000FF00 & wnum4) <<  8);
			 N4 = 0x000000FF & wnum4;

			 ether.Data[10] = BYTE(N1);
			 ether.Data[11] = BYTE(N2);
			 ether.Data[12] = BYTE(N3);
			 ether.Data[13] = BYTE(N4);


			 //MGRS Easting
			 ether.Trans.CMD.MGRS_Easting = _tstof((LPCTSTR)East_MGRS);
			 unsigned int wnum5, E1, E2, E3, E4;
			 wnum5 = (unsigned int)(ether.Trans.CMD.UTM_Easting);

			 E1 = ((0xFF000000 & wnum5) << 24);
			 E2 = ((0x00FF0000 & wnum5) <<  16);
			 E3= ((0x0000FF00 & wnum5) <<  8);
			 E4 = 0x000000FF & wnum5;

			 ether.Data[14] = BYTE(E1);
			 ether.Data[15] = BYTE(E2);
			 ether.Data[16] = BYTE(E3);
			 ether.Data[17] = BYTE(E4);

			 //MGRS Row Letter
			 ether.Trans.CMD.MGRS_ROW = (unsigned int)((LPCTSTR)Row_MGRS);
			 ether.Data[18] = BYTE(ether.Trans.CMD.MGRS_ROW);

			 //MGRS Col Letter
			 ether.Trans.CMD.MGRS_COL = (unsigned int)((LPCTSTR)Col_MGRS);
			 ether.Data[19] = BYTE(ether.Trans.CMD.MGRS_COL);

	 		 //MGRS Zone
			 ether.Trans.CMD.MGRS_Zone = (unsigned int)((LPCTSTR)Zone_MGRS);
			 ether.Data[20] = BYTE(ether.Trans.CMD.MGRS_Zone);

			 //MGRS Zone Field
			 ether.Trans.CMD.MGRS_Field = (unsigned int)((LPCTSTR)Field_MGRS);
			 ether.Data[21] = BYTE(ether.Trans.CMD.MGRS_Field);

			 
			 //MGRS Position Accuracy
			 ether.Trans.CMD.MGRS_PosA = _tstof((LPCTSTR)Pos_MGRS);
			 unsigned int wnum6, Pos1, Pos2, Pos3, Pos4;
			 wnum6 = (unsigned int)(ether.Trans.CMD.MGRS_PosA);

			 Pos1 = ((0xFF000000 & wnum6) <<  24);
			 Pos2 = ((0x00FF0000 & wnum6) <<  16);
			 Pos3= ((0x0000FF00 & wnum6) <<  8);
			 Pos4 = 0x000000FF & wnum6;

			 ether.Data[22] = BYTE(Pos1);
			 ether.Data[23] = BYTE(Pos2);
			 ether.Data[24] = BYTE(Pos3);
			 ether.Data[25] = BYTE(Pos4);

			 ether.Data[26] = 0;
			
			 dat = reinterpret_cast<char const="">(ether.Data);
			 sendto(s,  dat, strlen(dat), 0,(SOCKADDR*)& dat, sizeof(dat));
		}
		break;
	}


	//UpdateData(FALSE);
	::closesocket(s);
	WSACleanup();

}
Posted
Updated 5-Mar-15 16:23pm
v3
Comments
Richard MacCutchan 23-Feb-15 4:56am    
You are using dat as the data buffer and the address of a socket. That code does not make much sense. You are also not checking the return value from sendto to see whether it succeeded.
KarstenK 23-Feb-15 5:12am    
insted of strlen() you better use sizeof() to get the buffer size. Encapsulate the code in some sub functions and check error codes!!!

I see you are using MFC in your program, but not for communication.
I would suggest you use the CAsyncSocket or the CSocket MFC classes.
It will make your life much easier using them.

Take look at the following link from MSDN:
https://msdn.microsoft.com/en-us/library/t7a47kk4.aspx[^]
 
Share this answer
 
I did not read all your code, but I have a problem understanding your binary operations.

Example:
C
utmN1 = ((0xFF000000 & wnum) <<  24);


This will always return 0. Why?

You take an unsigned int and make a binary AND operation with 0xFF000000.
You then get an unsigned int whose most significant byte equals wnum's one.
Then you left shift it by 24: you loose this information as the most significant bit is wiped away.

wnum(500,000,000):  0001 1101 1100 1101 0110 0101 0000 0000
AND FF000000     :  1111 1111 0000 0000 0000 0000 0000 0000
                    ---------------------------------------
                    0001 1101 0000 0000 0000 0000 0000 0000
Left-shift 24    :  0000 0000 0000 0000 0000 0000 0000 0000


The problem is the same for all other operations.

You may reconsider what you think your binary operations really achieve. Maybe you need a right-shift instead of a left-one to isolate each byte of your unsigned int?
Good luck :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900