Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about Sockets in .net

Status
Not open for further replies.

talon121a

MIS
Aug 21, 2003
92
US
Having problems with the following code..

Not able to send enters or other characters like that. ie.

Send string (slogin username password<enter>)

Its a telnet session, any ideas on how to send the Enter?

Perhaps programatically... ie. how to send via code.
--- socket.aspx ---
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Text" %>
<%@ import Namespace="System.Net.Sockets" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
dim strBuffer as new stringbuilder

Sub Page_Load(sender As Object, e As EventArgs)
dim strHost as string
dim nPort as integer

if page.ispostback then

strHost=request("strHost")
if isnumeric(request("nPort")) then nPort=request("nPort")

if len(strHost)<=0 or nPort<=0 or nPort>65535 then
response.write("Invalid Hostname Or Port")
exit sub
end if
strBuffer.remove(0,strBuffer.length)
Dim tcpClient As New System.Net.Sockets.TcpClient()

try
tcpClient.Connect(strHost, nPort)
Dim networkStream As NetworkStream = tcpClient.GetStream()


if len(request("strRequest"))>0 then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(request("strRequest"))
networkStream.write(sendBytes,0,sendBytes.length)
end if

Dim responseArr() as string
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

strBuffer.append(Encoding.ASCII.GetString(bytes))

tcpClient.Close()
catch ex as exception
response.write (ex.message.tostring)
response.end
end try
dim str as string
str = replace(strBuffer.tostring,"<","&lt;")
str = replace(str,">","&gt;")
response.write ("<hr style=""position=absolute; left=0;top=170;""><pre style=""position=absolute; left=0;top=180;"">" & str & "</pre>")
end if
End Sub


</script>

<html>
<head>
</head>
<body>
<form runat="server">
<p>
<table style=""position=absolute; left=0;top=0;"">
<tbody>
<tr>
<td>
Hostname: </td>
<td>
<asp:TextBox id="strHost" runat="server" Width="274px"></asp:TextBox>
</tr>
<tr>
<td>
Port: </td>
<td>
<asp:TextBox id="nPort" runat="server" Width="274px"></asp:TextBox>&nbsp;
<input type="submit" value="Submit" /></td>
</tr>
<tr>
<td>
Text To Send: </td>
<td>
<asp:TextBox id="strRequest" runat="server" Width="275px" TextMode="MultiLine" Columns="50" Rows="5"></asp:TextBox>
</tr>
</tbody>
</table>
</p>
<p>
<br/>
<asp:Label id="lab1" runat="server"></asp:Label>
<br />
<asp:Table id="Table1" runat="server" BorderColor="Gray" BorderWidth="1px" CellSpacing="0"
CellPadding="3" BorderStyle="Solid" GridLines="Both"></asp:Table>
</p>
</form>
</body>
</html>
--- end socket.aspx


--------
Jason Burton
Leximedia,LLC.
jab@leximedia.net
(im confident with my spam fighting solution)
 
I tried
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("help" & Environment.NewLine)

Still didnt work.. Any ideas?



--------
Jason Burton
Leximedia,LLC.
jab@leximedia.net
(im confident with my spam fighting solution)
 
Sounds like you need to verify what is being sent over the socket.

To verify that the socket is being opened at all, use TCPView from
To see the contents of what's being sent over the socket, use Ethereal from
Be aware that use of ethereal in a corporate environment will likely get you in trouble.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks, put straight you want me to use a sniffer. I'll sniff the socket locally to see whats being sent..

I manage the network (IT manager) so no problems there. :)

Thanks! I'll post my results.

Jason

--------
Jason Burton
Leximedia,LLC.
jab@leximedia.net
(im confident with my spam fighting solution)
 
Code:
No:                   1
Timestamp:            2:12:22:250
MAC source address:   00:E0:18:F1:42:EC
MAC dest address:     00:C0:9F:08:6C:5D
Frame type:           IP
Protocol:             TCP->AUDIO-ACTIVMAIL
Source IP address:    10.1.1.19
Dest IP address:      10.1.1.1
Source port:          1397
Destination port:     51234
SEQ:                  2051919830
ACK:                  0
Packet size:          62

Packet data:
0000: 00 C0 9F 08 6C 5D 00 E0 18 F1 42 EC 08 00 45 00 ....l]....B...E.
0010: 00 30 63 EC 40 00 80 06 80 95 0A 01 01 13 0A 01 .0c.@...........
0020: 01 32 05 75 C8 22 7A 4D CF D6 00 00 00 00 70 02 .2.u."zM......p.
0030: FF FF 55 1D 00 00 02 04 05 B4 01 01 04 02       ..U...........

=====================================================================
No:                   2
Timestamp:            2:12:22:250
MAC source address:   00:C0:9F:08:6C:5D
MAC dest address:     00:E0:18:F1:42:EC
Frame type:           IP
Protocol:             TCP->AUDIO-ACTIVMAIL
Source IP address:    10.1.1.1
Dest IP address:      10.1.1.19
Source port:          51234
Destination port:     1397
SEQ:                  464686134
ACK:                  2051919831
Packet size:          62

Packet data:
0000: 00 E0 18 F1 42 EC 00 C0 9F 08 6C 5D 08 00 45 00 ....B.....l]..E.
0010: 00 30 52 FD 00 00 80 06 00 00 0A 01 01 32 0A 01 .0R..........2..
0020: 01 13 C8 22 05 75 1B B2 8C 36 7A 4D CF D7 70 12 ...".u...6zM..p.
0030: 40 00 6D 23 00 00 02 04 05 B4 01 01 04 02       @.m#..........

=====================================================================
No:                   3
Timestamp:            2:12:22:250
MAC source address:   00:E0:18:F1:42:EC
MAC dest address:     00:C0:9F:08:6C:5D
Frame type:           IP
Protocol:             TCP->AUDIO-ACTIVMAIL
Source IP address:    10.1.1.19
Dest IP address:      10.1.1.1
Source port:          1397
Destination port:     51234
SEQ:                  2051919831
ACK:                  464686135
Packet size:          60

Packet data:
0000: 00 C0 9F 08 6C 5D 00 E0 18 F1 42 EC 08 00 45 00 ....l]....B...E.
0010: 00 28 63 ED 40 00 80 06 80 9C 0A 01 01 13 0A 01 .(c.@...........
0020: 01 32 05 75 C8 22 7A 4D CF D7 1B B2 8C 37 50 10 .2.u."zM.....7P.
0030: FF FF D9 E7 00 00 00 00 00 00 00 00             ............

=====================================================================
No:                   4
Timestamp:            2:12:22:250
MAC source address:   00:E0:18:F1:42:EC
MAC dest address:     00:C0:9F:08:6C:5D
Frame type:           IP
Protocol:             TCP->AUDIO-ACTIVMAIL
Source IP address:    10.1.1.19
Dest IP address:      10.1.1.1
Source port:          1397
Destination port:     51234
SEQ:                  2051919831
ACK:                  464686135
Packet size:          60

Packet data:
0000: 00 C0 9F 08 6C 5D 00 E0 18 F1 42 EC 08 00 45 00 ....l]....B...E.
0010: 00 2E 63 EE 40 00 80 06 80 95 0A 01 01 13 0A 01 ..c.@...........
0020: 01 32 05 75 C8 22 7A 4D CF D7 1B B2 8C 37 50 18 .2.u."zM.....7P.
0030: FF FF F7 F9 00 00 68 65 6C 70 0D 0A             ......help..

=====================================================================
No:                   5
Timestamp:            2:12:22:250
MAC source address:   00:C0:9F:08:6C:5D
MAC dest address:     00:E0:18:F1:42:EC
Frame type:           IP
Protocol:             TCP->AUDIO-ACTIVMAIL
Source IP address:    10.1.1.1
Dest IP address:      10.1.1.19
Source port:          51234
Destination port:     1397
SEQ:                  464686135
ACK:                  2051919837
Packet size:          60

Packet data:
0000: 00 E0 18 F1 42 EC 00 C0 9F 08 6C 5D 08 00 45 00 ....B.....l]..E.
0010: 00 2E 52 FE 40 00 80 06 00 00 0A 01 01 32 0A 01 ..R.@........2..
0020: 01 13 C8 22 05 75 1B B2 8C 37 7A 4D CF DD 50 18 ...".u...7zM..P.
0030: FF F9 16 67 00 00 5B 54 53 5D 0D 0A             ...g..[TS]..

=====================================================================
No:                   6
Timestamp:            2:12:22:250
MAC source address:   00:C0:9F:08:6C:5D
MAC dest address:     00:E0:18:F1:42:EC
Frame type:           IP
Protocol:             TCP->AUDIO-ACTIVMAIL
Source IP address:    10.1.1.1
Dest IP address:      10.1.1.19
Source port:          51234
Destination port:     1397
SEQ:                  464686141
ACK:                  2051919837
Packet size:          0

Packet data:
0000:                                                 

=====================================================================
No:                   7
Timestamp:            2:12:22:250
MAC source address:   00:E0:18:F1:42:EC
MAC dest address:     00:C0:9F:08:6C:5D
Frame type:           IP
Protocol:             TCP->AUDIO-ACTIVMAIL
Source IP address:    10.1.1.19
Dest IP address:      10.1.1.1
Source port:          1397
Destination port:     51234
SEQ:                  2051919837
ACK:                  464687601
Packet size:          60

Packet data:
0000: 00 C0 9F 08 6C 5D 00 E0 18 F1 42 EC 08 00 45 00 ....l]....B...E.
0010: 00 28 63 EF 40 00 80 06 80 9A 0A 01 01 13 0A 01 .(c.@...........
0020: 01 32 05 75 C8 22 7A 4D CF DD 1B B2 91 F1 50 10 .2.u."zM......P.
0030: FF FF D4 27 00 00 00 00 00 00 00 00             ...'........

=====================================================================
No:                   8
Timestamp:            2:12:22:375
MAC source address:   00:E0:18:F1:42:EC
MAC dest address:     00:C0:9F:08:6C:5D
Frame type:           IP
Protocol:             TCP->AUDIO-ACTIVMAIL
Source IP address:    10.1.1.19
Dest IP address:      10.1.1.1
Source port:          1397
Destination port:     51234
SEQ:                  2051919837
ACK:                  464688544
Packet size:          60

Packet data:
0000: 00 C0 9F 08 6C 5D 00 E0 18 F1 42 EC 08 00 45 00 ....l]....B...E.
0010: 00 28 63 F7 40 00 80 06 80 92 0A 01 01 13 0A 01 .(c.@...........
0020: 01 32 05 75 C8 22 7A 4D CF DD 1B B2 95 A0 50 10 .2.u."zM......P.
0030: FC 50 D4 27 00 00 00 00 00 00 00 00             .P.'........

=====================================================================

Youll notice packet #4, is where the command 'help' is being sent. --- perhaps Environment.NewLine doesnt work? is there another 'enter key' or other combo to try?? this is specifically the 'Teamspeak' query port service. (51234). (teamspeak from I can easily telnet to the port and send the command w/o problems, just not via sockets.

--------
Jason Burton
Leximedia,LLC.
jab@leximedia.net
(im confident with my spam fighting solution)
 
Also [TS] is the only line i seen that the socket received. Usually is followed by multiple lines.. ie. a help command.. ---- forgot to add.

--------
Jason Burton
Leximedia,LLC.
jab@leximedia.net
(im confident with my spam fighting solution)
 
If you look at packet #4, right after the 'help' is 0D 0A, which is carriage-return & linefeed. This is Environment.NewLine in the DOS/Windows environment. So it looks like it's being sent.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
yea, which im lost with.. Why isnt it returning the output correctly...

Perhaps its not a carriage return or perhaps its interpreting it wrong????

Its not returning the multiple lines. Ie. the help response.

But yet, I can very easily telnet from that same ip, and type help and get a full response.


--------
Jason Burton
Leximedia,LLC.
jab@leximedia.net
(im confident with my spam fighting solution)
 
I tried using ControlChars.CrLf As well.,

I tried monitoring the application a bit carefully, determining if what i type gets thru..

They do infact get thru. The commands are being sent appropriately (ie. the enters.)

The problem now seems to be..The response from the server to browser output..

I tried a few things, but cant seem to get the realtime response back.. (w/o any constraints). The code sample above does .replaces ..

Any ideas on how to get 'exact' output? (im unsure what its sending as linefeeds..)



--------
Jason Burton
Leximedia,LLC.
jab@leximedia.net
Donate toward Katrina Efforts! --
 
What it looks like to me is that it's being sent correctly, but possibly not being read correctly. I would spend some time adding Trace statements to the code that is reading from the socket to dump out the bytes that get received.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top