socket的弱问题,困扰半天了,解决了马上结帖

topil 2004-09-30 11:04:23
我想实现一个很简单client和server的通讯,client发送个字符串过去,server进行验证,如果正确再给client送过去一个回复.但是在server验证时,老实不能通过(就是有个if语句,在比较字符串时老有问题)

搞定了马上结帖!!!!!

代码如下:
server端
Imports System.Net.Sockets
Imports System.Net

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建服务器端的Socket
Dim sServer As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim MyIP As IPAddress
'指定本地回路的IP地址
MyIP = IPAddress.Parse("127.0.0.1")
'设置服务器端的端口号
Dim ServerEP As New IPEndPoint(MyIP, 2001)
'设置客户端的端口号
Dim ClientEP As New IPEndPoint(MyIP, 2002)
'将Socket与端口绑定
sServer.Bind(ServerEP)
While True
Try
Dim bytes(100) As Byte
'等待客户端的请求
sServer.ReceiveFrom(bytes, ClientEP)
Dim str As String = System.Text.Encoding.ASCII.GetString(bytes)
Dim ans As String
If str.Trim.Equals("china") Then
ans = "pass"
Else
ans = "error"
End If
bytes = System.Text.Encoding.ASCII.GetBytes(ans)
'返回结果
sServer.SendTo(bytes, ClientEP)
Catch er As SocketException
MsgBox(er.ToString)
End Try
End While
End Sub
End Class

client端
Imports System.Net.Sockets
Imports System.Net

Public Class client
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建客户端的Socket
Dim cClient As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
'指定本地回路的IP地址
Dim MyIP As IPAddress
MyIP = IPAddress.Parse("127.0.0.1")
'设置服务器端的端口号
Dim ServerEP As New IPEndPoint(MyIP, 2001)
'设置客户端的端口号
Dim ClientEP As New IPEndPoint(MyIP, 2002)
'将Socket与端口绑定
cClient.Bind(ClientEP)
Try
Dim str As String = TxtInput.Text
Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(str)
'向服务器发出请求
cClient.SendTo(bytes, ServerEP)
Dim bytes2(100) As Byte
'接收服务器的响应结果
cClient.ReceiveFrom(bytes2, ServerEP)
str = System.Text.Encoding.ASCII.GetString(bytes2)
'将结果显示在文本框中
TxtDisplay.Text = str
Catch er As SocketException
MsgBox(er.ToString)
End Try
'关闭Socket
cClient.Close()

End Sub
End Class


大家帮忙啊,小弟感谢先
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
topil 2004-09-30
  • 打赏
  • 举报
回复
楼上的,我就是想判断从client端传入的字符串是否为一个固定的字符串,验证后在传回相应的结果,就我server端的那个代码,它为什么每次执行if语句后,都是走else,而两个字符确实的相等的啊。
wangsaokui 2004-09-30
  • 打赏
  • 举报
回复
一般是判断接收到的byte中是否包含某一专用的字符(作为结束标志),而不是判断是否是"china"这种固定的字符
wangsaokui 2004-09-30
  • 打赏
  • 举报
回复
你的Server端有问题

sServer.Bind(ServerEP)
While True
Try
Dim bytes(100) As Byte
'等待客户端的请求
sServer.ReceiveFrom(bytes, ClientEP)

绑定后没有监听
sServer.Listen(10)

后面也没有接受客户端发来的Socket
Dim handler As Socket = sServer.Accept()

参考这段Server端代码

Public Shared data As String = Nothing


Public Shared Sub StartListening()
' Data buffer for incoming data.
Dim bytes() As Byte = New [Byte](1024) {}

' Establish the local endpoint for the socket.
' Dns.GetHostName returns the name of the
' host running the application.
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, 11000)

' Create a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)

' Bind the socket to the local endpoint and
' listen for incoming connections.
Try
listener.Bind(localEndPoint)
listener.Listen(10)

' Start listening for connections.
While True
Console.WriteLine("Waiting for a connection...")
' Program is suspended while waiting for an incoming connection.
Dim handler As Socket = listener.Accept()
data = Nothing

' An incoming connection needs to be processed.
While True
bytes = New Byte(1024) {}
Dim bytesRec As Integer = handler.Receive(bytes)
data += Encoding.ASCII.GetString(bytes, 0, bytesRec)
If data.IndexOf("<EOF>") > - 1 Then
Exit While
End If
End While

' Show the data on the console.
Console.WriteLine("Text received : {0}", data)

' Echo the data back to the client.
Dim msg As Byte() = Encoding.ASCII.GetBytes(data)

handler.Send(msg)
handler.Shutdown(SocketShutdown.Both)
handler.Close()
End While

Catch e As Exception
Console.WriteLine(e.ToString())
End Try

Console.WriteLine(ControlChars.Cr + "Press ENTER to continue...")
Console.Read()
End Sub 'StartListening

topil 2004-09-30
  • 打赏
  • 举报
回复
现在是server端那里的语句
If str.Trim.Equals("china") Then
ans = "pass"
Else
ans = "error"
End If
从client传来字符串就是"china",str.Trim.Equals("china")的结果我监视了为true,可是每次都执行到else语句里面去。晕死了,到底是什么问题,其他地方没有问题的,就是这里
yuchun0607 2004-09-30
  • 打赏
  • 举报
回复
你应该写个线程来收吧,
把接收部分的代码写在线程里头,按button开始线程,然后在服务器端发,看看能不能收到

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧