Solved

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

Posted on 2013-12-17
7
242 Views
Last Modified: 2014-01-15
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
Comment
Question by:cmdolcet
  • 4
  • 3
7 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39724460
As far as I know, you start the listener on the local server not on the remote server.
0
 

Author Comment

by:cmdolcet
ID: 39724799
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
 

Author Comment

by:cmdolcet
ID: 39726615
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39733541
It should be the server name and this code should be running on that server.
0
 

Author Comment

by:cmdolcet
ID: 39741163
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
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 39754717
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
 

Author Comment

by:cmdolcet
ID: 39759143
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question