VB.NET 服务器与客户端数据交互问题?
VB.NET中由客户端向服务器提交信息,然后服务器端与数据库进行数据交互,处理完成后把处理结果返回给客户端,提交信息允许多个客户端同时向服务器提交数据,请问如何实现,往各...
VB.NET 中由客户端向服务器提交信息,然后服务器端与数据库进行数据交互,处理完成后把处理结果返回给客户端,提交信息允许多个客户端同时向服务器提交数据,请问如何实现,往各位大侠指点。 最好是有源码,谢谢了!~
展开
3个回答
展开全部
客户端简单代码:
Imports System.Net.Sockets
Imports System.IO
Private Output As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Dim StrSend As String
Dim client As New TcpClient '
client.Connect(servIP, 2006)
Output = client.GetStream
Writer = New BinaryWriter(Output)
Reader = New BinaryReader(Output)
StrSend = "字符串"
System.Threading.Thread.Sleep(100)
Writer.Write(StrSend)
Try
Message = Reader.ReadString
If Message = "OK" Then
LBinfo.Text = "成功!" '测试
Else
LBinfo.Text = "失败!"
End If
System.Threading.Thread.Sleep(100)
Catch ex As Exception
MessageBox.Show("Client Application Closing!")
Finally
Writer.Close()
Reader.Close()
Output.Close()
client.Close()
End Try
服务器端
用线程
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Private Connection As Socket
Private readThread As Thread
Private SocketStream As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
readThread = New Thread(AddressOf RunServer)
readThread.Start()
End Sub
Public Sub RunServer()
Dim Listener As TcpListener
Try
Listener = New TcpListener(Net.IPAddress.Parse("192.168.1.16"), 2006)
Listener.Start()
While True
Connection = Listener.AcceptSocket 'accept an incoming connection
SocketStream = New NetworkStream(Connection) 'create networkstream object associated with socket
Writer = New BinaryWriter(SocketStream) 'create object for transferring data across stream
Reader = New BinaryReader(SocketStream) 'read string data sent from client
Try
Do
strIncept = Reader.ReadString
strIncept = DataDisp(strIncept) '此处插入数据处理程序()
SendInfoToClient(strIncept) 'send to client
Loop While Connection.Connected
Catch ex As Exception
MsgBox(ex.ToString)
Finally
Writer.Close()
Reader.Close()
SocketStream.Close()
End Try
End While
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SendInfoToClient(ByVal Info As String)
'Send to client
Try
If Connection.Connected Then
Writer.Write(Info)
Connection.Close()
End If
Catch ex As SocketException
MessageBox.Show(ex.ToString)
End Try
End Sub
整个过程都有了
Imports System.Net.Sockets
Imports System.IO
Private Output As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Dim StrSend As String
Dim client As New TcpClient '
client.Connect(servIP, 2006)
Output = client.GetStream
Writer = New BinaryWriter(Output)
Reader = New BinaryReader(Output)
StrSend = "字符串"
System.Threading.Thread.Sleep(100)
Writer.Write(StrSend)
Try
Message = Reader.ReadString
If Message = "OK" Then
LBinfo.Text = "成功!" '测试
Else
LBinfo.Text = "失败!"
End If
System.Threading.Thread.Sleep(100)
Catch ex As Exception
MessageBox.Show("Client Application Closing!")
Finally
Writer.Close()
Reader.Close()
Output.Close()
client.Close()
End Try
服务器端
用线程
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Private Connection As Socket
Private readThread As Thread
Private SocketStream As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
readThread = New Thread(AddressOf RunServer)
readThread.Start()
End Sub
Public Sub RunServer()
Dim Listener As TcpListener
Try
Listener = New TcpListener(Net.IPAddress.Parse("192.168.1.16"), 2006)
Listener.Start()
While True
Connection = Listener.AcceptSocket 'accept an incoming connection
SocketStream = New NetworkStream(Connection) 'create networkstream object associated with socket
Writer = New BinaryWriter(SocketStream) 'create object for transferring data across stream
Reader = New BinaryReader(SocketStream) 'read string data sent from client
Try
Do
strIncept = Reader.ReadString
strIncept = DataDisp(strIncept) '此处插入数据处理程序()
SendInfoToClient(strIncept) 'send to client
Loop While Connection.Connected
Catch ex As Exception
MsgBox(ex.ToString)
Finally
Writer.Close()
Reader.Close()
SocketStream.Close()
End Try
End While
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SendInfoToClient(ByVal Info As String)
'Send to client
Try
If Connection.Connected Then
Writer.Write(Info)
Connection.Close()
End If
Catch ex As SocketException
MessageBox.Show(ex.ToString)
End Try
End Sub
整个过程都有了
展开全部
最简单的办法是把“服务器端”做成一个服务 服务有很多种 例如wcf和webservice
可以把这种类型的服务看成一个远程的类库 wcf有很完善的并发处理机制 可以满足大量用户同时提交数据 而webservice比较简单易用 并发处理和数据吞吐量的能力不如wcf
具体如何实现这种结构 到网上查查有很多例子
然后是如果服务器必需是一个应用程序的话 那还可以使用socket
套接字又分为udp和TCP两大类 udp是无服务端的 也就是说客户端可以独立运行 而TCP是必需开启服务端 客户端才可以运行的 而udp和tcp相对于上面所说的服务结构开发会复杂很多 并发情况和数据吞吐量也很难控制
socket介绍:
ht tp: //ba ike.baidu.c om/vi ew/538 713.h tm
WCF介绍:
ht tp: //ba ike.baidu.c om/vi ew/1140 438.h tm
WebService介绍:
ht tp: //ba ike.baidu.c om/vi ew/837 392.h tm
自己去掉空格 不然回答要被审核多久 你懂的
可以把这种类型的服务看成一个远程的类库 wcf有很完善的并发处理机制 可以满足大量用户同时提交数据 而webservice比较简单易用 并发处理和数据吞吐量的能力不如wcf
具体如何实现这种结构 到网上查查有很多例子
然后是如果服务器必需是一个应用程序的话 那还可以使用socket
套接字又分为udp和TCP两大类 udp是无服务端的 也就是说客户端可以独立运行 而TCP是必需开启服务端 客户端才可以运行的 而udp和tcp相对于上面所说的服务结构开发会复杂很多 并发情况和数据吞吐量也很难控制
socket介绍:
ht tp: //ba ike.baidu.c om/vi ew/538 713.h tm
WCF介绍:
ht tp: //ba ike.baidu.c om/vi ew/1140 438.h tm
WebService介绍:
ht tp: //ba ike.baidu.c om/vi ew/837 392.h tm
自己去掉空格 不然回答要被审核多久 你懂的
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改改服务器的代码就好了,你用的是winsock控件把,用winsock控件组,再用load加载控件,然后客户端来一个申请你就跟一个,算了,给你代码好了- -
winsock的index属性设为0
Private Sub FORM_load()
For i = 1 To 100
Load Winsock1(i)
Next i
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
For i = 1 To 100
If Winsock1(i).State = 0 Then
Winsock1(i).Close
Winsock1(i).Accept requestID
Exit Sub
End If
Next i
End Sub
把你代码框里和这两条有关的删去,再复制,form_load 里的内容看着改一改就可以,不过必须加载
winsock的index属性设为0
Private Sub FORM_load()
For i = 1 To 100
Load Winsock1(i)
Next i
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
For i = 1 To 100
If Winsock1(i).State = 0 Then
Winsock1(i).Close
Winsock1(i).Accept requestID
Exit Sub
End If
Next i
End Sub
把你代码框里和这两条有关的删去,再复制,form_load 里的内容看着改一改就可以,不过必须加载
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询