Question Winsock, hamachi, 'The requested address is not valid in its context'

Dada1995

New member
Joined
Oct 24, 2012
Messages
3
Location
Rotterdam, The Netherlands
Programming Experience
3-5
Hello all,


This is my first post on this forum, so first short introduction, I'm Daniel, a 17 year old programmer with an obsession for aviation... Today i started writing an application to fly so-called "shared cockpit" in Microsoft Flight Simulator. This application should include both the possibilities to serve as server or as client, connect to the other program over hamachi (so, on a specified IP address), then start sending data to synchronize both cockpits and planes. I decided to start with the connection part, as this is where I have the least experience with. Here is what I wrote so far (examples on the internet generally use much more code so maybe this looks stupid to you, but please remember this is the first time i work with winsock =S):


VB.NET:
Option Explicit On


Imports FSUIPC
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Text


Public Class Form1


    Dim FSUIPCconnected As Boolean = False
    Private isConnectedFlag As Boolean
    Private Client As TcpClient
    Private Server As TcpListener
    Private mobjClient As TcpClient
    Private mode = 0 '0 is server, 1 is client


    Dim bytes(256) As Byte
    Dim data As String = Nothing


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        TextBox2.Text = "1544"
    End Sub


    Private Sub button1_click() Handles Button1.Click
        If isConnectedFlag = False Then
            Try
                Label4.Text = "Listening.."
                Server = New TcpListener(IPAddress.Any, Convert.ToInt32(TextBox2.Text))
                Server.Start()
                mobjClient = Server.AcceptTcpClient
                Label4.Text = "Connected"
                isConnectedFlag = True
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            If MsgBox("Are you sure you want to disconnect?", MsgBoxStyle.YesNo, "FS-Share") = MsgBoxResult.Yes Then
                Client.Close()
                Server.Stop()
            End If
        End If
    End Sub


    Private Sub button2_click() Handles Button2.Click
        If isConnectedFlag = False Then
            Try
                Dim EP As New IPEndPoint(IPAddress.Parse(TextBox1.Text), Convert.ToInt32(TextBox2.Text))
                Client = New TcpClient(EP)
                If Client.Connected = True Then
                    Label4.Text = "Connected"
                    isConnectedFlag = True
                Else
                    Label4.Text = "Not Connected"
                    isConnectedFlag = False
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            If MsgBox("Are you sure you want to disconnect?", MsgBoxStyle.YesNo, "FS-Share") = MsgBoxResult.Yes Then
                Client.Close()
                Server.Stop()
            End If
        End If
    End Sub


End Class


Compiling went good the first try immediately. Now using localhost (127.0.0.1) on the client all works fine, but when trying the hamachi IP (25.93.147.153) it throws an exception with the description: ''The requested address is not valid in its context'' (WSAEADDRNOTAVAIL). I already found out the exception was thrown when creating the winsock client, therefore it must be a winsock problem. Searching on google learned me that ''This normally results from an attempt to bind to an address that is not valid for the local machine.''. I have continued searching for the answer, tried some things myself, but nothing seems to work.... Anyone here who can help out please? I really don't know more what to do. =S


Thanks,
Daniel

N.B. I'm using vb.NET 2010 here
 
Do you mean this code, where EP is the remote ip 25.93.147.153:
Client = New TcpClient(EP)
help said:
TcpClient Constructor (IPEndPoint)
Initializes a new instance of the TcpClient class and binds it to the specified local endpoint.
 
I found the solution for this problem.. I will post the code correction for them who face a similar problem:
VB.NET:
Private Sub button2_click() Handles Button2.Click
        If isConnectedFlag = False Then
            Try[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]          Dim strHostName as String = System.Net.Dns.GetHostName()
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]          Dim strIPAddress as String = System.Net.[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Dns[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].GetHostEntry(strHostName).AddressList(0).ToString()[/SIZE][/FONT][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]          Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] EP [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]IPEndPoint[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]IPAddress[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Parse(strIPAddress), [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Convert[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].ToInt32(TextBox2.Text))[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]                         Client = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]TcpClient[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2](EP)[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]          Client.Connect([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]IPAddress[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Parse(TextBox1.Text), [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Convert[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].ToInt32(TextBox2.Text))[/SIZE][/FONT][/SIZE][/FONT]
                If Client.Connected = True Then
                    Label4.Text = "Connected"
                    isConnectedFlag = True
                Else
                    Label4.Text = "Not Connected"
                    isConnectedFlag = False
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            If MsgBox("Are you sure you want to disconnect?", MsgBoxStyle.YesNo, "FS-Share") = MsgBoxResult.Yes Then
                Client.Close()
                Server.Stop()
            End If
        End If
    End Sub
 
You don't normally need to explicitly bind the client socket to a local endpoint, if you don't specify one it will be assigned automatically. There is also another constructor that will allow you to create, bind local, and connect remote -all in one call.
 
Back
Top