{"id":10011,"date":"2025-04-13T22:07:59","date_gmt":"2025-04-13T20:07:59","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=10011"},"modified":"2025-04-15T08:38:33","modified_gmt":"2025-04-15T06:38:33","slug":"powershell-simple-tcp-client-and-server","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/powershell-simple-tcp-client-and-server\/","title":{"rendered":"Powershell: Simple TCP Client and Server"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_10011 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_10011')){$('.twoclick_social_bookmarks_post_10011').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Powershell%3A%20Simple%20TCP%20Client%20and%20Server\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Athese%20are%20TCP%20client%20and%20server%20which%20can%20simply%20used%20for%20testing%20if%20a%20specific%20TCP%20port%20is%20open%0D%0A%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/powershell-simple-tcp-client-and-server\\\/\",\"post_id\":10011,\"post_title_referrer_track\":\"Powershell%3A+Simple+TCP+Client+and+Server\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>these are TCP client and server which can simply used for testing if a specific TCP port is open<\/p>\n<p><!--more--><\/p>\n<p>This is the server side. It listens at port 4444<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n# Server\r\n$iPort = 4444\r\n$TCPListener = New-Object System.Net.Sockets.TcpListener($iPort)\r\n$TCPListener.Start();\r\n&#x5B;byte&#x5B;]]$ReadBytesBuffer = New-Object byte&#x5B;] 65536\r\n# Here the code waits for an incoming connection\r\n$TCPServer = $TCPListener.AcceptTcpClient();\r\nwrite-host (&quot;Connected from {0}&quot; -f $TCPServer.Client.RemoteEndPoint.ToString())\r\n$TCPServerDataStream = $TCPServer.GetStream()\r\ndo\r\n{\r\n    try\r\n    {\r\n       $iBytesRead= $TCPServerDataStream.read($ReadBytesBuffer , 0, $ReadBytesBuffer.Length)\r\n       $TCPServerDataStream.Write(&#x5B;System.Text.Encoding]::Ascii.GetBytes('OK'),0,2)\r\n       write-host (&#x5B;text.encoding]::ASCII.GetString($ReadBytesBuffer ,0,$iBytesRead))\r\n    }\r\n    catch \r\n    {\r\n       write-host (&quot;Cannot read data from stream. Error: {0}&quot; -f $_.Exception.Message)\r\n    }\r\n}\r\nwhile($TCPServer.Connected)\r\nwrite-host &quot;Connection closed&quot;\r\n$TCPServerDataStream.Close()\r\n$TCPServerDataStream.Dispose()\r\n$TCPServer.Close()\r\n$TCPServer.Dispose()\r\n$TCPListener.Stop()\r\n<\/pre>\n<p>And the client side<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;Net.Sockets.TCPClient]$TCPClient=New-Object System.Net.IPEndpoint (&#x5B;ipaddress]::any,0)\r\n$TCPClient.Connect(&quot;localhost&quot;,4444) \r\n$TCPClientDataStream = $TCPClient.GetStream()\r\n<\/pre>\n<p>Then you can send data multiple times from the client over the connection<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$Data = &#x5B;System.Text.Encoding]::Ascii.GetBytes('Message to TCP Server')    \r\n$TCPClientDataStream.Write($Data,0,$Data.length)\r\n<\/pre>\n<p>Then close the connection  <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n$TCPClient.Close()\r\n<\/pre>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, these are TCP client and server which can simply used for testing if a specific TCP port is open<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[344],"tags":[180,133,640,2076,20],"class_list":["post-10011","post","type-post","status-publish","format-standard","hentry","category-powershell-scripting","tag-client","tag-powershell","tag-server","tag-tcp","tag-windows-2"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/10011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=10011"}],"version-history":[{"count":14,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/10011\/revisions"}],"predecessor-version":[{"id":10025,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/10011\/revisions\/10025"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=10011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=10011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=10011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}