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

首頁(yè) > 編程 > .NET > 正文

使用.NET訪問(wèn)Internet(4) Paul_Ni(原作)(補(bǔ)充)

2024-07-10 12:58:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
菜鳥(niǎo)學(xué)堂:
開(kāi)始從客戶端套接字接收數(shù)據(jù)的 acceptcallback 方法的此節(jié)首先初始化 stateobject 類(lèi)的一個(gè)實(shí)例,然后調(diào)用 beginreceive 方法以開(kāi)始從客戶端套接字異步讀取數(shù)據(jù)。

下面的示例顯示了完整的 acceptcallback 方法。它假定以下內(nèi)容:存在一個(gè)名為 alldone 的 manualresetevent 實(shí)例,定義了 stateobject 類(lèi),以及在名為 socketlistener 的類(lèi)中定義了 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);
  }
需要為異步套接字服務(wù)器實(shí)現(xiàn)的 final 方法是返回客戶端發(fā)送的數(shù)據(jù)的讀取回調(diào)方法。與接受回調(diào)方法一樣,讀取回調(diào)方法也是一個(gè) asynccallback 委托。該方法將來(lái)自客戶端套接字的一個(gè)或多個(gè)字節(jié)讀入數(shù)據(jù)緩沖區(qū),然后再次調(diào)用 beginreceive 方法,直到客戶端發(fā)送的數(shù)據(jù)完成為止。從客戶端讀取整個(gè)消息后,在控制臺(tái)上顯示字符串,并關(guān)閉處理與客戶端的連接的服務(wù)器套接字。

下面的示例實(shí)現(xiàn) readcallback 方法。它假定定義了 stateobject 類(lèi)。

[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();
  }
}
同步客戶端套接字示例
下面的示例程序創(chuàng)建一個(gè)連接到服務(wù)器的客戶端。該客戶端是用同步套接字生成的,因此掛起客戶端應(yīng)用程序的執(zhí)行,直到服務(wù)器返回響應(yīng)為止。該應(yīng)用程序?qū)⒆址l(fā)送到服務(wù)器,然后在控制臺(tái)顯示該服務(wù)器返回的字符串。

[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;
  }
}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 双辽市| 兴和县| 河北省| 昌江| 绵阳市| 永和县| 安阳县| 吉木萨尔县| 开鲁县| 武定县| 韩城市| 紫云| 清水河县| 祁阳县| 开阳县| 喜德县| 延庆县| 海盐县| 谢通门县| 徐州市| 保靖县| 金川县| 彰化县| 资溪县| 陆河县| 寻乌县| 武平县| 高阳县| 马边| 揭西县| 惠水县| 天台县| 内丘县| 天津市| 章丘市| 定安县| 竹溪县| 敦煌市| 安塞县| 饶平县| 浦北县|