gpt4 book ai didi

c# - 如何从服务器读取消息? (TCP)

转载 作者:太空宇宙 更新时间:2023-11-03 20:37:21 24 4
gpt4 key购买 nike

客户端写入服务器 - 服务器读取。和服务器写入客户端 - 客户端未读取。

服务器:

    using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

class SocketServer
{

public static void Main()
{

StreamReader streamReader;
NetworkStream networkStream;

TcpListener tcpListener = new TcpListener(5555);
tcpListener.Start();

Console.WriteLine("The Server has started on port 5555");
Socket serverSocket = tcpListener.AcceptSocket();

try
{
if (serverSocket.Connected)
{
Console.WriteLine("Client connected");
networkStream = new NetworkStream(serverSocket);

AsynchronousMethodDelegate asyncDeleg = new AsynchronousMethodDelegate(AsynchronousMethod);
AsyncCallback callback = new AsyncCallback(CallbackMethod);
asyncDeleg.BeginInvoke(networkStream, callback, asyncDeleg);
streamReader = new StreamReader(networkStream);
while (true)
{
Console.WriteLine(streamReader.ReadLine());
}

}

if (serverSocket.Connected)
serverSocket.Close();

Console.Read();
}

catch (SocketException ex)
{
Console.WriteLine(ex);
}
catch (IOException ex) { Console.WriteLine(ex); }
}



private static void CallbackMethod(IAsyncResult ar)
{
AsynchronousMethodDelegate asyncDeleg = (AsynchronousMethodDelegate)ar.AsyncState;
}
private delegate void AsynchronousMethodDelegate(NetworkStream networkStream);
private static void AsynchronousMethod(NetworkStream networkStream)
{
try
{
StreamWriter streamWriter;
streamWriter = new StreamWriter(networkStream);
while (true)
{
streamWriter.WriteLine(Console.ReadLine());
}
}
catch (SocketException ex){}
}
}

客户:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

class SocketClient
{
static void Main(string[] args)
{
TcpClient tcpClient;
NetworkStream networkStream;

StreamWriter streamWriter;

try
{
tcpClient = new TcpClient("localhost", 5555);
networkStream = tcpClient.GetStream();

AsynchronousMethodDelegate asyncDeleg = new AsynchronousMethodDelegate(AsynchronousMethod);
AsyncCallback callback = new AsyncCallback(CallbackMethod);
asyncDeleg.BeginInvoke(networkStream, callback, asyncDeleg);


streamWriter = new StreamWriter(networkStream);
while (true)
{
streamWriter.WriteLine(Console.ReadLine());
streamWriter.Flush();
//Console.WriteLine(streamReader.ReadLine());
}
//Console.Read();
}
catch (SocketException ex){
Console.WriteLine(ex);
}

}

private static void CallbackMethod(IAsyncResult ar)
{
AsynchronousMethodDelegate asyncDeleg = (AsynchronousMethodDelegate)ar.AsyncState;
//lblCallback.Text = asyncDeleg.EndInvoke(ar);
}

private delegate void AsynchronousMethodDelegate(NetworkStream networkStream);

private static void AsynchronousMethod(NetworkStream networkStream)
{
try
{
StreamReader streamReader = new StreamReader(networkStream);
while (true)
{

Console.WriteLine(streamReader.ReadLine());
}
}
catch (SocketException ex) { }
catch (IOException ioex) { }
}

}

最佳答案

首先,您不了解多线程/异步网络编程的工作原理。不要使用大量自制委托(delegate)。

其次,不要捕获那个异常。只捕获你可以处理的异常,或者防止你的应用程序关闭。您可以在我的博客中阅读有关异常的更多信息:http://blog.gauffin.org/tag/exceptions/

清理服务器:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

class SocketServer
{

public static void Main()
{

StreamReader streamReader;
NetworkStream networkStream;

TcpListener tcpListener = new TcpListener(5555);
tcpListener.Start();

Console.WriteLine("The Server has started on port 5555");
Socket serverSocket = tcpListener.AcceptSocket();

try
{
Console.WriteLine("Client connected");
networkStream = new NetworkStream(serverSocket);

streamReader = new StreamReader(networkStream);
while (true)
{
var line = streamReader.ReadLine();
Console.WriteLine(line);

//echoing
var buffer = Encoding.ASCII.GetBytes(line);
networkStream.Write(buffer, 0, buffer.Length);
}

serverSocket.Close();
Console.Read();
}

catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}

清理客户端:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

class SocketClient
{
static void Main(string[] args)
{
TcpClient tcpClient;
NetworkStream networkStream;
StreamWriter streamWriter;

try
{
tcpClient = new TcpClient("localhost", 5555);
networkStream = tcpClient.GetStream();

streamWriter = new StreamWriter(networkStream);
while (true)
{
streamWriter.WriteLine(Console.ReadLine());
streamWriter.Flush();
Console.WriteLine(streamReader.ReadLine());
}
Console.Read();
}
catch (SocketException ex){
Console.WriteLine(ex);
}

}
}

如果您想要异步客户端/服务器读取有关 BeginRead/EndRead、BeginWrite、EndWrite 的信息。不要与您自己的代表混在一起。

关于c# - 如何从服务器读取消息? (TCP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4517043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com