国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

使用.NET訪問Internet(4) Paul_Ni(原作)(補充)

2024-07-10 13:08:12
字體:
來源:轉載
供稿:網友
中國最大的web開發資源網站及技術社區,
開始從客戶端套接字接收數據的 acceptcallback 方法的此節首先初始化 stateobject 類的一個實例,然后調用 beginreceive 方法以開始從客戶端套接字異步讀取數據。

下面的示例顯示了完整的 acceptcallback 方法。它假定以下內容:存在一個名為 alldone 的 manualresetevent 實例,定義了 stateobject 類,以及在名為 socketlistener 的類中定義了 readcallback 方法。

[c#]
  public static void acceptcallback(iasyncresult ar) {
    // get the socket that handles the client request.
    socket listener = (socket) ar.asyncstate;
    socket handler = listener.endaccept(ar);

    // signal the main thread to continue.
    alldone.set();

    // create the state object.
    stateobject state = new stateobject();
    state.worksocket = handler;
    handler.beginreceive( state.buffer, 0, stateobject.buffersize, 0,
      new asynccallback(asynchronoussocketlistener.readcallback), state);
  }
需要為異步套接字服務器實現的 final 方法是返回客戶端發送的數據的讀取回調方法。與接受回調方法一樣,讀取回調方法也是一個 asynccallback 委托。該方法將來自客戶端套接字的一個或多個字節讀入數據緩沖區,然后再次調用 beginreceive 方法,直到客戶端發送的數據完成為止。從客戶端讀取整個消息后,在控制臺上顯示字符串,并關閉處理與客戶端的連接的服務器套接字。

下面的示例實現 readcallback 方法。它假定定義了 stateobject 類。

[c#]
public void readcallback(iasyncresult ar) {
  stateobject state = (stateobject) ar.asyncstate;
  socket handler = state.worksocket;

  // read data from the client socket.
  int read = handler.endreceive(ar);

  // data was read from the client socket.
  if (read > 0) {
    state.sb.append(encoding.ascii.getstring(state.buffer,0,read));
    handler.beginreceive(state.buffer,0,stateobject.buffersize, 0,
      new asynccallback(readcallback), state);
  } else {
    if (state.sb.length > 1) {
      // all the data has been read from the client;
      // display it on the console.
      string content = state.sb.tostring();
      console.writeline("read {0} bytes from socket./n data : {1}",
        content.length, content);
    }
    handler.close();
  }
}
同步客戶端套接字示例
下面的示例程序創建一個連接到服務器的客戶端。該客戶端是用同步套接字生成的,因此掛起客戶端應用程序的執行,直到服務器返回響應為止。該應用程序將字符串發送到服務器,然后在控制臺顯示該服務器返回的字符串。

[c#]
using system;
using system.net;
using system.net.sockets;
using system.text;

public class synchronoussocketclient {

  public static void startclient() {
    // data buffer for incoming data.
    byte[] bytes = new byte[1024];

    // connect to a remote device.
    try {
      // establish the remote endpoint for the socket.
      //    the name of the
      //   remote device is "host.contoso.com".
      iphostentry iphostinfo = dns.resolve("host.contoso.com");
      ipaddress ipaddress = iphostinfo.addresslist[0];
      ipendpoint remoteep = new ipendpoint(ipaddress,11000);

      // create a tcp/ip  socket.
      socket sender = new socket(addressfamily.internetwork,
        sockettype.stream, protocoltype.tcp );

      // connect the socket to the remote endpoint. catch any errors.
      try {
        sender.connect(remoteep);

        console.writeline("socket connected to {0}",
          sender.remoteendpoint.tostring());

        // encode the data string into a byte array.
        byte[] msg = encoding.ascii.getbytes("this is a test<eof>");

        // send the data through the  socket.
        int bytessent = sender.send(msg);

        // receive the response from the remote device.
        int bytesrec = sender.receive(bytes);
        console.writeline("echoed test = {0}",
          encoding.ascii.getstring(bytes,0,bytesrec));

        // release the socket.
        sender.shutdown(socketshutdown.both);
        sender.close();
        
      } catch (argumentnullexception ane) {
        console.writeline("argumentnullexception : {0}",ane.tostring());
      } catch (socketexception se) {
        console.writeline("socketexception : {0}",se.tostring());
      } catch (exception e) {
        console.writeline("unexpected exception : {0}", e.tostring());
      }

    } catch (exception e) {
      console.writeline( e.tostring());
    }
  }
  
  public static int main(string[] args) {
    startclient();
    return 0;
  }<



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 信宜市| 大港区| 巴中市| 太白县| 荆门市| 冷水江市| 杭锦旗| 苍山县| 盐山县| 兰溪市| 聂荣县| 金沙县| 高雄市| 通渭县| 马公市| 湛江市| 廊坊市| 长顺县| 丰镇市| 武邑县| 盘山县| 云浮市| 噶尔县| 新化县| 乐东| 民权县| 伊宁市| 大兴区| 云和县| 德惠市| 长岛县| 凌源市| 会宁县| 吐鲁番市| 商洛市| 天镇县| 肥西县| 吉安市| 淮阳县| 宣城市| 辉县市|