60 lines
2.0 KiB
VB.net
60 lines
2.0 KiB
VB.net
Imports System.Net.Sockets
|
|
Imports System.Text
|
|
Public Class Form1
|
|
Dim clientSocket As New System.Net.Sockets.TcpClient()
|
|
Dim serverStream As NetworkStream
|
|
Dim readData As String
|
|
Dim infiniteCounter As Integer
|
|
|
|
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
|
|
readData = "Connected to Chat Server ..."
|
|
msg()
|
|
clientSocket.Connect("127.0.0.1", 8888)
|
|
'Label1.Text = "Client Socket Program - Server Connected ..."
|
|
serverStream = clientSocket.GetStream()
|
|
|
|
Dim outStream As Byte() =
|
|
System.Text.Encoding.ASCII.GetBytes(TextBox1.Text + "$")
|
|
serverStream.Write(outStream, 0, outStream.Length)
|
|
serverStream.Flush()
|
|
|
|
Dim ctThread As Threading.Thread =
|
|
New Threading.Thread(AddressOf getMessage)
|
|
ctThread.Start()
|
|
End Sub
|
|
Private Sub getMessage()
|
|
For infiniteCounter = 1 To 2
|
|
infiniteCounter = 1
|
|
serverStream = clientSocket.GetStream()
|
|
Dim buffSize As Integer
|
|
Dim inStream(10024) As Byte
|
|
buffSize = clientSocket.ReceiveBufferSize
|
|
Try
|
|
serverStream.Read(inStream, 0, buffSize)
|
|
|
|
Catch ex As Exception
|
|
|
|
End Try
|
|
Dim returndata As String =
|
|
System.Text.Encoding.ASCII.GetString(inStream)
|
|
readData = "" + returndata
|
|
msg()
|
|
Next
|
|
End Sub
|
|
|
|
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
|
|
Dim outStream As Byte() =
|
|
System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
|
|
serverStream.Write(outStream, 0, outStream.Length)
|
|
serverStream.Flush()
|
|
End Sub
|
|
Private Sub msg()
|
|
If Me.InvokeRequired Then
|
|
Me.Invoke(New MethodInvoker(AddressOf msg))
|
|
Else
|
|
TextBox2.Text = TextBox2.Text +
|
|
Environment.NewLine + " >> " + readData
|
|
End If
|
|
End Sub
|
|
End Class
|