After several hours of searching the web and the student MSDN archive in Visual Studio.net i cannot figure out through the random blabber of sample codes how to do basic connections using TCP socket connections through a client and a listener. I got the jist that winsock had become outdated to Microsoft and they attempted something new, but it all seems to complicated for microsoft to have done. If anyone can point me in the right direction on basic TCP connections, much thanks to ya.
-Tyreses

Dani AI

Generated

A few focused pointers to resolve the two issues in this thread (parsing a text IP/port into an IPEndPoint, and the "Variable 'e' hides a variable in an enclosing block" warning). , you were close — the typical problems are: (1) using Val or an empty IPAddress instance instead of properly parsing/validating input, and (2) reusing the same exception variable name inside nested Try/Catch blocks. was right that you need separate client and server processes; the examples below build on that and show safer parsing and error handling.

Validate and resolve the port and address before building an endpoint. Prefer Integer.TryParse (don’t rely on Val) and parse/resolve the IP string; fall back to DNS if the user entered a hostname. Example pattern:

Dim port As Integer
If Not Integer.TryParse(txtPort.Text, port) OrElse port < 1 OrElse port > 65535 Then
  Throw New ArgumentException("Invalid port")
End If

Dim ip As System.Net.IPAddress
Try
  ip = System.Net.IPAddress.Parse(txtIP.Text)   ' accepts numeric IPv4/IPv6
Catch ex As System.FormatException
  Dim addrs = System.Net.Dns.GetHostAddresses(txtIP.Text)
  If addrs.Length = 0 Then Throw New ArgumentException("Host lookup failed")
  ip = addrs(0)
End Try

Dim endpoint = New System.Net.IPEndPoint(ip, port)

The hiding warning comes from declaring the same exception variable name in an inner Catch when an outer Try/Catch already declares it. Solve it by using distinct names (or reuse a single outer Try/Catch): Catch ex As Exception (or innerEx) instead of e. That removes the shadowing and makes stack traces easier to read.

Extra troubleshooting tips: check firewall/antivirus and that the port isn’t already bound; use IPAddress.Any (server) or IPAddress.Loopback (local testing) as appropriate; always call listener.Start() before accepting; use Using or explicitly close sockets to avoid ephemeral port issues.

Ok i have figured out the basics but now im running into a different issue.

Dim address As new IPAddress()
Dim port As Integer = Val(txtport.text)
Dim EndPoint As New IPEndPoint(address, port)

when i try to enter an ipaddress such as 127.0.0.1 directly into the ipendpoint, it doesnt accept it, and putting an ipaddress into a long doesnt work either, im at a stall point here of how to get txtip.text into the endpoint...

Once again with a little time and effort, i figured out the issue, now i cant figure out this one. Using the code provided form the MSDN archive, i altered it ecept the lines with "e" in them.

Try
socket.Bind(endpoint)
Catch e As Exception
Console.WriteLine("Winsock error: " & e.ToString())
End Try

It says "Variable 'e' hides a variable in an enclosing block." I have no idea how to fix this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.