熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

使用Socket發送和接收消息[3]

2013-11-13 11:07:59  來源: .NET編程 
    ——此文章摘自《ASPNET網絡數據庫開發實例精解》定價 特價 詳細>>http://tracklinktechcn/?m_id=dangdang&a_id=A&l=&l_type= width= height= border= nosave>

    fileSteamClose();
    //關閉發送數據的Socket
    sendsocketShutdown(SocketShutdownSend);
    sendsocketClose();
    }

    單擊頁面MySocketaspx中的【接收數據】按鈕觸發事件ReceiveBtn_Click (object sender SystemEventArgs e)該事件調用函數SendReceiveData()接收從函數SocketSend()發送的數據函數SendReceiveData()首先創建用來接收數據的Socket對象並設計接收數據的IP地址和端口最後把接收的數據寫入文件並保存函數SendReceiveData()和事件 ReceiveBtn_Click(object sender System EventArgs e)的程序代碼如下
    private void ReceiveBtn_Click(object sender SystemEventArgs e)
    {
    //接收數據
    SendReceiveData();
    }
    private void SendReceiveData()
    {
    //創建接收數據的Socket
    Socket receiveSocket = new Socket(AddressFamilyInterNetwork
    SocketTypeStreamProtocolTypeTcp);
    //設置接收數據的地址
    IPEndPoint hostPoint = new IPEndPoint(IPAddressParse());
    //設置端口
    receiveSocketBind(hostPoint);
    //監聽
    receiveSocketListen();
    //設置接收數據緩沖區的大小
    Byte[] recData = new Byte[];
    Socket hostSocket  = receiveSocketAccept();
    //接收數據
    hostSocketReceive(recData);
    if(hostSocketReceive(recData) > )
    {
    //顯示接收數據的信息
    LabelDataText = 網頁在 + DateTimeNowToString()
    + 接收的數據大小為 + hostSocketReceive(recData)ToString();
    }
    //創建一個新文件保存接收的數據

right>[http://developcsaicn/dotnet_ASP/htm>]  [http://developcsaicn/dotnet_ASP/htm>]  []  [http://developcsaicn/dotnet_ASP/htm>]  


From:http://tw.wingwit.com/Article/program/net/201311/15234.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.