public void exchangeData(object source, ElapsedEventArgs e) { if (handler.isConnected()) { handler.Send(player.dataString()); //Look how tiny I made all the networking stuff!!! #beproud handler.Receive(); } }
public void updateLocation(IAsyncResult ar) { ASCIIEncoding encoder = new ASCIIEncoding(); StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; byte[] buffer = encoder.GetBytes(player.dataString()); byte[] message = new byte[4096]; int bytesRead; //Sending client.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendStuff), client); //Listening state = new StateObject(); state.workSocket = client; try { client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(RecieveStuff), state); }catch (Exception e) { System.Diagnostics.Debug.WriteLine("ERROR: " + e.ToString()); } bytesRead = 0; /*if (true) { * talking = true; * while (true || encoder.GetString(message, 0, bytesRead).Split(deliminators).Length <= 4) { * bytesRead = 0; * try { * //System.Diagnostics.Debug.WriteLine("CLIENT: Attempting to find paper "); * bytesRead = clientStream.Read(message, 0, 4096); * //System.Diagnostics.Debug.WriteLine("CLIENT: Read the morning paper, page #" + bytesRead); * String rec = encoder.GetString(message, 0, bytesRead); * System.Diagnostics.Debug.WriteLine("CLIENT-RECIEVED-PRELIM: " + rec); * } catch (System.IndexOutOfRangeException exc) { * System.Diagnostics.Debug.WriteLine("CLIENT: Failed to recieve data: " + exc); * break; * } * if (bytesRead == 0) { * System.Diagnostics.Debug.WriteLine("CLIENT: Neighbour ditched us. "); * break; * } * } * }*/ if (bytesRead > 0) { String rec = encoder.GetString(message, 0, bytesRead); System.Diagnostics.Debug.WriteLine("CLIENT-RECIEVED: " + rec); } //writer.Flush(); }