客户端异步套接字通信 [英] Client Asynchronous Socket Communications

查看:99
本文介绍了客户端异步套接字通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对特定打印机进行客户端异步套接字通信。  它允许端口3028上的TCP / IP通信。

I have a client Asynchronous Socket communications I am trying to do to a specific Printer.   It allows TCP/IP communications on port 3028.

我已成功连接到打印机并发送了一个备份命令,作为响应转储打印机备份数据。  一切都很好,但我使用的代码示例我在MSDN下找到

https://msdn.microsoft.com/en-us/library/bbx2eya8.aspx?f=255&MSPPError=-2147217396

I have successfully connected to the printer and sent a backup command which in response dumps the printers backup data.   All is good, however I am using a code example I found on MSDN under https://msdn.microsoft.com/en-us/library/bbx2eya8.aspx?f=255&MSPPError=-2147217396

私人

共享 Sub ReceiveCallback( ByVal ar
作为 IAsyncResult

SharedSubReceiveCallback(ByValar AsIAsyncResult)

< span style ="color:#008000; font-size:small"> '检索状态对象和客户端套接字

"。

Dim state
作为 StateObject =
CType (ar.AsyncState,
StateObject

Dimstate AsStateObject= CType(ar.AsyncState, StateObject)

Dim client
作为 套接字 =
州.workSocket

Dimclient AsSocket= state.workSocket

'从远程设备读取数据。

Dim bytesRead
作为 整数 =
client .EndReceive(ar)

DimbytesRead AsInteger= client.EndReceive(ar)

控制台 。WriteLine( " Ready
to read {0} bytes from server。"
,bytesRead)

Console.WriteLine("Ready to Read {0} bytes from server.", bytesRead)

如果 bytesRead> 0
然后

'可能有更多数据,因此存储到目前为止收到的数据。

            state.sb.Append (

            state.sb.Append(

编码 .ASCII.GetString(state.buffer,
0,bytesRead))

Encoding.ASCII.GetString(state.buffer, 0, bytesRead))

< span style ="color:#008000; font-size:small"> '获取其余数据。这就是HANGS

            client.BeginReceive(state .buffer,0,

            client.BeginReceive(state.buffer, 0,

StateObject .BufferSize,0,
AsyncCallback AddressOf ReceiveCallback),
州)

StateObject.BufferSize, 0, NewAsyncCallback(AddressOfReceiveCallback), state)

其他

'所有数据都已到达;将其作为回应。

如果 state.sb.Length> ; 1
然后

                response = state.sb.ToString()

                response = state.sb.ToString()

结束 如果

'表示已收到所有字节的信号。

            receiveDone.Set()

            receiveDone.Set()

结束 如果

结束 Sub 'ReceiveCallback

在ReceicveCallBack中,它正在检查传入的缓冲区以查看如果他们是任何数据,然后再次自我调用。  当它自己调用它时它就会挂起。  如果我发表评论它工作正常。  有人可以告诉我
我是什么我做错了吗?  显然它是对自身的递归调用,以确保处理整个缓冲区,当缓冲区长度达到0时,将完成并设置ReceiveDone.Set()以关闭调用。

In the ReceicveCallBack, it is checking the incoming buffer to see if they is any data and then calls itself again.   When it calls itself it just hangs.   If I comment that out It works fine.  Can someone tell me what I am doing wrong?   Apparently it is a recursive call to itself to ensure the entire buffer has been handled and when the buffer length reaches 0 will complete and set the ReceiveDone.Set() to close out the call.

对于我项目的下一部分,当我实际开始向打印机发送数据时,我将获得一个1字节的确认代码,它接收到数据,然后得到一个1字节的代码告诉我它有完成打印数据,然后我收到一个代码,用于收到光电池
的时间。当时我的程序格式化打印数据并将下一个打印数据发送到打印机并重新开始循环。

For the next part of my project, when I actually start sending data to the printer, I will get a 1 byte acknowledgement code it received the data, then get a 1 byte code to tell me it has finished printing the data, and then I get a code for when a photocell was received.  At that time my program format the print data and sends the next print data to the printer and starts the cycle all over again.

我还没找到事情没有,我怎么能提出一个事件来判断套接字缓冲区中的数据是否可用。

I have not found anything no how I can raise an event to tell when data is available in the socket buffer.

对此的任何帮助将不胜感激。

Any help on this would be greatly appreciated.

兰迪

推荐答案

Hello  Randy

Hello Randy

您的问题似乎与产品/性能有关。本论坛的目的是支持开放规范文档。您可以从
http://www.microsoft.com/阅读有关Microsoft Open规范程序的信息。 openspecifications / en / us / default.aspx


Your question seems to be product/performance related. The purpose of this forum is to support the Open Specifications documentation. You can read about the Microsoft Open Specifications program from http://www.microsoft.com/openspecifications/en/us/default.aspx

可以从
http://msdn.microsoft.com/en-us/library/dd208104(PROT .10).aspx




您的问题可能更适用于以下论坛:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=netdevelopment

The library of Open Specification documents can be accessed from http://msdn.microsoft.com/en-us/library/dd208104(PROT.10).aspx

Your question may be more applicable to the following forum : https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=netdevelopment

谢谢


这篇关于客户端异步套接字通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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