你了解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();
}
}
最简单的客户端:
{
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();
}TrueNAS(原FreeNAS)是一套基于FreeBSD *** 作系统核心的开放源代码的网络存储设备(NAS)服务器系统,支持众多服务,用户访问权限管理,提供网页设置接口。
TrueNAS(12%0 版前称为 FreeNAS)是一套基于 FreeBSD *** 作系统核心的开放源代码的网络存储设备(英语:NAS)服务器系统,支持众多服务,用户访问权限管理,提供网页设置接口。FreeNAS 当前版本整个系统总共需 2gb 以上 USB 驱动器进行安装,并支持 USB 存储设备、LiveCD、CF 卡(转接成 IDE 设备)及硬盘等引导方式。目前有 LiveCD、镜像档、VMware 磁盘映像档三种发行方式。如您云主机有些TCP或UDP端口不能使用,请先确认使用的端口在您开通云主机时是否有申请开通,如之前已申请开通,请检查云主机上的防火墙是否有做相应的规则;如之前没有申请开通该端口,则需要向系统集成中心提交端口开通申请表开通该端口。(上述内容仅适用于广东联通用户)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)