VB.NET套接字编程网络流错误 [英] VB.NET Socket Programming Network stream Error
问题描述
我正在做一个大学项目.该项目是网络银行系统.我设计了两个程序,一个用于服务器(管理员),一个用于客户端(客户).通过TCP套接字进行连接.这是一个简短的说明:
当客户(客户)通过正确提供帐号和密码登录时,networkstream对象会正确传输帐号,但密码情况并非如此. 当我在服务器端提取密码时,它不是适合验证数据库中用户详细信息的Sql查询的有效字符串.密码字符串看起来像"abc,缺少双引号.缓冲区大小为10025.
这是服务器代码:
I am working on a college project . The project is network Banking system. I have designed two programs, one for server(admin) and one for the client(customers). The connection is via TCP sockets. here is a brief description:
When the customer(client) logs in by providing the account number and password correctly, the networkstream object transmits account number properly but this is not the case with passwords. When I extract the password on the server side it is not a valid string to fit into the Sql query for verifying the user details in the database. The password String looks like "abc with a missing closing double quotes. The buffer size is 10025.
Here is the server code:
Public Sub Client_Login()
networkStream = clientSocket.GetStream()
Dim bytes(10024) As Byte
networkStream.Read(bytes, 0, CInt(clientSocket.ReceiveBufferSize))
ClientID = (Encoding.ASCII.GetString(bytes))
networkStream.Flush()
networkStream.Read(bytes, 0, CInt(clientSocket.ReceiveBufferSize))
ClientPassword = (Encoding.ASCII.GetString(bytes))
networkStream.Flush()
connection = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;data source= D:\Project(Pritam)\Networking Banking System\ActualDatabase.mdb")
SQL = "Select * from ClientInfo where Account_No= " & ClientID & " and [Password]='" & ClientPassword & "'"
command = New OleDbCommand(SQL, connection)
'Try
connection.Open()
reader = command.ExecuteReader()
reader.Read()
If reader.HasRows = True Then
Dim ClientAccountNumber As Integer = reader("Account_No") 'extracts account number from database
Dim PasswordClient As String = reader("Password") ' extracts password from database
If ClientAccountNumber = Val(ClientID) And PasswordClient = (ClientPassword) Then
server_response = "Successful Login"
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(server_response)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
这是客户端代码:
Here is the client code:
Private Sub cmdSignin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSignin.Click
If txtClientID.Text <> "" And txtClientPassword.Text <> "" Then
Try
networkStream = clientSocket.GetStream()
Dim to_server As Byte() = Encoding.ASCII.GetBytes(txtClientID.Text)
networkStream.Write(to_server, 0, to_server.Length)
'networkStream.Flush()
'Dim client_writer As New StreamWriter(clientSocket.GetStream())
'client_writer.Write(txtClientPassword.Text)
to_server = Encoding.ASCII.GetBytes(txtClientPassword.Text)
networkStream.Write(to_server, 0, to_server.Length)
networkStream.Flush()
form_refresh()
Dim login_Thread As Threading.Thread = New Threading.Thread(AddressOf login_final)
login_Thread.Start()
Catch ex As Exception
MsgBox("Server not found" & vbCrLf & "Contact Administrator", MsgBoxStyle.Critical, "Notice!!")
Me.Close()
End Try
Else
请帮助我,因为我很快就要提交我的项目.
预先感谢您
--EDIT kschuler:尝试修复代码块,对实际问题加粗以使其更引人注意.
Kindly help me as I''ve to submit my project very soon, please.
Thanking you in advance
--EDIT kschuler: tried to fix code blocks, bolded the actual question to make it stand out more.
推荐答案
这是由于缓冲区的大小是固定的.使它动态,它应该可以正常工作.
This is due to your buffer being a fixed size. Make it dynamic and it should work fine.
Dim bytes() As Byte
Redim bytes(CInt(clientSocket.ReceiveBufferSize)-1)
networkStream.Read(bytes, 0, CInt(clientSocket.ReceiveBufferSize))
公平地说,我很惊讶您的两组数据没有合并到单个网络数据包中.在VB6中曾经发生过这种情况,也许.NET处理的数据包略有不同
To be fair, I am quite surprised that your two sets of data didn''t get merged into a single network packet. That used to happen in VB6, maybe .NET handles packets slightly different
这篇关于VB.NET套接字编程网络流错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!