C#编写的程序如何连接云服务器

C#编写的程序如何连接云服务器,第1张

你了解TCP/IP socket编程相关知识吗?

网页链接

首先你要在云服务器上运行一个服务器程序,然后在本机运行客户端程序,两者通过TCP协议通讯交换数据(即你所说的连上云服务器)。

最简单的服务器程序:

using System;
using SystemIO;
using SystemNet;
using SystemNetSockets;
using SystemText;
class MyTcpListener
{
  public static void Main()
  { 
    TcpListener server=null;   
    try
    {
      // Set the TcpListener on port 13000
      Int32 port = 13000;
      IPAddress localAddr = IPAddressParse("127001");
      
      // TcpListener server = new TcpListener(port);
      server = new TcpListener(localAddr, port);
      // Start listening for client requests
      serverStart();
         
      // Buffer for reading data
      Byte[] bytes = new Byte[256];
      String data = null;
      // Enter the listening loop
      while(true) 
      {
        ConsoleWrite("Waiting for a connection ");
        
        // Perform a blocking call to accept requests
        // You could also user serverAcceptSocket() here
        TcpClient client = serverAcceptTcpClient();            
        ConsoleWriteLine("Connected!");
        data = null;
        // Get a stream object for reading and writing
        NetworkStream stream = clientGetStream();
        int i;
        // Loop to receive all the data sent by the client
        while((i = streamRead(bytes, 0, bytesLength))!=0) 
        {   
          // Translate data bytes to a ASCII string
          data = SystemTextEncodingASCIIGetString(bytes, 0, i);
          ConsoleWriteLine("Received: {0}", data);
       
          // Process the data sent by the client
          data = dataToUpper();
          byte[] msg = SystemTextEncodingASCIIGetBytes(data);
          // Send back a response
          streamWrite(msg, 0, msgLength);
          ConsoleWriteLine("Sent: {0}", data);            
        }
         
        // Shutdown and end connection
        clientClose();
      }
    }
    catch(SocketException e)
    {
      ConsoleWriteLine("SocketException: {0}", e);
    }
    finally
    {
       // Stop listening for new clients
       serverStop();
    }
      
    ConsoleWriteLine("\nHit enter to continue");
    ConsoleRead();
  }   
}

最简单的客户端:

static void Connect(String server, String message) 
{
  try 
  {
    // Create a TcpClient
    // Note, for this client to work you need to have a TcpServer 
    // connected to the same address as specified by the server, port
    // combination
    Int32 port = 13000;
    TcpClient client = new TcpClient(server, port);
    
    // Translate the passed message into ASCII and store it as a Byte array
    Byte[] data = SystemTextEncodingASCIIGetBytes(message);         
    // Get a client stream for reading and writing
    //  Stream stream = clientGetStream();
    
    NetworkStream stream = clientGetStream();
    // Send the message to the connected TcpServer 
    streamWrite(data, 0, dataLength);
    ConsoleWriteLine("Sent: {0}", message);         
    // Receive the TcpServerresponse
    
    // Buffer to store the response bytes
    data = new Byte[256];
    // String to store the response ASCII representation
    String responseData = StringEmpty;
    // Read the first batch of the TcpServer response bytes
    Int32 bytes = streamRead(data, 0, dataLength);
    responseData = SystemTextEncodingASCIIGetString(data, 0, bytes);
    ConsoleWriteLine("Received: {0}", responseData);         
    // Close everything
    streamClose();         
    clientClose();         
  } 
  catch (ArgumentNullException e) 
  {
    ConsoleWriteLine("ArgumentNullException: {0}", e);
  } 
  catch (SocketException e) 
  {
    ConsoleWriteLine("SocketException: {0}", e);
  }
    
  ConsoleWriteLine("\n Press Enter to continue");
  ConsoleRead();
}

1、接入设备通过以太网物理PC相连接的方式,在空间距离不受限制,这也就是网络电脑终端机的概念。 2、接入设备通过USB 20线缆于物理PC相连接方式,接入设备与物理PC之间的USB线缆的最大长度是5米(可以通过有源USB Hub提升),这也就是电脑共享器的原始概念。 3、通过PCI接口的硬件板卡,安装在计算机机箱内部。接入硬件通过超五类双绞线或六类双绞线连接到物理PC,由于在线缆上传输的是模拟信号而不是标准的以太网数据,这些线缆不能连接到以太网交换机或路由器上,线缆的长度大致上限制到10米,这就是拖机卡的概念。天源腾创yun0101友情回答。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/13470472.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-08-13
下一篇 2023-08-13

发表评论

登录后才能评论

评论列表(0条)

保存