绑定线程内的套接字(MFC) [英] Binding A Socket Inside a Thread (MFC)

查看:113
本文介绍了绑定线程内的套接字(MFC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这只是我使用套接字的第二个实例,我提前道歉,因为我对此事的知识明显有限。这就是我所坚持的:



我有一个对话框A.对话框A有一个按钮,可以打开对话框B.

它包含两个私有变量:

Hello everyone,
This is only my second instance at working with Sockets, I apologize in advance as my knowledge on the matter is clearly limited. Here''s what I''m stuck on:

I have a Dialog box A. Dialog A has a button which opens Dialog Box B.
It contains two private variables:

DialogB *receive;
CWnd *t_pParent;










void CDialogA::OnButton1() 
{
	// TODO: Add your control notification handler code here
     receive->Create(_T("STATIC"), NULL, WS_CHILD | WS_VISIBLE,CRect(0, 0, 20, 20),                           t_pParent, 1234);
     receive->ShowWindow(SW_SHOW);
}





创建是这样的:



Create does this:

BOOL DialogB::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
    // TODO: Add your specialized code here and/or call the base class

    return CDialog::Create(IDD, pParentWnd);
}





DialogB还包含下一步所需的所有IP和端口分配。



还有一个按钮,运行以下代码:



DialogB also contains all of the required IPs and port assignments required for the next step.

Is also has a button with runs the following code:

void DialogB::OnButton1()
{ 
    WORD SockVersionRequested;
    WSADATA wsa_Data;
 
	// Check if socket library initialization was completed successful. 
	SockVersionRequested = MAKEWORD(1, 1);
	if(WSAStartup(SockVersionRequested, &wsa_Data) == 0)
	{
			// Create an UDP socket (SOCK_DGRAM)
		socket_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

		if(socket_fd > 0)
		{
			// SBC!
			sender_adr.sin_family = AF_INET;
			sender_adr.sin_addr.s_addr = htonl(ip_sender);
			sender_adr.sin_port = htons(port_sender);

			// Display!
			receiver_adr.sin_family = AF_INET;
			receiver_adr.sin_addr.s_addr = htonl(ip_receiver);
			receiver_adr.sin_port = htons(port_receiver);

			// Bind the UDP socket to the port of receiver.
                        if(bind(socket_fd, (struct sockaddr *)&receiver_adr,sizeof(struct sockaddr_in)) == -1) 
			{
				closesocket(socket_fd);
				socket_fd = 0;
			}
			
			int sz;
	                int recv_data = 0;
	                char data_buffer[1024];
                        sz = sizeof(sockaddr_in);
                        recv_data = 0;
			recv_data = recvfrom(socket_fd, data_buffer, 1024, 0, (struct sockaddr *)&sender_adr, &sz);
			
		}
	}
}





此代码正在笔记本电脑上运行在连接到FPGA的LAN网络上,每隔5秒发送一次gretting消息广播。



在这种情况下,它无法接收并卡在recvfrom()上,但是我也从DialogA运行上面的代码,并能够正确接收问候消息。这就是我知道上面的问题来自于DialogB不是应用程序的主线程这一事实。



有谁知道如何让我正确从Dialog B收到问候语?

我会稍微查看一下,看看是否需要更多关于这个问题的信息。

谢谢!



This code is being run on a laptop on a LAN network connected to an FPGA transmitting a gretting message broadcast once every 5 seconds.

In this case however it is unable to receive and gets stuck on recvfrom(),however I have also run the above code from DialogA and been able to properly receive the greeting message. This is how I know that the issue above comes from the fact that DialogB is not the main thread of the application.

Does anyone know how a way for me to properly receive the greeting from Dialog B?
I''ll check back in a bit and see if more information on the issue is required.
Thanks!

推荐答案

我猜测你正在使用一个小小的Windows测试程序测试你的FPGA



几乎总是一个坏主意从应用程序的主线程或事件/消息处理程序(如OnButton1)调用套接字函数。主要原因(以及其他原因)是套接字功能通常会阻塞,具体取决于实际通信/以太网线路的状态,这不是您想要的。



基本的方法是:



1)创建另一个(通信)线程并启动它

2)通信线程将调用recvfrom为必要的,永远不会阻止主应用程序线程。因此,无论您的以太网数据包发生什么,您的用户界面都将正常工作。显然,如果您需要将数据发送到FPGA,您也可以在该线程中执行此操作。

3)当通信线程收到数据时,它应设置一个标志,告诉主应用程序线程数据已准备好显示。显然你需要注意数据缓冲区是由2个线程访问的,没有冲突。



类似的安排对我很有帮助。如果你仍然被卡住,请告诉我,我会挖出一些实际的代码示例。
I am guessing you are testing your FPGA using a little windows test program

It is almost always a bad idea to call socket functions from the application''s main thread, or event/message handlers (such as OnButton1). The main reason (and there are others) is that socket functions will commonly block depending on the status of the actual comms/Ethernet line which is not what you want.

The basic way would be:

1) Create another (comms) thread and start it
2) The comms thread will call recvfrom as necessary and never block the main app thread. So your user interface will work no matter what is happening with your Ethernet packets. Clearly if you needed to send data to FPGA you would do it in that thread too.
3) When data is received by the comms thread, it should set a flag that will tell the main app thread that data is ready to be displayed. Clearly you need to be careful that data buffer is accessed by 2 threads with no conflicts.

Similar arrangements work for me very well. If you are still stuck let me know I will dig out some actual code examples.


你可以使用不同的技术实现这一点。

首先是使用select( )模型。

http: //msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx [ ^ ]

其次是使用事件模型使用WSAEventSelect,WSAWaitForMultipleEvents等函数。

http://msdn.microsoft.com/en-us/library/windows/desktop/ms741576(v = vs.85).aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/windows/desktop/ms742219(V = vs.85)的.aspx [ ^ ]

第三种是在接收和发送时使用带有回调函数的重叠I / O.

http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancediomethod5h.html [ ^ ]

当然,对于TCP / IP协议,您也可以使用I / O完成端口。



我还建议你给安东尼琼斯一本书微软Windows网络编程。在winsock编程中满足您的要求可能会有所帮助。
You could achieve this using different techniques.
First is to use select() model.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx[^]
Second is to use event model using functions such as WSAEventSelect, WSAWaitForMultipleEvents etc.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms741576(v=vs.85).aspx[^]
http://msdn.microsoft.com/en-us/library/windows/desktop/ms742219(v=vs.85).aspx[^]
Third is to use overlapped I/O with callback function on receive and send.
http://www.winsocketdotnetworkprogramming.com/winsock2programming/winsock2advancediomethod5h.html[^]
Of course for TCP/IP protocol you also could use I/O completion ports.

I also recomend you a book ''Network Programming for Microsoft Windows'' by Anthony Jones. It might help to fulfill your requirements in winsock programming.


这篇关于绑定线程内的套接字(MFC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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