Problems sending data with System.Net.Sockets.Socket

Problems sending data with System.Net.Sockets.Socket

Post by Covit » Sat, 16 Jun 2007 11:22:42


Hi all

For the last few days, I've been practicing my PS scripting by trying
to write a simple IRC client. So far I've only come up with this:

-- Begin Script --

#Initialize ASCIIEncoding object and create a Socket
$ASCIIEncoder = New-Object System.Text.ASCIIEncoding
$Socket = New-Object System.Net.Sockets.Socket
([System.Net.Sockets.AddressFamily]::InterNetwork,
[System.Net.Sockets.SocketType]::Stream,
[System.Net.Sockets.ProtocolType]::Tcp)

#Connect to IRC server
$Socket.Connect("irc.freenode.net",6667)

#Send user data
$Socket.Send($ASCIIEncoder.GetBytes("NICK UserName"))
$Socket.Send($ASCIIEncoder.GetBytes("USER UserName 8 * : UserName"))

#Declare variables needed for receiving server messages
[System.Byte[]]$Buffer = 0..255
[Int32]$CharacterCount

#Continue to receive and display messages until disconnected
while($true)
{
$CharacterCount = $Socket.Receive($Buffer)

#Disconnected: Clean up and exit loop
if($CharacterCount -le 0)
{
$Socket.Dispose
break;
}
Write-Host $($ASCIIEncoder.GetString($Buffer[0..[Int32]$
($CharacterCount-1)])) -noNewLine
}

-- End Script --

The script appears to work fine.. all methods return values indicating
success (including $Socket.Send(...)). However, after a few seconds,
the connection to the IRC server times out (behaving *exactly* as it
does in windows' telnet.exe if no NICK/USER information is sent). So
I'm assuming that somehow, even though the calls to $Socket.Send()
indicate success, the data is not getting through to the server.

Is there something that I'm doing wrong here?

NOTE: I have eliminated the possibility of network issues; I connect
to this server daily, and I can successfully connect through telnet
with the same settings.

Thanks,
Coviti