Site hosted by Angelfire.com: Build your free website today!

SERVER/CLIENT EXAMPLE

 

TCPClient (NOT THE CLASS) call name is TCPReadWrite.vb

Public Class TCPClient
Inherits System.Windows.Forms.Form
Dim SocketA As New TCPReadWrite() 'CALLING THE CLASS HERE....

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SocketA = New TCPReadWrite()

TxtIp.Text = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList(1).ToString
Socket.ConnectToTCPClient(TxtIp.Text, txtPort.Text) 'ERROR HERE


Socket.DisconnectTCPClient() 'AND HERE
End Sub

scorched.zip

CLIENT SEND DATA TO SERVER

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text

Public Class FrmClient

Inherits System.Windows.Forms.Form
Dim t As Thread
Dim x As Boolean = True
Dim nl As String = System.Environment.NewLine
Dim udpClient As New UdpClient()
Public Shared m_szHostName As String
Public Shared m_LocalHost As IPHostEntry


Private Sub ButSendData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButSendData.Click
' ***** SENDING Data from Client to Server *****'
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TxtLoginName.Text + ":" + TxtDataContent.Text)
Try
udpClient.Send(sendBytes, sendBytes.Length, TxtIP.Text, (TxtPort.Text))
TxtData.AppendText("Message Sent to IP " + TxtIP.Text + " To Port: " + TxtPort.Text + nl)
Catch ex As Exception
MsgBox(ex.ToString())
End Try

End Sub
Private Sub StartListening()
x = True

'Receive Packs from Any IP only from Port TxtPort.text
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, TxtPort.Text)
While x = True
Try
' Blocks until a message returns on this socket from a remote host.
Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
TxtData.AppendText(returnData.ToString())
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End While


End Sub
Protected Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Dispose

Private Sub FrmClient_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'ABNORMAL Closing

x = False

Try
udpClient.Close()
t.Abort()
t.Join()
Application.Exit()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Private Sub FrmClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try
TxtIP.Text = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList(1).ToString
Catch Ex As IndexOutOfRangeException
MsgBox("Index was out of range, recreating defualt Index for IP address", MsgBoxStyle.Information, "Recreating Index")
TxtIP.Text = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName).AddressList(0).ToString
End Try
'Loading Lister Thread to handle incoming/sending UDP messages
t = New Thread(AddressOf StartListening)
TxtData.Text = "Listening Now!" + nl
t.Start()
End Sub
End Class

 

SERVER RECEIVE DATA FROM CLIENT

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text

Public Class FrmServer
Inherits System.Windows.Forms.Form

**HERE IS WINDOWS DISGNER GENERATED CODE**

Private Sub StartListening()

''''TxtPort.Text=11000

Dim x As Boolean = True

receivingUdpClient = New UdpClient(TxtPort.Text)

Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Parse("212.190.182.240"), CType(TxtPort.Text, Integer))
While x = True
Try

' Blocks until a message returns on this socket from a remote host.
Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint)

lblUdpCount.Text = lblUdpCount.Text + 1
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)

TxtInfo.AppendText("**" + returnData.ToString() + "**" + nl)
' TxtInfo.AppendText("Sent From " + RemoteIpEndPoint.Address.ToString() + " Their Port: " + RemoteIpEndPoint.Port.ToString() + nl)
TxtInfo.AppendText("Sent From " + RemoteIpEndPoint.Address.ToString() + " Their Port: " + TxtPort.Text + nl)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End While
receivingUdpClient.Close()

End Sub

End class


ok