问题描述
我正在编写聊天应用程序,因此我需要创建登录服务器"作为第一步.我为客户端应用程序编写了代码:
<br /> Imports System.Net<br /> Imports System.Net.Sockets<br /> Imports System.Text<br /> Imports System.IO<br /> Imports System.Threading<br /> Public Class frmLogin<br /> <br /> <br /> Private Sub btnSignIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSignIn.Click<br /> If txtServer.Text = "" Or txtUName.Text = "" Then<br /> MsgBox("Popunite sva polja na formi")<br /> Else<br /> Dim tcpClient As New TcpClient()<br /> tcpClient.Connect(txtServer.Text, 8080)<br /> Dim sendBytes As Byte()<br /> sendBytes = Encoding.UTF8.GetBytes("LOGIN>" + txtUName.Text)<br /> tcpClient.GetStream.Write(sendBytes, 0, sendBytes.Length)<br /> End If<br /> End Sub<br /> End Class<br />
;
我还编写了用于文件传输的代码:
导入System.Threading
导入System.Net
导入System.Net.Sockets
导入System.Text
导入System.IO
公共课程表格1
私有alSockets为ArrayList
公共子listenerThread()
将tcpListener设为新的TcpListener(8080)
昏暗处理程序套接字作为套接字
将thdstHandler调暗为ThreadStart
昏暗的thdHandler作为线程
tcpListener.Start()
做
handlerSocket = tcpListener.AcceptSocket()
如果handlerSocket.Connected然后
lbConnections.Items.Add(_
handlerSocket.RemoteEndPoint.ToString()+ _
已连接.")
SyncLock(Me)
alSockets.Add(handlerSocket)
结束SyncLock
thdstHandler = New ThreadStart(AddressOf _
handlerThread)
thdHandler =新线程(thdstHandler)
thdHandler.Start()
如果结束
循环
结束子
公共Sub handlerThread()
昏暗处理程序套接字作为套接字
handlerSocket = alSockets(alSockets.Count-1)
昏暗的networkStream为NetworkStream =新_
NetworkStream(handlerSocket)
Dim块大小为Int16 = 1024
将这个读为Int16
昏暗dataByte(blockSize)作为字节
SyncLock Me
''只有一个进程可以访问
''在任何给定时间相同的文件
Dim fileStream作为流
fileStream = File.OpenWrite("e:\ upload.txt")
While(True)
thisRead = networkStream.Read(dataByte,_
0,blockSize)
fileStream.Write(dataByte,0,dataByte.Length)
如果thisRead = 0,则退出While
结束时
fileStream.Close()
结束SyncLock
lbConnections.Items.Add(已写文件")
handlerSocket =没什么
结束子
私有子Form1_Load(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
将IPHost变暗为IPHostEntry
IPHost = Dns.GetHostByName(Dns.GetHostName())
lblStatus.Text =我的IP地址是" + _
IPHost.AddressList(0).ToString()
alSockets =新的ArrayList()
将thdListener设为新线程(新ThreadStart _
(AddressOf listenerThread))
thdListener.Start()
结束子
结束类
我想知道是否可以修改此代码以使用TCP协议发送和接收文本消息?
发送文本,而不是文件.您不知道该怎么做?
它从套接字读取并将数据保存到文件.它从套接字读取的内容是字节-可以是任何字节,但是在这种情况下,您已经说过它是一个文件.如果客户端使用字符串"Hello",将其转换为字节数组并发送该字符串,则服务器所需要做的就是获取从networkStream.Read
接收的字节数组并将其转换回字符串.
I''m working on programming a chat application and I need to create "login to server" as a first step. I wrote a code for client application:
<br /> Imports System.Net<br /> Imports System.Net.Sockets<br /> Imports System.Text<br /> Imports System.IO<br /> Imports System.Threading<br /> Public Class frmLogin<br /> <br /> <br /> Private Sub btnSignIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSignIn.Click<br /> If txtServer.Text = "" Or txtUName.Text = "" Then<br /> MsgBox("Popunite sva polja na formi")<br /> Else<br /> Dim tcpClient As New TcpClient()<br /> tcpClient.Connect(txtServer.Text, 8080)<br /> Dim sendBytes As Byte()<br /> sendBytes = Encoding.UTF8.GetBytes("LOGIN>" + txtUName.Text)<br /> tcpClient.GetStream.Write(sendBytes, 0, sendBytes.Length)<br /> End If<br /> End Sub<br /> End Class<br />
;
I also wrote a code for file transfer:
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Public Class Form1
Private alSockets As ArrayList
Public Sub listenerThread()
Dim tcpListener As New TcpListener(8080)
Dim handlerSocket As Socket
Dim thdstHandler As ThreadStart
Dim thdHandler As Thread
tcpListener.Start()
Do
handlerSocket = tcpListener.AcceptSocket()
If handlerSocket.Connected Then
lbConnections.Items.Add( _
handlerSocket.RemoteEndPoint.ToString() + _
"connected.")
SyncLock (Me)
alSockets.Add(handlerSocket)
End SyncLock
thdstHandler = New ThreadStart(AddressOf _
handlerThread)
thdHandler = New Thread(thdstHandler)
thdHandler.Start()
End If
Loop
End Sub
Public Sub handlerThread()
Dim handlerSocket As Socket
handlerSocket = alSockets(alSockets.Count - 1)
Dim networkStream As NetworkStream = New _
NetworkStream(handlerSocket)
Dim blockSize As Int16 = 1024
Dim thisRead As Int16
Dim dataByte(blockSize) As Byte
SyncLock Me
'' Only one process can access the
'' same file at any given time
Dim fileStream As Stream
fileStream = File.OpenWrite("e:\upload.txt")
While (True)
thisRead = networkStream.Read(dataByte, _
0, blockSize)
fileStream.Write(dataByte, 0, dataByte.Length)
If thisRead = 0 Then Exit While
End While
fileStream.Close()
End SyncLock
lbConnections.Items.Add("File Written")
handlerSocket = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim IPHost As IPHostEntry
IPHost = Dns.GetHostByName(Dns.GetHostName())
lblStatus.Text = "My IP address is " + _
IPHost.AddressList(0).ToString()
alSockets = New ArrayList()
Dim thdListener As New Thread(New ThreadStart _
(AddressOf listenerThread))
thdListener.Start()
End Sub
End Class
I was wondering if it is possible to modify this code for sending and receiving text messages using TCP protocol???
sends text, not a file. What part do you not know how to do?
it reads from the socket and saves the data to file. what it reads from the socket are bytes - those could be anything but in this case you have said that its a file. If a client takes a string "Hello", converts it to an array of bytes and sends that, then all the server needs to do is take the byte array it receives from networkStream.Read
and convert it back into a string.
这篇关于聊天应用程序Visual Basic 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!