/// <summary> /// Start listening on udp port /// </summary> public override void Start() { if (_listen) return; try { Log.Info("PortZMq starting"); Log.Debug(string.Format("Publisher binding on {0}", _endpoint)); _res = new ResponseSocket(); _res.Bind(_localpoint); _listen = true; new Thread(() => { try { while (_listen) { byte[] data; while ((_res != null) && _res.HasIn) if (_res.TryReceiveFrameBytes(TimeSpan.FromSeconds(30), out data)) { Log.Debug(string.Format("Received {0} bytes", data.Length)); //Process received data if (!ProcessData(data, data.Length)) { _res.SignalError(); Log.Error(string.Format("Processing {0} bytes failed", data.Length)); } else { _res.SignalOK(); Log.Debug(string.Format("Processed {0} bytes", data.Length)); } } } } catch (Exception e) { Log.Error("PortZMq loop error", e); } }).Start(); base.Start(); } catch (Exception e) { Log.Error("PortZMq start error", e); } }
public ReactiveServer() { var context = NetMQContext.Create(); repSocket = context.CreateResponseSocket(); repSocket.Bind("tcp://127.0.0.1:3334"); repSocket.ReceiveReady += RepSocketOnReceiveReady; var repThread = new Thread(RepThread); repThread.Start(); pubSocket = context.CreatePublisherSocket(); pubSocket.Bind("tcp://127.0.0.1:3333"); }
private static void Main() { using (var response = new ResponseSocket()) { string address = GetComputerLanIP(); if (!string.IsNullOrEmpty(address)) { Console.WriteLine("Binding tcp://{0}:{1}", address, PortNumber); response.Bind(string.Format("tcp://{0}:{1}", address, PortNumber)); while (true) { bool hasMore; string msg = response.ReceiveFrameString(out hasMore); if (string.IsNullOrEmpty(msg)) { Console.WriteLine("No msg received."); break; } Console.WriteLine("Msg received! {0}", msg); response.SendFrame(msg, hasMore); Thread.Sleep(1000); } response.Options.Linger = TimeSpan.Zero; } else { Console.WriteLine("Wrong IP address"); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
private static int Main(string[] args) { if (args.Length != 3) { Console.WriteLine("usage: local_lat <bind-to> <message-size> <roundtrip-count>"); return 1; } string bindTo = args[0]; int messageSize = int.Parse(args[1]); int roundtripCount = int.Parse(args[2]); using (var rep = new ResponseSocket()) { rep.Bind(bindTo); var msg = new Msg(); msg.InitEmpty(); for (int i = 0; i != roundtripCount; i++) { rep.Receive(ref msg); if (msg.Size != messageSize) { Console.WriteLine("message of incorrect size received. Received: " + msg.Size + " Expected: " + messageSize); return -1; } rep.Send(ref msg, more: false); } msg.Close(); } return 0; }
private static void Main() { Console.Title = "NetMQ LazyPirate Server 2"; const string serverEndpoint = "tcp://127.0.0.1:5555"; var random = new Random(); using (var server = new ResponseSocket()) { Console.WriteLine("S: Binding address {0}", serverEndpoint); server.Bind(serverEndpoint); var cycles = 0; while (true) { var request = server.ReceiveFrameString(); cycles++; if (cycles > 3 && random.Next(0, 10) == 0) { Console.WriteLine("S: Simulating a crash"); Thread.Sleep(5000); } else if (cycles > 3 && random.Next(0, 10) == 0) { Console.WriteLine("S: Simulating CPU overload"); Thread.Sleep(1000); } Console.WriteLine("S: Normal request ({0})", request); server.SendFrame(request); } } }
void BeginServer() { using (var pubSocket = new PublisherSocket()) using (var server = new ResponseSocket()) { SetText($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} 服务器启动成功!"); //绑定订阅端口 pubSocket.Bind($"tcp://{pubIP}:{pubPort}"); //绑定服务端口 server.Bind($"tcp://{serverIP}:{serverPort}"); while (true) { //接收消息 string messsage = server.ReceiveFrameString(); #region 处理消息 var data = MessageHelper.GetMessage(messsage); if (data != null) { if (data.UserStatus.Equals(UserStatus.Connect)) { data.Message = $"欢迎{data.UserName}加入聊天室"; data.UserName = "******"; } if (data.UserStatus.Equals(UserStatus.Close)) { data.Message = $"{data.UserName}退出聊天室"; data.UserName = "******"; } string file_name = ""; if (data.File != null && !string.IsNullOrEmpty(data.File.FileName)) { file_name = "上传了【" + data.File.FileName + "】文件"; } string msg = ""; if (!string.IsNullOrEmpty(data.Message)) { msg = "发送了【" + data.Message + "】消息"; } SetText($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} {data.UserName}:{file_name} {msg}"); } else { data = new DataStructure { UserName = "******", Message = "网络异常", DateTime = DateTime.Now }; } #endregion // 推送消息 pubSocket.SendMoreFrame("all").SendFrame(MessageHelper.SendMessage(data)); server.SendFrameEmpty(); } } }
/// End of Getters /// Communication Function public static void Receive_From_Program() { AsyncIO.ForceDotNet.Force(); using (var server = new ResponseSocket()) { server.Bind("tcp://*:5555"); //Debug.Log("Using Server"); while (true) { //Debug.Log("inside the while loop"); string Input_From_Program = ""; //bool IsRecieved = server.TryReceiveFrameString(out Input_From_Program); Input_From_Program = server.ReceiveFrameString(); bool IsRecieved = true; if (IsRecieved == true) { string trim = Input_From_Program.Trim(); //Debug.Log(trim); string[] Parameters_List = Input_From_Program.Split('/'); if (Parameters_List[0] == "play" && Parameters_List[1] == "tm") { Set_turn(Parameters_List[2]); // Turn Debug.Log(Get_turn()); Set_Start_Col_From_Program(Parameters_List[3]); // Col Debug.Log(Get_Start_Col_From_Program()); Set_Start_Row_From_Program(Parameters_List[4]); // Row Debug.Log(Get_Start_Row_From_Program()); if (Parameters_List[5] == "true") { Set_Horizontal_From_Program(true); // Hor } else if (Parameters_List[5] == "false") { Set_Horizontal_From_Program(false); // Ver } Debug.Log(Get_Horizontal_From_Program()); Set_Word_From_Program(Parameters_List[6]); // The new word to be displayed on the board Debug.Log(Get_Word_From_Program()); Set_My_Score(Parameters_List[7]); // My Score Debug.Log(Get_My_Score()); Set_Opp_score(Parameters_List[8]); // Opp Score Debug.Log(Get_Opp_score()); Set_My_Time(Parameters_List[9]); // My Time Debug.Log(Get_My_Time()); Set_Opp_Time(Parameters_List[10]); // Opp Time Debug.Log(Get_Opp_Time()); Set_Time(Parameters_List[11]); // Total Time Debug.Log(Get_Time()); Set_Rack(Parameters_List[12]); // Rack tiles Debug.Log(Get_Rack()); Set_Message_From_Teacher(Parameters_List[13]); // Messege received from the teacher in teacher mode Debug.Log(Get_Message_From_Teacher()); Set_Ready_to_Recv(true); } else if (Parameters_List[0] == "board") { Set_board(Parameters_List[1]); Set_Ready_to_Recv(true); } else { Set_Ready_to_Recv(false); //Debug.Log(" receive dummy"); } if (Get_Ready_to_Send() == true && Get_Pass_Move() == false && Get_Exchange_Move() == false) { //Set_turn("1"); Set_Start_Col_From_Gui("7"); Set_Start_Row_From_Gui("7"); Set_Horizontal_From_Gui(true); Set_Word_From_GUi("SEVENTY"); Set_My_Score("70"); Set_Opp_score("0"); Set_My_Time("120"); Set_Opp_Time("10"); Set_Time("120"); Set_Rack(""); Set_Message_From_Teacher("Hello from the other side"); string Sending_String = "play/" + Get_turn() + "/" + Get_Start_Col_From_Gui() + "/" + Get_Start_Row_From_Gui() + "/" + Get_Horizontal_From_Gui() + "/" + Get_Word_From_GUi() + "/" + Get_My_Score() + "/" + Get_Opp_score() + "/" + Get_My_Time() + "/" + Get_Opp_Time() + "/" + Get_Time() + "/" + Get_Rack() + "/" + Get_Message_From_Teacher(); Set_Ready_to_Send(false); server.SendFrame(Sending_String); // send function to gui } else if (Get_Ready_to_Send() == true && Get_Pass_Move() == true) { server.SendFrame("####"); Set_Pass_Move(false); Set_Ready_to_Send(false); // send function to gui } else if (Get_Ready_to_Send() == true && Get_Exchange_Move() == true) { server.SendFrame("****" + Get_Exchanged_Value()); Set_Pass_Move(false); Set_Ready_to_Send(false); // send function to gui } else { Set_Ready_to_Send(false); server.SendFrame("0"); /// send zero if gui is not ready to send } } } } }
private void dataServer() { AsyncIO.ForceDotNet.Force(); using (var dataSender = new ResponseSocket()) { dataSender.Bind("tcp://" + VPETSettings.Instance.serverIP + ":5565"); Debug.Log("Enter while.. "); while (isRunning) { string message = ""; dataSender.TryReceiveFrameString(out message); //print ("Got request message: " + message); // re-run scene iteration if true if (doGatherOnRequest) { gatherSceneData(); } switch (message) { case "header": print("Send Header.. "); dataSender.SendFrame(headerByteData); print(string.Format(".. Nodes ({0} bytes) sent ", headerByteData.Length)); break; case "nodes": print("Send Nodes.. "); dataSender.SendFrame(nodesByteData); print(string.Format(".. Nodes ({0} bytes) sent ", nodesByteData.Length)); break; case "objects": print("Send Objects.. "); dataSender.SendFrame(objectsByteData); print(string.Format(".. Objects ({0} bytes) sent ", objectsByteData.Length)); break; case "characters": print("Send Characters.. "); dataSender.SendFrame(charactersByteData); print(string.Format(".. Characters ({0} bytes) sent ", charactersByteData.Length)); break; case "textures": print("Send Textures.. "); dataSender.SendFrame(texturesByteData); print(string.Format(".. Textures ({0} bytes) sent ", texturesByteData.Length)); break; #if TRUNK case "materials": print("Send Materials.. "); dataSender.SendFrame(materialsByteData); print(string.Format(".. Materials ({0} bytes) sent ", materialsByteData.Length)); break; #endif default: break; } } // TODO: check first if closed try { dataSender.Disconnect("tcp://" + VPETSettings.Instance.serverIP + ":5565"); dataSender.Dispose(); dataSender.Close(); } finally { NetMQConfig.Cleanup(false); } } }