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

c#(Socket)同步套接字代碼示例

2022-06-13   來源: .NET編程 

  同步客戶端套接字示例
    下面的示例程序創建一個連接到服務器的客戶端該客戶端是用同步套接字生成的因此掛起客戶端應用程序的執行直到服務器返回響應為止該應用程序將字符串發送到服務器然後在控制台顯示該服務器返回的字符串
    C#
    using System;
    using SystemNet;
    using SystemNetSockets;
    using SystemText;
    public class SynchronousSocketClient {
    public static void StartClient() {
    // Data buffer for incoming data
    byte[] bytes = new byte[];
    // Connect to a remote device
    try {
    // Establish the remote endpoint for the socket
    // This example uses port on the local computer
    IPHostEntry ipHostInfo = DnsResolve(DnsGetHostName())
    IPAddress ipAddress = ipHostInfoAddressList[];
    IPEndPoint remoteEP = new IPEndPoint(ipAddress);
    // Create a TCP/IP  socket
    Socket sender = new Socket(AddressFamilyInterNetwork
    SocketTypeStream ProtocolTypeTcp );
    // Connect the socket to the remote endpoint Catch any errors
    try {
    senderConnect(remoteEP);
    ConsoleWriteLine(Socket connected to {}
    senderRemoteEndPointToString());
    // Encode the data string into a byte array
    byte[] msg = EncodingASCIIGetBytes(This is a test<EOF>);
    // Send the data through the socket
    int bytesSent = senderSend(msg);
    // Receive the response from the remote device
    int bytesRec = senderReceive(bytes);
    ConsoleWriteLine(Echoed test = {}
    EncodingASCIIGetString(bytesbytesRec));
    // Release the socket
    senderShutdown(SocketShutdownBoth);
    senderClose();
    } catch (ArgumentNullException ane) {
    ConsoleWriteLine(ArgumentNullException : {}aneToString());
    } catch (SocketException se) {
    ConsoleWriteLine(SocketException : {}seToString());
    } catch (Exception e) {
    ConsoleWriteLine(Unexpected exception : {} eToString());
    }
    } catch (Exception e) {
    ConsoleWriteLine( eToString());
    }
    }
    public static int Main(String[] args) {
    StartClient();
    return ;
    }
    }
    同步服務器套接字示例 下面的示例程序創建一個接收來自客戶端的連接請求的服務器該服務器是用同步套接字生成的
    因此在等待來自客戶端的連接時掛起服務器應用程序的執行該應用程序接收來自客戶端的字符串
    在控制台顯示該字符串然後將該字符串回顯到客戶端來自客戶端的字符串必須包含字符串<EOF>
    以發出表示消息結尾的信號

  C#
     復制代碼
    using System;
    using SystemNet;
    using SystemNetSockets;
    using SystemText;
    public class SynchronousSocketListener {
    // Incoming data from the client
    public static string data = null;
    public static void StartListening() {
    // Data buffer for incoming data
    byte[] bytes = new Byte[];
    // Establish the local endpoint for the socket
    // DnsGetHostName returns the name of the
    // host running the application
    IPHostEntry ipHostInfo = DnsResolve(DnsGetHostName());
    IPAddress ipAddress = ipHostInfoAddressList[];
    IPEndPoint localEndPoint = new IPEndPoint(ipAddress );
    // Create a TCP/IP socket
    Socket listener = new Socket(AddressFamilyInterNetwork
    SocketTypeStream ProtocolTypeTcp );
    // Bind the socket to the local endpoint and
    // listen for incoming connections
    try {
    listenerBind(localEndPoint);
    listenerListen();
    // Start listening for connections
    while (true) {
    ConsoleWriteLine(Waiting for a connection);
    // Program is suspended while waiting for an incoming connection
    Socket handler = listenerAccept();
    data = null;
    // An incoming connection needs to be processed
    while (true) {
    bytes = new byte[];
    int bytesRec = handlerReceive(bytes);
    data += EncodingASCIIGetString(bytesbytesRec);
    if (dataIndexOf(<EOF>) > ) {
    break;
    }
    }
    // Show the data on the console
    ConsoleWriteLine( Text received : {} data);
    // Echo the data back to the client
    byte[] msg = EncodingASCIIGetBytes(data);
    handlerSend(msg);
    handlerShutdown(SocketShutdownBoth);
    handlerClose();
    }
    } catch (Exception e) {
    ConsoleWriteLine(eToString());
    }
    ConsoleWriteLine(\nPress ENTER to continue);
    ConsoleRead();
    }
    public static int Main(String[] args) {
    StartListening();
    return ;
    }
    }


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