ASP.NET Tutorial/File Directory/NetworkStream

Материал из .Net Framework эксперт
Версия от 11:57, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Reading from a NetworkStream (C#)

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Sockets" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        TcpClient client = new TcpClient();
        IPAddress addr = IPAddress.Parse("65.122.214.52");
        IPEndPoint endpoint = new IPEndPoint(addr, 23);
        client.Connect(endpoint);
        NetworkStream ns = client.GetStream();
        if (ns.DataAvailable)
        {
            byte[] bytes = new byte[client.ReceiveBufferSize];
            ns.Read(bytes, 0, client.ReceiveBufferSize);
            string data = System.Text.Encoding.ASCII.GetString(bytes);
        }
        ns.Close();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>


Reading from a NetworkStream (VB)

<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Sockets" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim client As New TcpClient()
        Dim addr As IPAddress = IPAddress.Parse("65.122.214.52")
        Dim endpoint As New IPEndPoint(addr, 23)
        client.Connect(endpoint)
        Dim ns As NetworkStream = client.GetStream()
        If (ns.DataAvailable) Then
            Dim data(client.ReceiveBufferSize) As Byte
            ns.Read(data, 0, client.ReceiveBufferSize)
            Dim response As String = System.Text.Encoding.ASCII.GetString(data)
        End If
        ns.Close()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>