| 分类: 只谈技术 |
TCPClient 是在VB中建立客户端所使用的要NEW的量,对于客户端,采用的方式是首先声明一个TCPClient的变量client,然后用connect方法建立与服务器端的连接,再建立一个NetworkStream的新变量(这个变量要等于Client.GetStream()这是client建立的通道),这个变量可以使用write方法向建立的服务器端发送消息,也可是使用beginread方法从服务器端读取发来的消息。具体的程序如下
Dim client as new TCPClient
Client.connect(SeverIP,PORT)
Try
Synclock
client.getstream()
End synclock
End try
Try
Dim readstream as networkstream=client.getstream()
Readstream.begin(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
‘readbuffer 用来存储读到的内容
‘0为开始端,READ_BUFFER_SIZE 设置读取的内容大小
‘AddressOf DoRead 作用是消息内容的处理
End try
Doread(ByVal ar As IAsyncResult)
{
Dim BytesRead As Integer
Try
End try
}
‘获取消息类型
Dim MessageStr As String = Encoding.Default.GetString(readBuffer, intBegin, BytesRead)

加载中…