读取网络问题.NET
我尝试开发一个简单的 smtp 服务器,问题是当我尝试从网络读取消息时,所有消息都是带有大量“/0”的字符串,而不是键入的命令。我使用 telnet 连接到我的应用程序并输入消息。
这是我的代码:
public void StartListen()
{
SMTPParser parser = new SMTPParser();
SMTPResponder responder = new SMTPResponder();
Listening = true;
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 25);
TcpListener listener = new TcpListener(endPoint);
listener.Start();
while(Listening)
{
byte[] data = new byte[2048];
Socket socket = listener.AcceptSocket();
socket.Receive(data);
string cmd = Encoding.ASCII.GetString(data);
if(cmd !="") parser.Parse(cmd, responder);
}
}
I try to develop an simple smtp server, the problem is when I try to read message from network all messages are a string with a lot of "/0" instead of typed command. I use telnet to connect to my application and to type messages.
this is my code:
public void StartListen()
{
SMTPParser parser = new SMTPParser();
SMTPResponder responder = new SMTPResponder();
Listening = true;
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 25);
TcpListener listener = new TcpListener(endPoint);
listener.Start();
while(Listening)
{
byte[] data = new byte[2048];
Socket socket = listener.AcceptSocket();
socket.Receive(data);
string cmd = Encoding.ASCII.GetString(data);
if(cmd !="") parser.Parse(cmd, responder);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Socket.Receive
返回读取的字节数。目前,您总是从缓冲区的 2048 字节中获取字符串,即使只读取了几个字节。更改为:Socket.Receive
returns the number of read bytes. Currently, you're always getting a string from the 2048 bytes of the buffer, even if only a few bytes are read. Change to: