Flash客户端与C#服务器通讯的消息接发问题

threem 2004-08-14 02:42:34
C#控制台程序代码:----------------------------------
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApplication6
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string data;
data="";
IPEndPoint ipep=new IPEndPoint(IPAddress.Parse("127.0.0.1"),9050);
Socket newsock= new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
Console.WriteLine("Wait for a connect ...");
Socket client = newsock.Accept();
IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",newclient.Address,newclient.Port);

NetworkStream ns = new NetworkStream(client);
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
string welome="Welcome to ,my test server";
sw.WriteLine(welome);
sw.Flush();
sw.Write("<LOGINREPLY login='1' msg='Msg'/>");
sw.Flush();
while(true)
{
try
{
data=sr.ReadLine();
}
catch(IOException e)

{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
Console.WriteLine(data);
sw.WriteLine("<LOGINREPLY msg='Msg'/>");
sw.Flush();
}
Console.WriteLine("Disconnect form {0} ",newclient.Address);
sw.Close();
sr.Close();
ns.Close();
}
}
}
Flash客户端的代码:---------------------------------
Flash第一帧的帧上加脚本:
// Create a new XMLSocket object
sock = new XMLSocket();
// Call its connect() method to establish a connection with port 1024
// of the server at the URL
// Define a function to assign to the sock object that handles
// the server?s response.If the connection succeeds, send the
// myXML object.If it fails, provide an error message in a text
// field.
function onSockConnect(success){
if (success){
trace("Connect to server success!/n");
} else {
msg="There has been an error connecting to "+serverName;
}
}
sock.onXML=function(xmlText){
trace("Get xmlText from server");
}
sock.onData=function(data){
trace("Get Msg from server");
}
// Assign the onSockConnect() function to the onConnect property
sock.onConnect = onSockConnect;
sock.connect("127.0.0.1", 9050);
trace("connect...");
stop();
-------------
Flash舞台上放一个向服务器发送信息的按钮,代码如下:
on(release){
sock.send("Hello! 01\n");
sock.send("Hello! 02\n");
}
---------------------------------------
本地已经测试通过,C#能接收到Flash发送的信息,但是flash却收不到C#服务器发回的信息。

请各位一起帮忙看一下!谢谢!
...全文
211 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
JackLucifer 2005-06-10
  • 打赏
  • 举报
回复
up
SunnieZhong 2004-11-15
  • 打赏
  • 举报
回复
碰到同样问题,请大家过来帮忙,谢谢!
21xxb 2004-08-18
  • 打赏
  • 举报
回复
把sw.Write("<LOGINREPLY login='1' msg='Msg'/>");
改为
sw.Write("<LOGINREPLY login='1' msg='Msg'/>\0");
试试。
threem 2004-08-14
  • 打赏
  • 举报
回复
自己Up一下! :)
werdcd 2004-08-14
  • 打赏
  • 举报
回复
你应该在listen后,写一个while(true),如果有新的连接进来,应该建立新的套接字,感觉是你这里处理有问题

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧