新搭建了一台win2003服务器,所有内容都是按照旧服务器设置的,能正常运转,但在做查询时有时会报错,使用的是socket通讯,实在不知哪里有问题,声明:不是全部不行,有的商户可以查询,有的商户查询不了,他们都调用同一个程序,把程序和截图粘出来,大家看下什么原因
VB程序代码如下:Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.IO
Imports System.Runtime.InteropServicesPublic Class StateObject
'SOCKET ID
Public workSocket As Socket = Nothing
'可接收数据大小
Public Const BufferSize As Integer = 1024 * 512
'Public Const BufferSize As Integer = 1024
'接收数据缓冲区
Public buffer(BufferSize) As Byte
'接收数据
Public sb As New StringBuilder
End ClassPublic Class ESA_TCP_CLIENT '处理结果
Public Enum ExSocketState
ExSocketSucceed = 0 '处理成功
ExSocketNoConnect = 1 '主机无连接
ExSocketUnKnown = 2 '未知状态(主机未响应)
ExSocketAllUsed = 3 '无可用端口(没有空闲状态的端口)
End Enum Private connectDone As New ManualResetEvent(False)
Private sendDone As New ManualResetEvent(False)
Private receiveDone As New ManualResetEvent(False)
Private response As String = String.Empty
Private iTimeOutM As Integer
Private isConn As Boolean = False Public Function SendTo(ByVal strHostIP As String, _
ByVal iPort As Integer, _
ByVal iTimeout As Integer, _
ByVal strSend As String, _
ByRef strRecv As String, _
ByVal Merch_ID As String, _
ByVal ULNm As String) As ExSocketState '增加Merch_ID,ULNm zfy 2011-1-6 用于生成Merch_ID.txt文件
On Error GoTo go_Err
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(strHostIP)
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim remoteEP As New IPEndPoint(ipAddress, iPort)
Dim client As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'2011-7-19 zfy
strRecv = ""
If iTimeout > 0 And iTimeout < 200000 Then '1000000
iTimeOutM = iTimeout
Else
iTimeOutM = 5000 '5000
End If
SendTo = ExSocketState.ExSocketUnKnown
client.BeginConnect(remoteEP, New AsyncCallback(AddressOf ConnectCallback), client) If connectDone.WaitOne(iTimeOutM, False) = True Then
If isConn = True Then
Send(client, strSend, Merch_ID, ULNm) '增加Merch_ID zfy 2011-1-6 用于生成Merch_ID.txt文件 (问题出在这)
If sendDone.WaitOne(iTimeOutM, False) = True Then
If isConn = True Then
'由于测试中出现异步通讯时接收主机报文有报文不完整情况
'所以在此处修改为在连接、发送时采用异步方式,而接收数据时采用同步方式
'此处还需要考虑同步接收的超时机制,暂略
Dim state As New StateObject
state.workSocket = client If Receive(state, Merch_ID, ULNm) Then '增加Merch_ID,ULNm zfy 2011-1-6 用于生成Merch_ID.txt文件
strRecv = state.sb.ToString()
If strRecv.Length > 10 Then
strRecv = strRecv.Substring(10, strRecv.Length - 10)
Else
strRecv = ""
End If
SendTo = ExSocketState.ExSocketSucceed
Else
SendTo = ExSocketState.ExSocketUnKnown
End If
Else
SendTo = ExSocketState.ExSocketUnKnown
End If
Else
SendTo = ExSocketState.ExSocketUnKnown
End If
Else
SendTo = ExSocketState.ExSocketNoConnect
End If
Else
SendTo = ExSocketState.ExSocketNoConnect
End If
go_Exit:
If client.Connected = True Then
client.Shutdown(SocketShutdown.Both)
End If
client.Close() Exit Function
go_Err:
GoTo go_Exit
End Function Public Function InitSocketAndConnect(ByVal strHostIP As String, _
ByVal iPort As Integer, _
ByVal iTimeout As Integer, _
ByVal strSend As String, _
ByRef strRecv As String, _
ByVal Merch_ID As String, _
ByVal ULNm As String) As ExSocketState Dim strLen As Integer
For n = 1 To strSend.Length
If AscW(Mid(strSend, n, 1)) > 256 Then strLen = strLen + 2 Else strLen = strLen + 1
Next
strSend = Format(strLen, "0000000000") & strSend
Dim FileToWrite As System.IO.FileStream = System.IO.File.Create("D:\程序\ESA_OPR_New\debug\Send" + Merch_ID + "_" + ULNm + ".txt")
'Dim rByte() As Byte = Encoding.Default.GetBytes(data.ToCharArray)
Dim rByte() As Byte = Encoding.Default.GetBytes(strSend.ToCharArray)
FileToWrite.Write(rByte, 0, rByte.Length)
FileToWrite.Close()
FileToWrite = Nothing
Try
Dim port As Integer = 6501
Dim host As String = "192.168.1.10"
Dim ip As IPAddress = IPAddress.Parse(host)
Dim ipe As New IPEndPoint(ip, port) '把ip和端口转化为IPEndPoint实例
Dim c As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '创建一个Socket
c.Connect(ipe) '连接到服务器
Dim byteData As Byte() = Encoding.Default.GetBytes(strSend)
c.Send(byteData, byteData.Length, 0) '发送测试信息 Dim recvStr As String = ""
Dim recvBytes As Byte() = New Byte(1023) {}
Dim bytes As Integer
bytes = c.Receive(recvBytes, recvBytes.Length, 0) '从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes)
'显示服务器返回信息
c.Close()
Catch e As ArgumentNullException Catch e As SocketException
'Console.WriteLine("SocketException: {0}", e)
End Try End Function
VB程序代码如下:Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.IO
Imports System.Runtime.InteropServicesPublic Class StateObject
'SOCKET ID
Public workSocket As Socket = Nothing
'可接收数据大小
Public Const BufferSize As Integer = 1024 * 512
'Public Const BufferSize As Integer = 1024
'接收数据缓冲区
Public buffer(BufferSize) As Byte
'接收数据
Public sb As New StringBuilder
End ClassPublic Class ESA_TCP_CLIENT '处理结果
Public Enum ExSocketState
ExSocketSucceed = 0 '处理成功
ExSocketNoConnect = 1 '主机无连接
ExSocketUnKnown = 2 '未知状态(主机未响应)
ExSocketAllUsed = 3 '无可用端口(没有空闲状态的端口)
End Enum Private connectDone As New ManualResetEvent(False)
Private sendDone As New ManualResetEvent(False)
Private receiveDone As New ManualResetEvent(False)
Private response As String = String.Empty
Private iTimeOutM As Integer
Private isConn As Boolean = False Public Function SendTo(ByVal strHostIP As String, _
ByVal iPort As Integer, _
ByVal iTimeout As Integer, _
ByVal strSend As String, _
ByRef strRecv As String, _
ByVal Merch_ID As String, _
ByVal ULNm As String) As ExSocketState '增加Merch_ID,ULNm zfy 2011-1-6 用于生成Merch_ID.txt文件
On Error GoTo go_Err
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(strHostIP)
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim remoteEP As New IPEndPoint(ipAddress, iPort)
Dim client As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'2011-7-19 zfy
strRecv = ""
If iTimeout > 0 And iTimeout < 200000 Then '1000000
iTimeOutM = iTimeout
Else
iTimeOutM = 5000 '5000
End If
SendTo = ExSocketState.ExSocketUnKnown
client.BeginConnect(remoteEP, New AsyncCallback(AddressOf ConnectCallback), client) If connectDone.WaitOne(iTimeOutM, False) = True Then
If isConn = True Then
Send(client, strSend, Merch_ID, ULNm) '增加Merch_ID zfy 2011-1-6 用于生成Merch_ID.txt文件 (问题出在这)
If sendDone.WaitOne(iTimeOutM, False) = True Then
If isConn = True Then
'由于测试中出现异步通讯时接收主机报文有报文不完整情况
'所以在此处修改为在连接、发送时采用异步方式,而接收数据时采用同步方式
'此处还需要考虑同步接收的超时机制,暂略
Dim state As New StateObject
state.workSocket = client If Receive(state, Merch_ID, ULNm) Then '增加Merch_ID,ULNm zfy 2011-1-6 用于生成Merch_ID.txt文件
strRecv = state.sb.ToString()
If strRecv.Length > 10 Then
strRecv = strRecv.Substring(10, strRecv.Length - 10)
Else
strRecv = ""
End If
SendTo = ExSocketState.ExSocketSucceed
Else
SendTo = ExSocketState.ExSocketUnKnown
End If
Else
SendTo = ExSocketState.ExSocketUnKnown
End If
Else
SendTo = ExSocketState.ExSocketUnKnown
End If
Else
SendTo = ExSocketState.ExSocketNoConnect
End If
Else
SendTo = ExSocketState.ExSocketNoConnect
End If
go_Exit:
If client.Connected = True Then
client.Shutdown(SocketShutdown.Both)
End If
client.Close() Exit Function
go_Err:
GoTo go_Exit
End Function Public Function InitSocketAndConnect(ByVal strHostIP As String, _
ByVal iPort As Integer, _
ByVal iTimeout As Integer, _
ByVal strSend As String, _
ByRef strRecv As String, _
ByVal Merch_ID As String, _
ByVal ULNm As String) As ExSocketState Dim strLen As Integer
For n = 1 To strSend.Length
If AscW(Mid(strSend, n, 1)) > 256 Then strLen = strLen + 2 Else strLen = strLen + 1
Next
strSend = Format(strLen, "0000000000") & strSend
Dim FileToWrite As System.IO.FileStream = System.IO.File.Create("D:\程序\ESA_OPR_New\debug\Send" + Merch_ID + "_" + ULNm + ".txt")
'Dim rByte() As Byte = Encoding.Default.GetBytes(data.ToCharArray)
Dim rByte() As Byte = Encoding.Default.GetBytes(strSend.ToCharArray)
FileToWrite.Write(rByte, 0, rByte.Length)
FileToWrite.Close()
FileToWrite = Nothing
Try
Dim port As Integer = 6501
Dim host As String = "192.168.1.10"
Dim ip As IPAddress = IPAddress.Parse(host)
Dim ipe As New IPEndPoint(ip, port) '把ip和端口转化为IPEndPoint实例
Dim c As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) '创建一个Socket
c.Connect(ipe) '连接到服务器
Dim byteData As Byte() = Encoding.Default.GetBytes(strSend)
c.Send(byteData, byteData.Length, 0) '发送测试信息 Dim recvStr As String = ""
Dim recvBytes As Byte() = New Byte(1023) {}
Dim bytes As Integer
bytes = c.Receive(recvBytes, recvBytes.Length, 0) '从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes)
'显示服务器返回信息
c.Close()
Catch e As ArgumentNullException Catch e As SocketException
'Console.WriteLine("SocketException: {0}", e)
End Try End Function
解决方案 »
- VS2010 编写VB程序运行时,为什么默认文本框的文字是被选定的?
- 我怎么样把一个文件夹中的文件名后缀为*.txt的文件按修改时间顺序列出来?
- 把一个字段里所有包含有某个字符串的文本替换成另一个字条串,用SQL应该怎么写?
- 怎样实现标志窗体?源代码如下,问题出在哪里?
- 如何使窗体标题由右向左流动????
- data 访问access数据库 数据库设置了密码怎么访问
- 简单问题:如果判断一张bmp的颜色深度是不是256色
- 救命啊………………………………………………救我%………………物理化学 杀人了
- VB直接打印窗体边距如何设置?
- 编译后,调用datareport的窗体出错:定义的应用程序或对象错误,是什么原因啊?
- vb新人求帮助 vb讲解视频
- 如何用VBA获取WORD中前十行的文本
On Error GoTo go_Err
Dim client As Socket = TryCast(ar.AsyncState, Socket)
If client IsNot Nothing Then
client.EndConnect(ar)
isConn = client.Connected
Else
isConn = False
End If
connectDone.Set()
go_Exit:
Exit Sub
go_Err:
isConn = False
connectDone.Set()
GoTo go_Exit
End Sub Private Function Receive(ByRef state As StateObject, ByVal Merch_ID As String, ByVal ULNm As String) As Boolean '增加Merch_ID,ULNm zfy 2011-1-6 用于生成Merch_ID.txt文件
On Error GoTo go_Err
Dim readStream As New NetworkStream(state.workSocket)
Dim blnReturn As Boolean = False
Dim intRcv As Integer
Dim strTmp As String = "" state.buffer.Clear(state.buffer, 0, StateObject.BufferSize)
readStream.ReadTimeout = iTimeOutM
intRcv = readStream.Read(state.buffer, 0, StateObject.BufferSize) Do While intRcv > 0
strTmp = Encoding.Default.GetString(state.buffer, 0, intRcv)
state.sb.Append(strTmp) If ConfigurationManager.AppSettings("g_IsDebug") = "1" Then
Dim FileToWrite As System.IO.FileStream = System.IO.File.Create("D:\程序\ESA_OPR_New_20130117\debug\Receive" + Merch_ID + "_" + ULNm + ".txt")
Dim rByte() As Byte = Encoding.Default.GetBytes(state.sb.ToString.ToCharArray)
FileToWrite.Write(rByte, 0, rByte.Length)
FileToWrite.Close()
FileToWrite = Nothing
End If '不按照报文头字符长度来取报文体字段,因为在测试中发现WINDOWS与UNIX在中文和特殊字符的长度判断上不相同
'所以采用查找结束符号的方式来判断报文尾
If InStr(strTmp, "</PACKAGE>") > 0 Then
Exit Do
Else
state.buffer.Clear(state.buffer, 0, StateObject.BufferSize)
intRcv = readStream.Read(state.buffer, 0, StateObject.BufferSize)
End If
Loop blnReturn = True readStream = Nothinggo_Exit:
Return blnReturn
Exit Function
go_Err:
blnReturn = False
GoTo go_Exit
End Function Private Sub Send(ByVal client As Socket, ByVal data As String, ByVal Merch_ID As String, ByVal ULNm As String) '增加Merch_ID,ULNm 2010-1-6 zfy
On Error GoTo go_Err Dim strLen As Integer
For n = 1 To data.Length
If AscW(Mid(data, n, 1)) > 256 Then strLen = strLen + 2 Else strLen = strLen + 1
Next
data = Format(strLen, "0000000000") & data
If ConfigurationManager.AppSettings("g_IsDebug") = "1" Then Dim FileToWrite As System.IO.FileStream = System.IO.File.Create("D:\程序\ESA_OPR_New_20130117\debug\Send" + Merch_ID + "_" + ULNm.ToString + ".txt")
Dim rByte() As Byte = Encoding.Default.GetBytes(data.ToCharArray)
FileToWrite.Write(rByte, 0, rByte.Length)
FileToWrite.Close()
FileToWrite = Nothing
End If
Dim byteData As Byte() = Encoding.Default.GetBytes(data) '2011-7-15 zfy 解决报文发送乱码问题
client.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), client)
go_Exit:
Exit Sub
go_Err:
GoTo go_Exit
End Sub Private Sub SendCallback(ByVal ar As IAsyncResult)
On Error GoTo go_Err
Dim client As Socket = TryCast(ar.AsyncState, Socket)
If client IsNot Nothing Then
isConn = True
Dim bytesSent As Integer = client.EndSend(ar)
Else
isConn = False
End If
sendDone.Set()
go_Exit:
Exit Sub
go_Err:
isConn = False
sendDone.Set()
GoTo go_Exit
End Sub
End Class查询界面:查询结果: