我正在为我的游戏(使用Flex编写)开发聊天/即时消息系统,并希望通过套接字将其连接到我的服务器(用C#编写)。
所以,我已经使用FlexS上的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 :(得分:2)
发现问题 - 我没有在每次发送结束时发送\ 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);