Solved

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

Posted on 2013-12-17
7
237 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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 …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now