• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

How to communicate via TCP/IP Settings in vb 2005 Error SocketException was unhandled

I am trying to workout an issues I am having with the following code. Each time I get to the TCPListener I get an error saying that the "SokeyException was unhandled - The requset address is nt valid in its context". I am trying to communication to another PC that is in my network. I selected port 5679 because it said it was a listnening port when I did the following command C:\> netstat -ao |find /i "listening" However I do not get past the tcpListener.Start() call. When I have the programs on the same PC everything works fine.
Imports System.Net.Sockets
Imports System.net
Imports System.Text
Imports System.Net.DnsPermissionAttribute
Imports System.Security.Permissions


Public Class Form1
    Const portNumber As Integer = 5679
    Private tcpListener As New TcpListener(CType(Dns.Resolve("LMI111").AddressList(0), IPAddress), portNumber)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Listening must be on the same port that the client is connected on. 
        '"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.
        'Comment the previous line and uncomment the following line if you are using Visual Basic .NET (2003).
        'Dim tcpListener As New TcpListener(portNumber)
        tcpListener.Start()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Console.WriteLine("TCP Server is up and waiting for Client connection...")
        Me.Label1.Text = "TCP Server is up and waiting for Client connection..."

        ''Accept the pending client connection and return a TcpClient for communication. 
        Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
        ' Console.WriteLine("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.Label1.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)
        'Console.WriteLine(("Message Sent by TCP Server /> : " + responseString))
        'Close TcpListener and TcpClient.
        tcpClient.Close()
        tcpListener.Stop()
        ' Console.WriteLine("Exit")
        'Console.ReadLine()

    End Sub
End Class

Open in new window

0
cmdolcet
Asked:
cmdolcet
  • 4
  • 3
2 Solutions
 
CodeCruiserCommented:
As far as I know, you start the listener on the local server not on the remote server.
0
 
cmdolcetAuthor Commented:
Yes correct, however the code above is on one PC and then I another set of code is on another PC when I run two different instances of VS running a server to client applications I get no errors and everything runs correctly.
0
 
cmdolcetAuthor Commented:
It seems that its something to do with the Dns.Resolve("LMI111") which does exist on our network and is active.

the code:
 Private tcpListener As New TcpListener(CType(Dns.Resolve("LMI111").AddressList(0), IPAddress), portNumber)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Listening must be on the same port that the client is connected on. 
        '"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.
        'Comment the previous line and uncomment the following line if you are using Visual Basic .NET (2003).
        'Dim tcpListener As New TcpListener(portNumber)
        tcpListener.Start()
    End Sub

Open in new window


Always fails when I call the tcpListener.start The "AKA" server PC is on LMI135 and the client is on LMI111

so what PC name should I be calling in the........
Private tcpListener As New TcpListener(CType(Dns.Resolve("LMI111").AddressList(0), IPAddress), portNumber)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CodeCruiserCommented:
It should be the server name and this code should be running on that server.
0
 
cmdolcetAuthor Commented:
Wait what should be the server name? this code below should be the server name in the Dns.Resolve?

Private tcpListener As New TcpListener(CType(Dns.Resolve("LMI111").AddressList(0), IPAddress), portNumber)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Listening must be on the same port that the client is connected on. 
        '"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.
        'Comment the previous line and uncomment the following line if you are using Visual Basic .NET (2003).
        'Dim tcpListener As New TcpListener(portNumber)
        tcpListener.Start()
    End Sub

Open in new window

0
 
CodeCruiserCommented:
You are confusing Windows server with socket server. In terms of sockets, the server is the computer where TcpListener.Start code is running so if you want LMI135 to act as the socket server then use that name and run this code on that computer.
0
 
cmdolcetAuthor Commented:
OK, let me try this out.... Yes this could be the issue. I just wanted to be clear where the second computer came into play

Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now