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
Solved

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

Posted on 2013-12-17
7
243 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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