【发布时间】:2015-09-27 10:05:35
【问题描述】:
我正在使用 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'时,服务器端将接收到整个数据流。
【问题讨论】:
标签: c# sockets asynchronous