private void sendMsg(FuncMsgClient.FuncType type, List <object> args) { FuncMsgClient msg = new FuncMsgClient(); msg.type = type; msg.args = args; BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(_server.GetStream(), msg); }
public void run() { try { BinaryFormatter bformatter = new BinaryFormatter(); while (true) { FuncMsgClient msg = (FuncMsgClient)bformatter.Deserialize(_con.GetStream()); FuncMsgServer response = _usrHand.processMsg(msg); bformatter.Serialize(_con.GetStream(), response); } } catch { return; } }
static void Main2(string[] args) { FuncMsgClient msg = new FuncMsgClient(); FuncMsgClient msg2 = new FuncMsgClient(); SubForumInfo info = new SubForumInfo(); info.Name = "forum1"; SubForumInfo info2 = new SubForumInfo(); info2.Name = "forum2"; msg.type = FuncMsgClient.FuncType.Replay; msg.args = new List <object>(); msg.args.Add(info); msg2.type = FuncMsgClient.FuncType.ErrorReplay; msg2.args = new List <object>(); msg2.args.Add(info2); TcpClient gameServer = new TcpClient(); //try //{ System.Console.WriteLine("connecting..."); gameServer.Connect("192.168.2.102", 12345); System.Console.WriteLine("connected"); //} //catch (SocketException exp) //{ // System.Console.WriteLine("error"); // System.Console.WriteLine(exp); // System.Console.ReadLine(); // return; //} NetworkStream sourceStream = gameServer.GetStream(); BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(sourceStream, msg); bformatter.Serialize(sourceStream, msg2); sourceStream.Close(); }
public FuncMsgServer processMsg(FuncMsgClient msg) { try { object response = _actions[(int)msg.type](msg.args); List <object> responseLst = new List <object> { response }; FuncMsgServer responseMsg = new FuncMsgServer { type = FuncMsgServer.FuncType.Replay, args = responseLst }; return(responseMsg); } catch { FuncMsgServer responseMsg = new FuncMsgServer { type = FuncMsgServer.FuncType.ErrorReplay, args = null }; return(responseMsg); } }
static void Main2(string[] args) { IPAddress aa = LocalIPAddress(); TcpListener _listener = new TcpListener(aa, 12345); _listener.Start(); TcpClient Client = null; try { Console.WriteLine("listening"); Client = _listener.AcceptTcpClient(); Console.WriteLine("got client"); } catch (SocketException exp) { Console.WriteLine("Error"); return; } BinaryFormatter bformatter = new BinaryFormatter(); System.Threading.Thread.Sleep(5000); Console.WriteLine("Reading msg1"); FuncMsgClient msg = (FuncMsgClient)bformatter.Deserialize(Client.GetStream()); FuncMsgClient msg2 = (FuncMsgClient)bformatter.Deserialize(Client.GetStream()); System.Console.WriteLine(msg.type); System.Console.WriteLine(((SubForumInfo)msg.args[0]).Name); Console.WriteLine("Reading msg2"); System.Console.WriteLine(msg2.type); System.Console.WriteLine(((SubForumInfo)msg2.args[0]).Name); Client.Close(); System.Console.ReadLine(); }