通过UDP协议发送unisgned char数组的问题 [英] Problem in sending unisgned char arrays through UDP protocol

查看:121
本文介绍了通过UDP协议发送unisgned char数组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我遇到问题的代码。场景:一旦我点击命令按钮,它就会触发函数Command_Msg(),它将通过UDP发送无符号字符数组。但是,它不起作用。即使编译时没有错误,也没有任何内容被发送到接收方。有什么建议吗?



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?

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();



}


}

推荐答案

我看到你在你的程序中使用MFC但是不能用于通信。

我建议你使用CAsyncSocket或CSocket MFC类。

这将使你的生活更容易使用它们。



请查看以下来自MSDN的链接:

https://msdn.microsoft.com/en-us/library/t7a47kk4.aspx [ ^ ]
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[^]


我没有阅读您的所有代码,但我在理解您的二进制操作时遇到了问题。



示例:

I did not read all your code, but I have a problem understanding your binary operations.

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





这将始终返回0.为什么?



你取一个unsigned int并进行二元AND操作n使用0xFF000000。

然后你得到一个unsigned int,其最重要的字节等于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





所有其他操作的问题都是一样的。



您可能会重新考虑您认为您的二元操作真正实现的目标。也许你需要一个右移而不是一个左移来隔离你的unsigned int的每个字节?

祝你好运:)



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 :)


这篇关于通过UDP协议发送unisgned char数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆