gpt4 book ai didi

c# - 从 Flex (XMLSocket) 连接到 .NET (Socket) 的数据问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:40 25 4
gpt4 key购买 nike

我正在为我的游戏(用 Flex 编写)开发一个聊天/IM 系统,并希望通过套接字将它连接到我的服务器(用 C# 编写)。

因此,我已经使用 Flex 上的 XMLSocket 和服务器端的 Socket 成功地将它们连接在一起,客户端获得连接事件,正确发送数据,但是当我尝试将数据从服务器发送回客户端时,没有任何反应(即使 BeginSend 回调显示整个缓冲区已发送)。

客户:

private var hostName:String = "localhost";
private var port:uint = 4444;
private var socket:XMLSocket;
public var app:DotNETServerTest;

public function DotNetSocketExample() {
socket = new XMLSocket();
configureListeners(socket);
socket.connect(hostName, port);
}

public function send(data:Object):void {
socket.send(data);
}

public function disconnect():void {
socket.close();
closeHandler(null);
}

private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.CLOSE, closeHandler);
dispatcher.addEventListener(Event.CONNECT, connectHandler);
dispatcher.addEventListener(DataEvent.DATA, dataHandler);
}

private function closeHandler(event:Event):void {
trace("closeHandler: " + event);
app.send_btn.enabled = false;
app.disconnect_btn.enabled = false;
}

private function connectHandler(event:Event):void {
trace("connectHandler: " + event);
app.send_btn.enabled = true;
app.disconnect_btn.enabled = true;
}

private function dataHandler(event:DataEvent):void {
trace("dataHandler: " + event);
Alert.show(event.data);
}

服务器(仅限特定部分):

// This is the call back function, which will be invoked when a client is connected
public static void OnClientConnect(IAsyncResult asyn)
{
try
{
SocketClient NewConnection = new SocketClient(SocketServer.EndAccept(asyn));

// This is a test message I'm attempting to send
SendMessageTo(NewConnection, "<test></test>");
Clients.Add(NewConnection);
WaitForData(NewConnection);
LogMessage(NewConnection, "Client # {0} connected", Clients.Count);

SocketServer.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
catch (ObjectDisposedException)
{
LogMessage("OnClientConnection: Socket has been closed.");
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
}

internal static void SendMessageTo(SocketClient client, string Data)
{
byte[] byteData = Encoding.ASCII.GetBytes(Data);
SendMessageTo(client, byteData);
}

internal static void SendMessageTo(SocketClient client, byte[] byteData)
{
try
{
client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);
}
catch (Exception e)
{
LogMessage(client, "Error sending data to client: {0}", e.Message);
}
}

internal static void SendCallback(IAsyncResult ar)
{
// Retrieve the socket from the async state object.
SocketClient handler = (SocketClient)ar.AsyncState;
try
{
int bytesSent = handler.socket.EndSend(ar);
}
catch (Exception e)
{
LogMessage(handler, e.Message);
}
}

求助!谢谢,罗恩

最佳答案

发现问题 - 我没有在每次发送结束时发送\0。

改变了这个:

byte[] byteData = Encoding.ASCII.GetBytes(Data);
client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);

对此:

byte[] byteData = Encoding.ASCII.GetBytes(Data + "\0");
client.socket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);

关于c# - 从 Flex (XMLSocket) 连接到 .NET (Socket) 的数据问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3742543/

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