Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - problem with very simple tcpclient in powershell

Reply
 
Old 02-26-2009   #1 (permalink)
Invictus


 
 

problem with very simple tcpclient in powershell

Hi there,

I created a server in C# using TcpListner which is working fine. To connect
to that server, i created a powershell script as follows:

$s = new-object System.Net.Sockets.TcpClient("127.0.0.1", 4099)
$str = $s.GetStream
$wrtr = new-object System.IO.StreamWriter($str)
$wrtr.WriteLine("Hello world!");
$wrtr.Flush
$wrtr.Close();
$s.Close()

Now, this is a very simple script and the SAME EQUIVALENT when written in C#
works fine.

But when i run this from the powershell, i get no errors, the Server
definitely detects the connection, but it NEVER receives anything sent from
WriteLine(). The same code written in C# works fine... even WriteLine()
works fine in C# but not powershell.

I do not get any error message either. What might be going wrong?

Any help is appreciated.

Thank you
Invictus


My System SpecsSystem Spec
Old 02-28-2009   #2 (permalink)
Josh Einstein


 
 

Re: problem with very simple tcpclient in powershell

Not sure why you aren't getting an error but you need parenthases after
GetStream otherwise the return value will be MethodInfo's.

Josh

"Invictus" <subrat@xxxxxx> wrote in message
news:BFC0F279-0630-441B-B412-2F2D3843176B@xxxxxx
Quote:

> Hi there,
>
> I created a server in C# using TcpListner which is working fine. To
> connect to that server, i created a powershell script as follows:
>
> $s = new-object System.Net.Sockets.TcpClient("127.0.0.1", 4099)
> $str = $s.GetStream
> $wrtr = new-object System.IO.StreamWriter($str)
> $wrtr.WriteLine("Hello world!");
> $wrtr.Flush
> $wrtr.Close();
> $s.Close()
>
> Now, this is a very simple script and the SAME EQUIVALENT when written in
> C# works fine.
>
> But when i run this from the powershell, i get no errors, the Server
> definitely detects the connection, but it NEVER receives anything sent
> from WriteLine(). The same code written in C# works fine... even
> WriteLine() works fine in C# but not powershell.
>
> I do not get any error message either. What might be going wrong?
>
> Any help is appreciated.
>
> Thank you
> Invictus
My System SpecsSystem Spec
Old 03-01-2009   #3 (permalink)
Marco Shaw [MVP]


 
 

Re: problem with very simple tcpclient in powershell

Invictus wrote:
Quote:

> Hi there,
>
> I created a server in C# using TcpListner which is working fine. To
> connect to that server, i created a powershell script as follows:
>
> $s = new-object System.Net.Sockets.TcpClient("127.0.0.1", 4099)
> $str = $s.GetStream
> $wrtr = new-object System.IO.StreamWriter($str)
> $wrtr.WriteLine("Hello world!");
> $wrtr.Flush
> $wrtr.Close();
> $s.Close()
>
> Now, this is a very simple script and the SAME EQUIVALENT when written
> in C# works fine.
I played with this a bit, and couldn't get it to work either. For one
thing, you don't have your Flush method specified properly, it should be:
$wrtr.Flush()

(Semi-colons aren't required in PowerShell unless you put things on the
same line.)

Marco
My System SpecsSystem Spec
Old 03-01-2009   #4 (permalink)
Marco Shaw [MVP]


 
 

Re: problem with very simple tcpclient in powershell

Josh Einstein wrote:
Quote:

> Not sure why you aren't getting an error but you need parenthases after
> GetStream otherwise the return value will be MethodInfo's.
There you go. I missed that one, so if you do this, it does work:

$s = new-object System.Net.Sockets.TcpClient("127.0.0.1", 8001)
$str = $s.GetStream()
$wrtr = new-object System.IO.StreamWriter($str)
$wrtr.Write("Test");
$wrtr.Flush();
$wrtr.Close();
$s.Close()

You had *2* spots where you were missing the "()" on a method.

Marco
My System SpecsSystem Spec
Old 03-01-2009   #5 (permalink)
Marco Shaw [MVP]


 
 

Re: problem with very simple tcpclient in powershell

Marco Shaw [MVP] wrote:
Quote:

> Josh Einstein wrote:
Quote:

>> Not sure why you aren't getting an error but you need parenthases
>> after GetStream otherwise the return value will be MethodInfo's.
>
> There you go. I missed that one, so if you do this, it does work:
>
> $s = new-object System.Net.Sockets.TcpClient("127.0.0.1", 8001)
> $str = $s.GetStream()
> $wrtr = new-object System.IO.StreamWriter($str)
> $wrtr.Write("Test");
> $wrtr.Flush();
> $wrtr.Close();
> $s.Close()
>
> You had *2* spots where you were missing the "()" on a method.
>
> Marco
I switched to using Write() in my testing, but WriteLine() likely works
equally well now.

Marco
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
simple problem Network & Sharing
Simple Intellitype pro problem? Drivers
Help with simple keyboard problem General Discussion
TCPClient Thread .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46