public void write_subj_to_file(Subj S) { Console.WriteLine("WRITING GAZE PT DATA TO FILE"); String out_path = Path.GetFullPath(System.Environment.GetCommandLineArgs()[1]); String filename = out_path + "\\OUT_S" + Convert.ToString(S.get_ID()) + ".txt"; StreamWriter sW = new StreamWriter(filename); ArrayList gaze_pts = S.get_gaze_pts(); for(int i=0; i<gaze_pts.Count; i++) { Point_Obj g = (Point_Obj)gaze_pts[i]; sW.WriteLine("({0}, {1})", g.get_x(), g.get_y()); } sW.Flush(); sW.Close(); Console.WriteLine("DONE WRITING"); }
private void bg_worker1_DoWork(object sender, DoWorkEventArgs e) { //Int32 port = 13000; Int32 port = 1223; //IPAddress localAddr = IPAddress.Parse("130.126.122.122"); //IPAddress localAddr = IPAddress.Any; TcpListener server = null; //server = new TcpListener(localAddr, port); server = new TcpListener(port); // Start listening for client requests. server.Start(); // Buffer for reading data int num_floats = 512; // number of floating point numbers in each packet int packet_length = num_floats * sizeof(float); // (in bytes) Byte[] bytes = new Byte[packet_length]; TcpClient client = null; NetworkStream stream = null; write_to_msg_box("Waiting for connection: \n"); Subj S = new Subj(); while (true) { if (bg_worker1.CancellationPending == true) { server.Stop(); Thread.Sleep(500); break; } else { try { //write_to_msg_box("In BG_Do_WORK\n"); client = server.AcceptTcpClient(); write_to_msg_box("Client Connected!!\n"); stream = client.GetStream(); S = new Subj(client, 0, all_subj.Count); int i; while ((i = stream.Read(bytes, 0, packet_length)) != 0) { // Convert byte array to float float[] float_array = new float[num_floats]; for (int ii = 0; ii < float_array.Length; ii++) { float_array[ii] = BitConverter.ToSingle(bytes, ii * sizeof(float)); if (ii < 11) Console.Write(float_array[ii] + " "); } Console.Write("\n\n"); // Process float array and get estimated gaze point Point_Obj est_gaze_pt = process_float_data(float_array); // Log estimated gaze point S.get_gaze_pts().Add(est_gaze_pt); // Check if subject is distracted S = check_if_distract(S, est_gaze_pt); Console.WriteLine("NUM DISTRACT FRAMES: {0}", S.get_num_frames_distracted()); if (S.get_num_frames_distracted() > DISTRACT_WINDOW) { write_to_alert_box(1); } else { write_to_alert_box(0); } } all_subj.Add(S); stream.Close(); client.Close(); write_to_msg_box("Connection Closed"); } catch (Exception EE) { Console.WriteLine("EXCEPTION: {0}", EE.Message); all_subj.Add(S); stream.Close(); client.Close(); write_to_msg_box("Connection Closed"); } // Write Subj. to a file File_IO F = new File_IO(); F.write_subj_to_file(S); } } }