求助:还是编码转换问题,网络

a11s 2004-09-10 10:55:56
我用MSDN提供的例子居然没有任何用处.不知道M$怎么想的
服务器:219.146.191.100 :6666 或者 6667
发出任意长度少于255 的字符串回得到服务器回复
问题是无法在客户端还原,所以请大家帮助
看看你们收到了什么回复.
我的代码就是这样的 发送Textbox1 收到 JU5=PEO":TextBox1

===============================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim iphost As IPHostEntry = Dns.Resolve("www.rzvnet.com")
'Dim aliases() As String = iphost.Aliases
Dim addr() As IPAddress = iphost.AddressList
'Dim ep As New IPEndPoint(addr(0), 6666)
Dim tcpc As New TcpClient

Connect(addr(0).ToString, TextBox1.Text)



End Sub
Sub Connect(ByVal server As [String], ByVal message As [String])
Try
' Create a TcpClient.
' Note, for this client to work you need to have a TcpServer
' connected to the same address as specified by the server, port
' combination.
Dim port As Int32 = 6666
Dim client As New TcpClient(server, port)

' Translate the passed message into ASCII and store it as a Byte array.
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)

' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
Dim stream As NetworkStream = client.GetStream()

' Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length)

'Console.WriteLine("Sent: {0}", message)

' Receive the TcpServer.response.
' Buffer to store the response bytes.
data = New [Byte](256) {}

' String to store the response ASCII representation.
Dim responseData As [String] = [String].Empty

' Read the first batch of the TcpServer response bytes.
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
TextBox2.Text = cv(responseData)
'TextBox2.Text = Encoding.GetEncoding("GB2312").GetString(Encoding.UTF8.GetBytes(responseData))
' Close everything.
client.Close()
Catch e As ArgumentNullException
Console.WriteLine("ArgumentNullException: {0}", e)
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try

'Console.WriteLine(ControlChars.Cr + " Press Enter to continue...")
'Console.Read()
End Sub 'Connect
Function cv(ByVal UnicodeString As String) As String
'Dim unicodeString As String = "This string contains the unicode character Pi(?"
'UnicodeString = "This string contains the unicode character Pi(?"

' Create two different encodings.
Dim ascii As Encoding = Encoding.ASCII
Dim [unicode] As Encoding = Encoding.Unicode

' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(UnicodeString)

' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)

' Convert the new byte[] into a char[] and then into a string.
' This is a slightly different approach to converting to illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)

' Display the strings created before and after the conversion.
Return asciiString
End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox(cv(TextBox2.Text))
End Sub
...全文
138 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
a11s 2004-09-12
  • 打赏
  • 举报
回复
还是自己顶
a11s 2004-09-11
  • 打赏
  • 举报
回复
为了解答别人的问题,居然把自己的帖子弄沉了,失败
a11s 2004-09-11
  • 打赏
  • 举报
回复
还是JU5=PEO":TextBox1
您自己尝试成功了么?
连接6666或者6667试试
速马 2004-09-10
  • 打赏
  • 举报
回复
Encoding.ASCII -> Encoding.Default或者Encoding.UTF8
不能用Encoding.ASCII模式传非Western字符,并且两边的编码格式要相同
a11s 2004-09-10
  • 打赏
  • 举报
回复
自己顶

16,553

社区成员

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

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