gpt4 book ai didi

c# - 异步服务器套接字缺少第一个缓冲区流

转载 作者:行者123 更新时间:2023-12-03 12:04:46 25 4
gpt4 key购买 nike

我正在使用C#异步服务器套接字,它总是会错过第一个缓冲区流。我已经调试了客户端,它没有丢失数据的迹象。我怀疑故障出在服务器上,但我似乎无法查明问题所在。

服务器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace ChatServer
{
class TCPServer
{
private List<Socket> socketList;
private const int PORT = 1337;
private Socket serverSocket;
const int BUFFER_SIZE = 1;
byte[] receiveBuffer = new byte[BUFFER_SIZE];
public TCPServer()
{
socketList = new List<Socket>();
}

public void Go()
{
SetupChat();
StartupNetwork();
AcceptConnections();
}

private void HandleDisconnections()
{
// throw new NotImplementedException();
}

private void Update()
{
// throw new NotImplementedException();
}

private void DumpGarbageSocket()
{

}

private void ProcessMessages()
{

}

private void SendToAllClients(string inMsg)
{
foreach (Socket clientSock in socketList)
{
SendMSGToClient(clientSock, inMsg);
}
}

private void AcceptConnections()
{
serverSocket.BeginAccept(ClientSocket.BUFFER_SIZE, StartAccepting, serverSocket);
}

private void StartAccepting(IAsyncResult inAsyncResult)
{
try
{
Socket serverSock = (Socket)inAsyncResult.AsyncState;
Socket clientSocket = serverSock.EndAccept(inAsyncResult);
socketList.Add(clientSocket);
ClientSocket cSocket = new ClientSocket();
cSocket.clientSocket = clientSocket;
Console.WriteLine("[SYSTEM] Socket has been connected.");
clientSocket.BeginReceive(cSocket.buffer_stream, 0, ClientSocket.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(StartReceiving), cSocket);
}
catch (Exception ex)
{
Console.WriteLine("[ERROR] " + ex.Message);
}
finally
{
serverSocket.BeginAccept(ClientSocket.BUFFER_SIZE, StartAccepting, serverSocket);
}
}

private void StartReceiving(IAsyncResult inAsyncResult)
{
try
{
string content = String.Empty;
ClientSocket cSocket = (ClientSocket)inAsyncResult.AsyncState;
Socket clientSocket = cSocket.clientSocket;
int bytesRead = clientSocket.EndReceive(inAsyncResult);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
cSocket.message.Append(Encoding.ASCII.GetString(
cSocket.buffer_stream, 0, bytesRead));

// Check for end-of-file tag. If it is not there, read
// more data.
content = cSocket.message.ToString();
if (content.IndexOf("<EOF>") > -1)
{
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine(content.Substring(0, content.Length - 5));
SendToAllClients(content.Substring(0, content.Length - 5) + "<EOF>");
cSocket.message.Remove(0, cSocket.message.Length);
}
else
{
// Not all data received. Get more.
}
clientSocket.BeginReceive(cSocket.buffer_stream, 0, ClientSocket.BUFFER_SIZE, SocketFlags.None, new AsyncCallback(StartReceiving), cSocket);
}
}
catch (Exception err)
{
Console.WriteLine("[ERROR] " + err.Message);
}
}

private void SendMSGToClient(Socket inSocketToSend, string message)
{
byte[] dataBuffer = Encoding.ASCII.GetBytes(message);
inSocketToSend.BeginSend(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, new AsyncCallback(MsgFinishSending), inSocketToSend);
}

private void MsgFinishSending(IAsyncResult inAsyncResult)
{
Socket handler = (Socket)inAsyncResult.AsyncState;
int bytesSent = handler.EndSend(inAsyncResult);
}

private void StartupNetwork()
{
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, PORT);
Console.WriteLine("SERVER::STARTING SOCKET");
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(localEndPoint);
serverSocket.Listen(100);
Console.WriteLine("SERVER::BINDED");
Console.WriteLine("SERVER::IP ADDRESS::" + localEndPoint.Address.ToString());
}

private void SetupChat()
{
//throw new NotImplementedException();
}


}
}

示例:缓冲区大小为10,因此如果我通过“abcdefghijklmn”发送,则服务器端将仅接收“klmn”。第二次发送“abcdefghijklmn”时,服务器端将接收整个数据流。

最佳答案

BeginAccept方法中,您传入了要读取的缓冲区大小,但未在回调中处理该大小。尝试使用不带缓冲区大小的BeginAccept方法作为第一个参数

关于c# - 异步服务器套接字缺少第一个缓冲区流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31310574/

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