[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-12-17
7
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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