7
\$\begingroup\$

I need this down to as few characters as possible. I would use Test-NetConnection but I need PSv2 compatability. Any help would be appreciated.

$socket = new-object Net.Sockets.TcpClient
$socket.Connect("192.168.5.5",445)
$socket.Connected

As a note I have tried this but it doesn't work

(new-object Net.Sockets.TcpClient).Connect("192.168.5.5",445).Connected
\$\endgroup\$
7
  • 1
    \$\begingroup\$ My code does work. I am proposing the challenge of "make it as few characters as possible". I fail to see how this is considered "off topic" when one of the topics is "programming challenges". Please explain.... \$\endgroup\$ Commented Apr 3, 2017 at 17:20
  • 5
    \$\begingroup\$ @apsillers This is a perfectly on-topic question; we've accepted many "how can I make my code shorter" questions in the past: 1, 2, 3, 4, 5, 6, 7, 8, etc. \$\endgroup\$ Commented Apr 3, 2017 at 17:27
  • \$\begingroup\$ This would be better suited for The Nineteenth Byte or something. IMO, it doesn't warrant its own question on the main site. \$\endgroup\$ Commented Apr 3, 2017 at 17:28
  • 5
    \$\begingroup\$ @NickClifford This type of question, while uncommon, is absolutely on topic. \$\endgroup\$ Commented Apr 3, 2017 at 17:30
  • 1
    \$\begingroup\$ OP, just to explain the contention here, the site accepts two types of posts: "challenges" to achieve some general goal ("Given a number as input, compute the lowest prime number higher than the input number" as one simple example) and "tips" which ask for help golfing in a particular language. "Tips" posts are comparatively rare, and are largely language-specific compendiums of knowledge (e.g., Tips for golfing in Windows PowerShell. However, tips-posts asking to golf a particular problem are allowed, but fairly uncommon, hence the confusion. \$\endgroup\$
    – apsillers
    Commented Apr 3, 2017 at 17:48

2 Answers 2

6
\$\begingroup\$

PowerShell v2+, 63 bytes

You can use a different constructor to create the object and connect in one go.

(new-object Net.Sockets.TcpClient('192.168.5.5',445)).Connected

I've verified this works in v2 on my Windows 8.1 machine. That constructor is supported by .NET 2.0, so this should be v2 compatible.

\$\endgroup\$
4
  • \$\begingroup\$ Can replace '192.168.5.5' with 3232236805 \$\endgroup\$
    – ceilingcat
    Commented Apr 4, 2017 at 6:36
  • \$\begingroup\$ @ceilingcat Can you explain that? \$\endgroup\$ Commented May 13, 2017 at 3:22
  • \$\begingroup\$ @MichaelTimmerman The integer 3232236805 is equal to 192*256^3 + 168*256^2 + 5*256 + 5, the native format that the TCP stack uses internally. \$\endgroup\$
    – ceilingcat
    Commented May 17, 2017 at 1:11
  • \$\begingroup\$ @ceilingcat Just tested this by pinging 192.168.1.1. I would have never in a million years guessed that this would work lol. I'll be sure to remember this. Thank you for explaining :) \$\endgroup\$ Commented May 20, 2017 at 14:56
2
\$\begingroup\$

AFAIK, there's no default alias for New-Object, and no type accelerator for Net.Sockets.TcpClient, so they can't be any shorter. You can merge the constructor and connect code into one line:

(New-Object Net.Sockets.TcpClient -A 192.168.5.5,445)

But if it can't connect, it now throws an exception, which you can't silence with -ErrorAction. So handling that ends up being 70 characters, only 8 shorter than your original after shrinking the variable name:

!!$(try{(new-object net.sockets.tcpclient -A 192.168.5.5,445)}catch{})

!! forcing the result to a bool.

\$\endgroup\$
1
  • \$\begingroup\$ My final solution ended up taking parts from both of your answers: While(!!(New-Object net.sockets.tcpclient (192.168.5.5,445))){} Thank you both for your input! I gave the answer to TesselatingHeckler's because I was impressed at the way he returned a bool value in only two characters. Quite impressive! \$\endgroup\$ Commented Apr 4, 2017 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.