本文整理汇总了C#中Socket.GetStream方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.GetStream方法的具体用法?C# Socket.GetStream怎么用?C# Socket.GetStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.GetStream方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Listen
protected async Task Listen(Socket socket) {
Stream ioStream = socket.GetStream();
StreamReader reader = new StreamReader(ioStream);
StreamWriter writer = new StreamWriter(ioStream);
await writer.WriteLineAsync(FtpResponse.PositiveCompletionReply.ServiceReady.ToString());
await writer.FlushAsync();
string command;
while (true) {
string result = await RunCommand(await reader.ReadLineAsync());
await writer.WriteLineAsync(result);
await writer.FlushAsync();
}
}
示例2: SendMessageFromSocket
static void SendMessageFromSocket(int port)
{
// Буфер для входящих данных
byte[] bytes = new byte[1024];
// Соединяемся с удаленным устройством
// Устанавливаем удаленную точку для сокета
IPHostEntry ipHost = Dns.GetHostEntry("localhost");
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, port);
Socket sender = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
// Соединяем сокет с удаленной точкой
sender.Connect(ipEndPoint);
string path = Console.ReadLine();
NetworkStream stream = sender.GetStream();
BinaryFormatter format = new BinaryFormatter();
byte[] buf = new byte[1024];
int count;
int process = 0;
int endprocess = 100;
FileStream fs = new FileStream("C:\\" + path, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
long k = fs.Length;
format.Serialize(stream, path);
format.Serialize(stream, k.ToString());
while ((count = br.Read(buf, 0, 1024)) > 0)
{
process++;
Console.Clear();
Console.Write(((process * 1024) * 100) / fs.Length + "%");
format.Serialize(stream, buf);
}
Console.WriteLine("\nSuccessfully send");
stream.Close();
// Освобождаем сокет
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}