/// <summary> /// Send a message to the server that needs to be broadcast to all other clients /// </summary> /// <param name="path">the path drawn on the client's DrawingBox that will be relayed to other clients</param> public void sendMessage(string path) { // prepare the data by converting the message informatino into a byte array byte[] data = (new TFMS_Data(TFMS_Command.Message, path, strName)).ToByte(); TFMS_Data lenToSend = new TFMS_Data(TFMS_Command.MsgLen, string.Format("{0}", data.Length), strName); // send the message to the socket clientSocket.Send(lenToSend.ToByte()); Thread.Sleep(TFMS_Constants.DELAY_TIME); // begin sending the message to the server clientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(OnSend), null); }
/// <summary> /// When the server receives a message from a client, it rebroadcasts the message to all other clients /// This method sends the message to a single client /// </summary> /// <param name="data">TFMS_Data object to be sent</param> /// <param name="socket">Socket belonging to the target client</param> /// <param name="send">function to be called when the message is done sending (should be "onSend")</param> /// <returns>the result of sending the message to the target client</returns> private IAsyncResult sendTFMSmsg(TFMS_Data data, Socket socket, AsyncCallback send) { // get the byte array of the TFMS_Data object byte[] message = data.ToByte(); Console.WriteLine("Sending length: {0}", message.Length); // syncronously send the length of the actual message and block until the message has been sent. socket.Send(new TFMS_Data(TFMS_Command.MsgLen, string.Format("{0}", message.Length), data.strName).ToByte()); Console.WriteLine("BeginSend: {0} msg", data.cmdCommand); // the async call to send the message // returns an IAsyncResult object return socket.BeginSend(message, 0, message.Length, SocketFlags.None, send, socket); }
/// <summary> /// Attempt to connect to the server at the specified IP address /// </summary> /// <param name="IP_address">the IP address of the target server ("a.b.c.d" format)</param> /// <returns>true if the connection was succesful, otherwise false</returns> public bool connect(string IP_address) { try { // create the socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // get the IP addresses, then connect them with the socket IPAddress serverip = IPAddress.Parse(IP_address); IPEndPoint ipEnd = new IPEndPoint(serverip, serverPort); clientSocket.Connect(ipEnd); // try to login after connecting to the server TFMS_Data msgToSend = new TFMS_Data(TFMS_Command.Login, null, strName); clientSocket.Send(msgToSend.ToByte()); // setup buffer to receive data from the server byteData = new byte[TFMS_Constants.BUFFER_SIZE]; // start listening to the data asynchronously clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), null); // the client successfully connected Console.WriteLine("{0} successfully connected to the server!", msgToSend.strName); return true; } catch (Exception) { // the client failed to connect Console.WriteLine("Client at '{0}' failed to connect to the server!", IP_address); return false; } }