使用套接字发送和接收数据. [英] send-receive data with socket.
本文介绍了使用套接字发送和接收数据.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我想与条形码打印机连接,并向他发送一些特定的字符,并取回他们发送给我的所有内容
在VB6中,我使用了winsock,并且(对我而言)事情很简单....
Hi to all
I want to connect with a barcode printer and send him some specific characters and take back anything they send to me
In VB6 i have use the winsock and (for me) the things is simple....
Winsock1(totlistsite_w).RemoteHost = ListView1.ListItems(totlistsite_w).SubItems(1)
Winsock1(totlistsite_w).RemotePort = ListView1.ListItems(totlistsite_w).SubItems(2)
Winsock1(totlistsite_w).Connect
Do
txtCounter = ""
If Return_Status.Enabled = False Then Exit Do
If ListView1.ListItems(totlistsite_w).ListSubItems(6) = "" Then Exit Do
DoEvents
If Winsock1(totlistsite_w).State = 7 Then
Winsock1(totlistsite_w).SendData "!V32 1" & vbCrLf
txtCounter = ReadString1(Winsock1(totlistsite_w))
End If
Loop Until Winsock1(totlistsite_w).State = 7 Or Winsock1(totlistsite_w).State = 9
Winsock1(totlistsite_w).Close
请您能帮我用vb.net做些类似的事情.
感谢和问候.
Please could you help me to make something similar with vb.net.
Thanks and regards.
推荐答案
^ ]线程说明了如何在.Net中使用VB6.0 Winsock控件.
如果您想完全使用.NET,请此 [
This[^] thread explains how you can use the VB6.0 Winsock Control in .Net.
If you want to go totally .NET though, this[^] will get you started.
以及VB.net中的代码
ked ....
And the code in VB.net
Cheked.................
Dim i, j As Integer
Dim FindBlank As String
Dim txtonlyCounter As Integer = 0
Dim tcpClient As New System.Net.Sockets.TcpClient
tcpClient.Connect(PrinterIP, PrinterPort)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
Dim sendBytes As [Byte]() = System.Text.ASCIIEncoding.ASCII.GetBytes("!V32 1" & vbCrLf)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Dim bytes1(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes1, 0, CInt(tcpClient.ReceiveBufferSize))
Dim returndata As String = ""
returndata = Encoding.ASCII.GetString(bytes1)
tcpClient.Close()
Dim txtCounterLength As Integer = Len(returndata)
For i = 1 To txtCounterLength
FindBlank = Mid(returndata, i, 1)
If FindBlank = " " Then
j = i - 1
txtonlyCounter = Mid(returndata, 1, j)
Exit For
End If
Next i
Try
ListView1.Items(PrinterIndex).SubItems(12).Text = txtonlyCounter
Catch
ListView1.Items.Item(PrinterIndex).SubItems.Add(12).Text = txtonlyCounter
End Try
Else
If Not networkStream.CanRead Then
'MsgBox("Could not write data to data stream")
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
'MsgBox("Could not read data from data stream")
tcpClient.Close()
End If
End If
End If
这篇关于使用套接字发送和接收数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文