hey, i was wondering if 1) anyone could tell me whats wrong with my code, i
did a little fixing to the msdn version fo this, or 2) how to make a simple
program that will connect and listen for connections async. if the user
pushes the selected buttons.
i just want a program that connects to another computer on the port and IP
address selected and if the other computer is listening, then connect to it
but this code doesnt work :
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Net.DnsPermissionAttribute
Imports System.Security.Permissions
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 GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
Friend WithEvents btnconnect As System.Windows.Forms.Button
Friend WithEvents txtconnectport As System.Windows.Forms.TextBox
Friend WithEvents txtconnectIP As System.Windows.Forms.TextBox
Friend WithEvents rtbconnect As System.Windows.Forms.RichTextBox
Friend WithEvents btnlisten As System.Windows.Forms.Button
Friend WithEvents rtblisten As System.Windows.Forms.RichTextBox
Friend WithEvents txtlistenport As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.txtconnectport = New System.Windows.Forms.TextBox
Me.txtconnectIP = New System.Windows.Forms.TextBox
Me.btnconnect = New System.Windows.Forms.Button
Me.GroupBox2 = New System.Windows.Forms.GroupBox
Me.btnlisten = New System.Windows.Forms.Button
Me.txtlistenport = New System.Windows.Forms.TextBox
Me.rtbconnect = New System.Windows.Forms.RichTextBox
Me.rtblisten = New System.Windows.Forms.RichTextBox
Me.GroupBox1.SuspendLayout()
Me.GroupBox2.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.txtconnectport)
Me.GroupBox1.Controls.Add(Me.txtconnectIP)
Me.GroupBox1.Controls.Add(Me.btnconnect)
Me.GroupBox1.Controls.Add(Me.rtbconnect)
Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(616, 176)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Connect"
'
'txtconnectport
'
Me.txtconnectport.Location = New System.Drawing.Point(128, 72)
Me.txtconnectport.MaxLength = 5
Me.txtconnectport.Name = "txtconnectport"
Me.txtconnectport.Size = New System.Drawing.Size(56, 20)
Me.txtconnectport.TabIndex = 1
Me.txtconnectport.Text = "Port"
'
'txtconnectIP
'
Me.txtconnectIP.Location = New System.Drawing.Point(128, 32)
Me.txtconnectIP.Name = "txtconnectIP"
Me.txtconnectIP.TabIndex = 2
Me.txtconnectIP.Text = "IP"
'
'btnconnect
'
Me.btnconnect.Location = New System.Drawing.Point(32, 56)
Me.btnconnect.Name = "btnconnect"
Me.btnconnect.TabIndex = 0
Me.btnconnect.Text = "Connect"
'
'GroupBox2
'
Me.GroupBox2.Controls.Add(Me.btnlisten)
Me.GroupBox2.Controls.Add(Me.txtlistenport)
Me.GroupBox2.Controls.Add(Me.rtblisten)
Me.GroupBox2.Location = New System.Drawing.Point(8, 192)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(616, 160)
Me.GroupBox2.TabIndex = 0
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "Listen"
'
'btnlisten
'
Me.btnlisten.Location = New System.Drawing.Point(56, 80)
Me.btnlisten.Name = "btnlisten"
Me.btnlisten.TabIndex = 4
Me.btnlisten.Text = "Listen"
'
'txtlistenport
'
Me.txtlistenport.Location = New System.Drawing.Point(168, 80)
Me.txtlistenport.MaxLength = 5
Me.txtlistenport.Name = "txtlistenport"
Me.txtlistenport.Size = New System.Drawing.Size(56, 20)
Me.txtlistenport.TabIndex = 3
Me.txtlistenport.Text = "Port"
'
'rtbconnect
'
Me.rtbconnect.Location = New System.Drawing.Point(296, 16)
Me.rtbconnect.Name = "rtbconnect"
Me.rtbconnect.Size = New System.Drawing.Size(312, 152)
Me.rtbconnect.TabIndex = 3
Me.rtbconnect.Text = ""
'
'rtblisten
'
Me.rtblisten.Location = New System.Drawing.Point(296, 16)
Me.rtblisten.Name = "rtblisten"
Me.rtblisten.Size = New System.Drawing.Size(312, 136)
Me.rtblisten.TabIndex = 4
Me.rtblisten.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(632, 358)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.GroupBox2)
Me.Name = "Form1"
Me.Text = "Form1"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox2.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Dim HomeIP As IPAddress
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
HomeIP = Dns.GetHostByName(Dns.GetHostName).AddressList.Get Value(0)
Me.Text = HomeIP.ToString
End Sub
Private Sub btnconnect_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnconnect.Click
Call Connect()
End Sub
Private Sub Connect()
Try
Dim tcpClient As New System.Net.Sockets.TcpClient
'"Localhost" string is used when the client and the listener are
on the same computer.
'If the listener is listening at a computer that is different
from the client, provide the host name of the computer
'where the listener is listening.
tcpClient.Connect(Me.txtconnectIP.Text, Me.txtconnectport.Text)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
' Do a simple write.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is
anybody listening...")
networkStream.Write(sendBytes, 0, sendBytes.Length)
' Read the NetworkStream into a byte buffer.
'****Dim bytes(tcpClient.ReceiveBufferSize) As Byte
'''****networkStream.Read(bytes, 0,
CInt(tcpClient.ReceiveBufferSize))
' Output the data received from the host to the console.
'****Dim returndata As String =
Encoding.ASCII.GetString(bytes)
'****Console.WriteLine(("TCP Server returned: " + returndata))
Else
If Not networkStream.CanRead Then
Me.rtbconnect.Text += "Could not write data to data
stream"
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
Me.rtbconnect.Text += "Could not read data from data
stream"
tcpClient.Close()
End If
End If
End If
' Pause to let the user view the console output.
'**** Console.ReadLine()
Catch ex As Exception
Me.rtbconnect.Text += ex.Message
End Try
End Sub
Private Sub btnlisten_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnlisten.Click
Call CTestTCPServer()
End Sub
<DnsPermissionAttribute(SecurityAction.Demand, Unrestricted:=True)>
Private Sub CTestTCPServer()
'Listening must be on the same port that the client is connected on.
Dim portNumber As Integer = Me.txtlistenport.Text
'"Localhost" string is used when the client and the listener are on
the same computer.
'If the listener is listening at a computer that is different from
the client, then provide the host name of the computer
'where the listener is listening.
'****Dim tcpListener As New
TcpListener(CType(Dns.Resolve("Localhost").Address List(0), IPAddress),
portNumber)
'Comment the previous line and uncomment the following line if you
are using Visual Basic .NET (2003).
Dim tcpListener As New TcpListener(HomeIP, portNumber)
tcpListener.Start()
Me.rtblisten.Text += "TCP Server is up and waiting for Client
connection..."
Try
''Accept the pending client connection and return a TcpClient
for communication.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Me.rtblisten.Text += "Connection accepted."
' Get the data stream.
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the data stream into a byte array.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
Me.rtblisten.Text += "Client sent: " + clientdata
Dim responseString As String = "Successfully connected to TCP
server."
Dim sendBytes As [Byte]() =
Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Me.rtblisten.Text += "Message Sent by TCP Server /> : " +
responseString
'Close TcpListener and TcpClient.
tcpClient.Close()
tcpListener.Stop()
Catch ex As Exception
Me.rtblisten.Text += ex.Message
End Try
End Sub
End Class