whups, n00b error there, thanks for pointing that out, but now i havbe a
problem. il show the code but one computer says its connected while the other
does not. the computer listening throws an exception saying it cant recieve
data when its not connected while the other computer says its
connected....heres the code, thanks for all your help
Imports System.Net.Sockets
Imports System.Net
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides 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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnconnect As System.Windows.Forms.Button
Friend WithEvents txtport As System.Windows.Forms.TextBox
Friend WithEvents txtIP As System.Windows.Forms.TextBox
Friend WithEvents btnlisten As System.Windows.Forms.Button
Friend WithEvents txtlistenport As System.Windows.Forms.TextBox
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
Friend WithEvents rtbinfo As System.Windows.Forms.RichTextBox
Friend WithEvents rtbsend As System.Windows.Forms.RichTextBox
Friend WithEvents btnsend As System.Windows.Forms.Button
Friend WithEvents txtsend As System.Windows.Forms.TextBox
Friend WithEvents btndisconnect As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.btnconnect = New System.Windows.Forms.Button
Me.txtport = New System.Windows.Forms.TextBox
Me.txtIP = New System.Windows.Forms.TextBox
Me.btnlisten = New System.Windows.Forms.Button
Me.txtlistenport = New System.Windows.Forms.TextBox
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.btndisconnect = New System.Windows.Forms.Button
Me.GroupBox2 = New System.Windows.Forms.GroupBox
Me.rtbinfo = New System.Windows.Forms.RichTextBox
Me.rtbsend = New System.Windows.Forms.RichTextBox
Me.btnsend = New System.Windows.Forms.Button
Me.txtsend = New System.Windows.Forms.TextBox
Me.GroupBox1.SuspendLayout()
Me.GroupBox2.SuspendLayout()
Me.SuspendLayout()
'
'btnconnect
'
Me.btnconnect.Location = New System.Drawing.Point(16, 56)
Me.btnconnect.Name = "btnconnect"
Me.btnconnect.TabIndex = 0
Me.btnconnect.Text = "Connect"
'
'txtport
'
Me.txtport.Location = New System.Drawing.Point(112, 56)
Me.txtport.Name = "txtport"
Me.txtport.Size = New System.Drawing.Size(56, 20)
Me.txtport.TabIndex = 1
Me.txtport.Text = "5190"
'
'txtIP
'
Me.txtIP.Location = New System.Drawing.Point(32, 24)
Me.txtIP.Name = "txtIP"
Me.txtIP.TabIndex = 2
Me.txtIP.Text = "IP"
'
'btnlisten
'
Me.btnlisten.Location = New System.Drawing.Point(24, 48)
Me.btnlisten.Name = "btnlisten"
Me.btnlisten.TabIndex = 3
Me.btnlisten.Text = "Listen"
'
'txtlistenport
'
Me.txtlistenport.Location = New System.Drawing.Point(120, 48)
Me.txtlistenport.Name = "txtlistenport"
Me.txtlistenport.Size = New System.Drawing.Size(56, 20)
Me.txtlistenport.TabIndex = 4
Me.txtlistenport.Text = "5190"
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.txtport)
Me.GroupBox1.Controls.Add(Me.txtIP)
Me.GroupBox1.Controls.Add(Me.btnconnect)
Me.GroupBox1.Controls.Add(Me.btndisconnect)
Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(208, 88)
Me.GroupBox1.TabIndex = 5
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Connect"
'
'btndisconnect
'
Me.btndisconnect.Location = New System.Drawing.Point(16, 56)
Me.btndisconnect.Name = "btndisconnect"
Me.btndisconnect.TabIndex = 9
Me.btndisconnect.Text = "Disconnect"
Me.btndisconnect.Visible = False
'
'GroupBox2
'
Me.GroupBox2.Controls.Add(Me.btnlisten)
Me.GroupBox2.Controls.Add(Me.txtlistenport)
Me.GroupBox2.Location = New System.Drawing.Point(8, 104)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(208, 88)
Me.GroupBox2.TabIndex = 0
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "Listen"
'
'rtbinfo
'
Me.rtbinfo.Location = New System.Drawing.Point(16, 200)
Me.rtbinfo.Name = "rtbinfo"
Me.rtbinfo.ReadOnly = True
Me.rtbinfo.Size = New System.Drawing.Size(208, 152)
Me.rtbinfo.TabIndex = 6
Me.rtbinfo.Text = ""
'
'rtbsend
'
Me.rtbsend.Location = New System.Drawing.Point(256, 184)
Me.rtbsend.Name = "rtbsend"
Me.rtbsend.Size = New System.Drawing.Size(296, 168)
Me.rtbsend.TabIndex = 7
Me.rtbsend.Text = ""
'
'btnsend
'
Me.btnsend.Location = New System.Drawing.Point(320, 40)
Me.btnsend.Name = "btnsend"
Me.btnsend.TabIndex = 3
Me.btnsend.Text = "Send"
'
'txtsend
'
Me.txtsend.Location = New System.Drawing.Point(320, 72)
Me.txtsend.Name = "txtsend"
Me.txtsend.TabIndex = 8
Me.txtsend.Text = "TextBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(560, 357)
Me.Controls.Add(Me.txtsend)
Me.Controls.Add(Me.rtbsend)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.GroupBox2)
Me.Controls.Add(Me.rtbinfo)
Me.Controls.Add(Me.btnsend)
Me.Name = "Form1"
Me.Text = "Form1"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox2.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
#Region "Declarations"
Dim homeip As Net.IPAddress
Dim ClientSocket As Net.Sockets.Socket
Dim ASCII As New System.Text.ASCIIEncoding
Dim ListenSocket As Net.Sockets.Socket
#End Region
#Region "Connect"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
homeip =
Net.Dns.GetHostByName(Net.Dns.GetHostName).AddressList.GetValue(0)
Me.Text = homeip.ToString
End Sub
Private Sub btnconnect_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnconnect.Click
Dim addr As Net.IPAddress =
Net.Dns.Resolve(Me.txtIP.Text).AddressList(0)
If Not addr Is Nothing Then
'Create IP endpoint
Dim EP As New Net.IPEndPoint(addr, CInt(Me.txtport.Text))
'create socket
ClientSocket = New
Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork,
Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
'Connect
ClientSocket.BeginConnect(EP, AddressOf ConnectCallBack, Nothing)
' ClientSocket.Send(
End If
End Sub
Private Sub ConnectCallBack(ByVal ar As IAsyncResult)
Try
ClientSocket.EndConnect(ar)
Dim dlg As New NoParamsDelegate(AddressOf ConnectedUI)
Me.Invoke(dlg)
'Begin Receiveing Data
Dim bytes(4095) As Byte
ClientSocket.BeginReceive(bytes, 0, bytes.Length,
SocketFlags.None, AddressOf ReceiveCallBack, bytes)
Catch ex As Exception
Me.rtbinfo.Text += vbNewLine & ex.Message & vbNewLine
End Try
End Sub
Private Delegate Sub NoParamsDelegate()
Private Sub ConnectedUI()
Me.rtbinfo.Text += vbNewLine & "Connected" & vbNewLine
Me.btnsend.Enabled = True
Me.rtbsend.Focus()
End Sub
Private Sub DisconnectedUI()
Me.rtbinfo.Text += vbNewLine & "Disconnected" & vbNewLine
Me.btnconnect.Visible = True
Me.btndisconnect.Visible = False
Me.btnsend.Enabled = False
Me.btnconnect.Focus()
End Sub
Private Sub ReceiveCallBack(ByVal ar As IAsyncResult)
Dim bytes() As Byte = CType(ar.AsyncState, Byte())
Dim numbytes As Int32 = ClientSocket.EndReceive(ar)
If numbytes = 0 Then
ClientSocket.Shutdown(SocketShutdown.Both)
ClientSocket.Close()
Dim dlg As New NoParamsDelegate(AddressOf DisconnectedUI)
Me.Invoke(dlg)
Else
Dim recv As String = ASCII.GetString(bytes, 0, numbytes)
'Clear the buffer
Array.Clear(bytes, 0, bytes.Length)
'Show in UI
Dim dlg As New OneStringDelegate(AddressOf DisplayReceivedData)
Dim args() As Object = {recv}
Me.Invoke(dlg, args)
'Begin Receive Again
ClientSocket.BeginReceive(bytes, 0, bytes.Length,
SocketFlags.None, AddressOf ReceiveCallBack, bytes)
End If
End Sub
Private Delegate Sub OneStringDelegate(ByVal data As String)
Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnsend.Click
Try
If sender Is ClientSocket Then
Dim bytes() As Byte = ASCII.GetBytes(Me.txtsend.Text)
ClientSocket.Send(bytes)
ElseIf sender Is ListenSocket Then
Dim bytes() As Byte = ASCII.GetBytes(Me.txtsend.Text)
ListenSocket.Send(bytes)
End If
Catch ex As Exception
Me.rtbinfo.Text += vbNewLine & ex.Message & vbNewLine
End Try
End Sub
#End Region
#Region "Listen"
Private Sub btnlisten_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnlisten.Click
Me.btndisconnect.Visible = True
Me.btnconnect.Visible = False
Call Listen(homeip, Me.txtlistenport.Text)
End Sub
Private Sub Listen(ByVal IP As IPAddress, ByVal port As Integer)
Dim ep As New Net.IPEndPoint(IP, port)
ListenSocket = New Net.Sockets.Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
ListenSocket.Bind(ep)
ListenSocket.Listen(50)
ListenSocket.BeginAccept(AddressOf ListenCallBack, Nothing)
End Sub
Private Sub ListenCallBack(ByVal ar As IAsyncResult)
Try
ListenSocket.EndAccept(ar)
'Begin Receiveing Data
Dim bytes(4095) As Byte
ListenSocket.BeginReceive(bytes, 0, bytes.Length,
SocketFlags.None, AddressOf ListenReceiveCallBack, bytes)
Catch ex As Exception
Me.rtbinfo.Text += vbNewLine & ex.Message & vbNewLine
End Try
End Sub
Private Sub ListenReceiveCallBack(ByVal ar As IAsyncResult)
Dim bytes() As Byte = CType(ar.AsyncState, Byte())
Dim numbytes As Int32 = ListenSocket.EndReceive(ar)
If numbytes = 0 Then
ListenSocket.Shutdown(SocketShutdown.Both)
ListenSocket.Close()
Else
Dim recv As String = ASCII.GetString(bytes, 0, numbytes)
'Clear the buffer
Array.Clear(bytes, 0, bytes.Length)
'Begin Receive Again
ListenSocket.BeginReceive(bytes, 0, bytes.Length,
SocketFlags.None, AddressOf ListenReceiveCallBack, bytes)
End If
End Sub
#End Region
Private Sub DisplayReceivedData(ByVal data As String)
With Me.rtbsend
.SelectionStart = .Text.Length
.SelectedText = data
End With
End Sub
End Class