/// <summary> /// 連線至主機 /// </summary> public void ConnectToServer() { //先建立IPAddress物件,IP為欲連線主機之IP IPAddress ipa = IPAddress.Parse(ConnectManager.ServerIP); //建立IPEndPoint IPEndPoint ipe = new IPEndPoint(ipa, ConnectManager.Port); //先建立一個TcpClient; TcpClient tcpClient = new TcpClient(); //開始連線 try { Debug.Log("主機IP=(" + ipa.ToString() + ") Port(" + ConnectManager.Port + ")"); Debug.Log("客戶端連線至主機中...\n"); tcpClient.Connect(ipe); if (tcpClient.Connected) { Debug.Log("客服端連線成功!"); CommunicationBase cb = new CommunicationBase(); cb.SendMsg("這是客戶端傳給主機的訊息", tcpClient); Debug.Log(cb.ReceiveMsg(tcpClient)); } else { Debug.Log("客服端連線失敗!"); } } catch (System.Exception ex) { tcpClient.Close(); Debug.Log(ex.Message); } }
/// <summary> /// Communicate /// </summary> public void Communicate() { try { CommunicationBase cb = new CommunicationBase(); string msg = cb.ReceiveMsg(this.mTcpClient); Debug.Log(msg + "\n"); cb.SendMsg("伺服器主機回傳訊息!", this.mTcpClient); } catch { Debug.Log("客戶端主動關閉連線!"); this.mTcpClient.Close(); } }
public void Communicate() { try { CommunicationBase comB = new CommunicationBase(); comB.SendMsg("You can send data", mclient); bool run = true; string[] allFile; int count = 0; while (run == true) { string receiveMsg = comB.ReadMessage(mclient); switch (receiveMsg) { case "": run = false; break; default: count++; allFile = receiveMsg.Split('\n'); List <string> bfile = allFile.ToList(); bfile.Remove(bfile.Last()); SendtoAzure(bfile); break; } } this.mclient.Close(); } catch { Trace.WriteLine("Client close the connection"); this.mclient.Close(); } }
/// <summary> /// Connect to server /// </summary> public void ConnectToServer() { string hostIP = "127.0.0.1"; IPAddress ipa = IPAddress.Parse(hostIP); IPEndPoint ipe = new IPEndPoint(ipa, 1234); TcpClient tcpClient = new TcpClient(); try { Console.WriteLine("Server IP=" + ipa.ToString()); Console.WriteLine("Connecting...\n"); tcpClient.Connect(ipe); if (tcpClient.Connected) { Console.WriteLine("Connected!"); CommunicationBase cb = new CommunicationBase(); //cb.SendMsg("Register;username;password", tcpClient); //cb.SendMsg("LogIn;username;password1", tcpClient); //cb.SendMsg("AddSchedule;username;lot;max_lot_distance;max_wait_time", tcpClient); //cb.SendMsg("GetSchedule;username", tcpClient); //cb.SendMsg("UpdateColors;username;full_color;busy_color;free_color", tcpClient); cb.SendMsg("GetColors;username", tcpClient); Console.WriteLine(cb.ReceiveMsg(tcpClient)); } else { Console.WriteLine("Fail!"); } Console.Read(); } catch (Exception ex) { tcpClient.Close(); Console.WriteLine(ex.Message); Console.Read(); } }
} // end ListenToConnect() /// <summary> /// Communicate /// </summary> private void Communicate(TcpClient mTcpClient) { try { CommunicationBase cb = new CommunicationBase(); string[] msg = cb.ReceiveMsg(mTcpClient).Split(';'); string result = "Error"; switch (msg[0]) { case "Register": if (UserList.Count(x => x.Account == msg[1]) == 0) { UserList.Add(new User() { Account = msg[1], Password = msg[2] }); result = "true"; } else { result = string.Format("false;User Account:{0} Exists!", msg[1]); } break; case "LogIn": if (UserList.Count(x => x.Account == msg[1] && x.Password == msg[2]) == 0) result = "false"; else result = "true"; break; case "UpdateColors": User colors = UserList.FirstOrDefault(x => x.Account == msg[1]); if (colors != null) { if (msg.Length == 5) { colors.ColorFull = msg[2]; colors.ColorBusy = msg[3]; colors.ColorFree = msg[4]; result = "true"; } else { result = "Incorrect format!"; } } else { result = "User Not Exist!"; } break; case "GetColors": User user = UserList.FirstOrDefault(x => x.Account == msg[1]); if (user != null) { result = string.Format("{0};{1};{2}", user.ColorFull, user.ColorBusy, user.ColorFree); } else { result = "User Not Exist!"; } break; case "AddSchedule": if (msg.Length == 5) { ScheduleList.Add(new Schedule() { Account = msg[1], Lot = msg[2], Distance = msg[3], Time = msg[4] }); result = "true"; } else { result = "Incorrect format!"; } break; case "GetSchedule": Schedule[] schedules = ScheduleList.Where(x => x.Account == msg[1]).ToArray(); result = ""; foreach (Schedule s in schedules) { result += string.Format("{0};{1};{2}@", s.Lot, s.Distance, s.Time); } break; default: result = "Unknown function: " + msg[0]; break; } cb.SendMsg(result, mTcpClient); } catch { Console.WriteLine("Client disconnected!"); mTcpClient.Close(); Console.Read(); } } // end Communicate()