0% found this document useful (0 votes)
21 views

TCP Seerver

Tcp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

TCP Seerver

Tcp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SERVER

........................................................................................................................................................................
Imports System.Net
Imports System.Net.Sockets
Imports System.Numerics
Imports System.Text
Imports Microsoft.VisualBasic.Logging
Imports Microsoft.Win32

Public Class SERVER

Dim _server As TcpListener


Dim _listOfClients As New List(Of TcpClient)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Try

Dim ip As String =
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString
Dim port As Integer = TextBox2.Text

_server = New TcpListener(IPAddress.Parse(ip), port)


_server.Start()
Button1.BackColor = Color.Green

Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub NewClient(state As Object)
Dim client As TcpClient = _server.AcceptTcpClient()
Try
_listOfClients.Add(client)
Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)

While True
Dim ns As NetworkStream = client.GetStream()

Dim toReceive(100000) As Byte


ns.Read(toReceive, 0, toReceive.Length)
Dim txt As String = Encoding.ASCII.GetString(toReceive)

For Each c As TcpClient In _listOfClients


If c IsNot client Then
Dim nns As NetworkStream = c.GetStream()
nns.Write(Encoding.ASCII.GetBytes(txt), 0, txt.Length)
End If
Next
End While

Catch ex As Exception
If _listOfClients.Contains(client) Then
_listOfClients.Remove(client)
End If
MsgBox(ex.Message)
End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


TextBox1.Text =
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString
RX.Show()
TX.Show()
TX2.Show()
TX4.Show()
TX5.Show()

End Sub
End Class
........................................................................................................................................................................

TX

........................................................................................................................................................................
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar

Public Class TX
Dim _client As TcpClient
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim ip As String = "172.20.10.8"
Dim port As Integer = "8080"

_client = New TcpClient(ip, port)

CheckForIllegalCrossThreadCalls = False
Timer1.Start()
Button1.BackColor = Color.Green
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick


Try
Dim ns As NetworkStream = _client.GetStream()
Dim DrivePath As String = "Z:\"
Dim I As String = 1
Dim J As String = 11
If Directory.Exists(DrivePath) Then

ns.Write(Encoding.ASCII.GetBytes(I), 0, I.Length)
ElseIf Not Directory.Exists(DrivePath) Then
ns.Write(Encoding.ASCII.GetBytes(J), 0, J.Length)
Else
ns.Write(Encoding.ASCII.GetBytes(""), 0, "".Length)
End If

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
........................................................................................................................................................................

RX
........................................................................................................................................................................
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar

Public Class RX
Dim _client As TcpClient
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
Dim ip As String = "172.20.10.8"
Dim port As Integer = "8080"
_client = New TcpClient(ip, port)

CheckForIllegalCrossThreadCalls = False

Threading.ThreadPool.QueueUserWorkItem(AddressOf ReceiveMessages)
Button3.BackColor = Color.Green
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub ReceiveMessages(state As Object)

Try

While True

Dim ns As NetworkStream = _client.GetStream()

Dim toReceive(100000) As Byte


ns.Read(toReceive, 0, toReceive.Length)
Dim txt As String = Encoding.ASCII.GetString(toReceive)
If txt = 1 Then
Button1.BackColor = Color.Green
ElseIf txt = 11 Then
Button1.BackColor = Color.Red
End If
If txt = 2 Then
Button2.BackColor = Color.Green
ElseIf txt = 22 Then
Button2.BackColor = Color.Red
End If
If txt = 3 Then
Button4.BackColor = Color.Green
ElseIf txt = 33 Then
Button4.BackColor = Color.Red
End If
If txt = 4 Then
Button5.BackColor = Color.Green
ElseIf txt = 44 Then
Button5.BackColor = Color.Red
End If
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

You might also like