private void button1_Click(object sender, EventArgs e) { this.clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8000); this.clientSocket.BeginConnect(remoteEP, new AsyncCallback(this.OnConnect), null); String server; String message = "<Msg Terminal=\"karaoq\"/>"; // Create a TcpClient. // Note, for this client to work you need to have a TcpServer // connected to the same address as specified by the server, port // combination. Int32 port = 13000; TcpClient client = new TcpClient("127.0.0.1", port); // Translate the passed message into ASCII and store it as a Byte array. Byte[] data = new Byte[1024]; // Get a client stream for reading and writing. // Stream stream = client.GetStream(); NetworkStream stream = client.GetStream(); // String to store the response ASCII representation. String responseData = String.Empty; Int32 bytes; // Read the first batch of the TcpServer response bytes. while (stream.DataAvailable) { bytes = stream.Read(data, 0, data.Length); responseData += System.Text.Encoding.Unicode.GetString(data, 0, bytes); } Console.WriteLine("Received: {0}", responseData); string[] result; result = responseData.Split(new string[] { "<Msg" }, StringSplitOptions.None); data = new Byte[1024]; if (responseData.Contains("<Msg Command=\"GetTerminalID")) { data = System.Text.Encoding.Unicode.GetBytes(message); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); } data = new Byte[1024]; while (stream.DataAvailable) { bytes = stream.Read(data, 0, data.Length); responseData += System.Text.Encoding.Unicode.GetString(data, 0, bytes); } if (responseData.Contains("<Msg Command=\"GetClientVersion")) { message = "<Msg Terminal=\"" + Application.ProductVersion + "\"/>"; data = new Byte[1024]; data = System.Text.Encoding.Unicode.GetBytes(message); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); } data = new Byte[1024]; while (stream.DataAvailable) { bytes = stream.Read(data, 0, data.Length); responseData += System.Text.Encoding.Unicode.GetString(data, 0, bytes); } DataTable SongList = new DataTable(); KaraoqDataAccess.KaraoqWebServiceSoapClient objClient = new KaraoqDataAccess.KaraoqWebServiceSoapClient(); SongList = objClient.GetSongListForDJ(General.DJId, 279); for (int i = 0; i < SongList.Rows.Count; i++) { message = "<RequestSong Singer=\"" + SongList.Rows[i]["performer_name"] + "\" Title=\"" + SongList.Rows[i]["song_name"] + "\" Artist=\"" + SongList.Rows[i]["artist_name"] + "\" ID= \"C03268C\" Index=\"432786\" FileName=\"Filename And Path\"/>"; data = new Byte[1024]; data = System.Text.Encoding.Unicode.GetBytes(message); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); data = new Byte[1024]; while (stream.DataAvailable) { bytes = stream.Read(data, 0, data.Length); responseData += System.Text.Encoding.Unicode.GetString(data, 0, bytes); } } // Receive the TcpServer.response. // Close everything. stream.Close(); client.Close(); }
/// <summary> /// Event is invoked when Do work /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bwVenueSync_DoWork(object sender, DoWorkEventArgs e) { CurrentSteps = "Step 1/3 (Importing KaraokeTracks)"; //bwVenueSync.ReportProgress(0); KaraoqDataAccess.KaraoqWebServiceSoapClient objClient = new KaraoqDataAccess.KaraoqWebServiceSoapClient(); int lastTrackId = objClient.GetLastSynqTrackId(General.DJId, VenueId, Convert.ToInt32(Properties.Settings.Default.DJPlaybackTool)); int lastTrackHistoryId = objClient.GetLastSynqHistoryTrackId(General.DJId, VenueId, Convert.ToInt32(Properties.Settings.Default.DJPlaybackTool)); OleDbConnection connectionString = new OleDbConnection(accessDatabaseConnectionString); connectionString.Open(); try { DataTable dtKaraokeTracks = new DataTable(); dtKaraokeTracks.TableName = "KaraokeTracks"; try { OleDbCommand oleDbCommandKaraokeTracks = new OleDbCommand("SELECT * FROM KaraokeTracks order by KaraokeTrackID ASC", connectionString); if (lastTrackId > 0) { oleDbCommandKaraokeTracks = new OleDbCommand("SELECT * FROM KaraokeTracks where KaraokeTrackID > " + lastTrackId + " order by KaraokeTrackID ASC ", connectionString); } OleDbDataAdapter adapterKaraokeTracks = new OleDbDataAdapter(oleDbCommandKaraokeTracks); adapterKaraokeTracks.Fill(dtKaraokeTracks); } catch (Exception ex) { if (ex.Message == "The Microsoft Jet database engine cannot find the input table or query 'KaraokeTracks'. Make sure it exists and that its name is spelled correctly.") { OleDbCommand oleDbCommandKaraokeTracks = new OleDbCommand("SELECT * FROM tblKaraokeTracks order by KaraokeTrackID ASC", connectionString); if (lastTrackId > 0) { oleDbCommandKaraokeTracks = new OleDbCommand("SELECT * FROM tblKaraokeTracks where KaraokeTrackID > " + lastTrackId + " order by KaraokeTrackID ASC ", connectionString); } OleDbDataAdapter adapterKaraokeTracks = new OleDbDataAdapter(oleDbCommandKaraokeTracks); adapterKaraokeTracks.Fill(dtKaraokeTracks); } } CurrentProgressbarTotal = dtKaraokeTracks.Rows.Count; CurrentSteps = "Step 1/3 (Importing Karaoke Tracks)"; if (dtKaraokeTracks.Rows.Count > 0) { for (int i = 0; i < dtKaraokeTracks.Rows.Count; i++) { try { bwVenueSync.ReportProgress(i + 1); int trackNumber = 0; int.TryParse(Convert.ToString(dtKaraokeTracks.Rows[i]["TrackNo"]), out trackNumber); objClient.InsertSynqTracks(VenueId, General.DJId, Convert.ToInt32(Properties.Settings.Default.DJPlaybackTool), Convert.ToInt32(dtKaraokeTracks.Rows[i]["KaraokeTrackID"]), Convert.ToString(dtKaraokeTracks.Rows[i]["ArtistName"]), Convert.ToString(dtKaraokeTracks.Rows[i]["TrackName"]), Convert.ToString(dtKaraokeTracks.Rows[i]["DiscName"]), trackNumber.ToString(), Convert.ToString(dtKaraokeTracks.Rows[i]["Filename"]), Convert.ToString(dtKaraokeTracks.Rows[i]["TrackPath"]), "", "", "" ); } catch { continue; } } } CurrentSteps = "Step 2/3 (Importing Karaoke Tracks History)"; bwVenueSync.ReportProgress(0); DataTable dttblKaraokeTrackHistory = new DataTable(); dttblKaraokeTrackHistory.TableName = "tblKaraokeTrackHistory"; OleDbCommand oleDbCommandtblKaraokeTrackHistory = new OleDbCommand("SELECT * FROM tblKaraokeTrackHistory order by KaraokeTrackHistoryID ASC", connectionString); if (lastTrackHistoryId > 0) { oleDbCommandtblKaraokeTrackHistory = new OleDbCommand("SELECT * FROM tblKaraokeTrackHistory where KaraokeTrackHistoryID > " + lastTrackHistoryId + " order by KaraokeTrackHistoryID ASC", connectionString); } OleDbDataAdapter adaptertblKaraokeTrackHistory = new OleDbDataAdapter(oleDbCommandtblKaraokeTrackHistory); adaptertblKaraokeTrackHistory.Fill(dttblKaraokeTrackHistory); CurrentProgressbarTotal = dttblKaraokeTrackHistory.Rows.Count; if (dttblKaraokeTrackHistory.Rows.Count > 0) { for (int i = 0; i < dttblKaraokeTrackHistory.Rows.Count; i++) { try { bwVenueSync.ReportProgress(i + 1); objClient.InsertSynqTracksHistory(VenueId, General.DJId, Convert.ToInt32(Properties.Settings.Default.DJPlaybackTool), Convert.ToInt32(dttblKaraokeTrackHistory.Rows[i]["KaraokeTrackHistoryID"]), Convert.ToString(dttblKaraokeTrackHistory.Rows[i]["SingerName"]), Convert.ToString(dttblKaraokeTrackHistory.Rows[i]["Artist"]), Convert.ToString(dttblKaraokeTrackHistory.Rows[i]["Title"]), Convert.ToString(dttblKaraokeTrackHistory.Rows[i]["Key"]), Convert.ToString(dttblKaraokeTrackHistory.Rows[i]["TrackPath"]), Convert.ToDateTime(dttblKaraokeTrackHistory.Rows[i]["Date"]) ); } catch { continue; } } } } catch { } finally { if (connectionString != null && connectionString.State == ConnectionState.Open) { connectionString.Close(); } } SongList = new DataTable(); SongList = objClient.GetSongListForDJ(General.DJId, VenueId); }