/// <summary> /// 连接音频流,并开启监听逻辑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void getButton_Click(object sender, EventArgs e) { try { Mic.settings.sourcename = ddlDevice.SelectedItem.ToString(); AudioOutDevice = ddlDevice.SelectedItem.ToString(); MicVolumeLevel = new SendVolumeLevel(Mic, 0, this); MicVolumeLevel.AudioMode = 0; this.panel1.Controls.Add(MicVolumeLevel); MicVolumeLevel.Dock = DockStyle.Fill; MicVolumeLevel.Enable(); this.muteButton.Enabled = true; this.trackBar1.Enabled = true; } catch (System.Exception ex) { MessageBox.Show(ex.ToString()); } }
//This method Accepts new connection and //First it receives the welcome massage from the client, //Then it sends the Current date time to the Client. public void StartListen() { while (Running && numErr < 5) { //Accept a new connection try { Socket mySocket = myListener.AcceptSocket(); if (MySockets.Count() < _socketindex + 1) { MySockets.Add(mySocket); } else { MySockets[_socketindex] = mySocket; } if (mySocket.Connected) { mySocket.NoDelay = true; mySocket.ReceiveBufferSize = 8192; mySocket.ReceiveTimeout = 1500; try { //make a byte array and receive data from the client string sBuffer; string sHttpVersion; Byte[] bReceive = new Byte[1024]; mySocket.Receive(bReceive); sBuffer = Encoding.ASCII.GetString(bReceive); if (sBuffer.Substring(0, 4) == "TALK") { var socket = mySocket; var feed = new Thread(p => AudioIn(socket)); _socketindex++; feed.Start(); continue; } if (sBuffer.Substring(0, 3) != "GET") { continue; } int iStartPos = sBuffer.IndexOf("HTTP", 1, StringComparison.Ordinal); sHttpVersion = sBuffer.Substring(iStartPos, 8); int cid = -1, vid = -1, camid = -1; int w = -1, h = -1; string qs = sBuffer.Substring(4); qs = qs.Substring(0, qs.IndexOf(" ", StringComparison.Ordinal)).Trim('/').Trim('?'); string[] nvs = qs.Split('&'); foreach (string s in nvs) { string[] nv = s.Split('='); switch (nv[0].ToLower()) { case "c": cid = Convert.ToInt32(nv[1]); break; case "w": w = Convert.ToInt32(nv[1]); break; case "h": h = Convert.ToInt32(nv[1]); break; case "camid": camid = Convert.ToInt32(nv[1]); //mjpeg break; case "micid": vid = Convert.ToInt32(nv[1]); break; } } if (vid != -1) { SendVolumeLevel vl = Parent.MicVolumeLevel; if (vl != null) { String sResponse = ""; sResponse += "HTTP/1.1 200 OK\r\n"; sResponse += "Server: iSpy\r\n"; sResponse += "Expires: 0\r\n"; sResponse += "Pragma: no-cache\r\n"; sResponse += "Content-Type: multipart/x-mixed-replace;boundary=--myboundary"; sResponse += "\r\n\r\n"; Byte[] bSendData = Encoding.ASCII.GetBytes(sResponse); SendToBrowser(bSendData, mySocket); vl.OutSockets.Add(mySocket); _socketindex++; continue; } } else { const string resp = "iSpy server is running"; SendHeader(sHttpVersion, "", resp.Length, " 200 OK", 0, ref mySocket); SendToBrowser(resp, ref mySocket); } numErr = 0; } catch (SocketException ex) { System.Diagnostics.Debug.WriteLine("Server Error (socket): " + ex.Message); //MainForm.LogExceptionToFile(ex); numErr++; } mySocket.Close(); mySocket = null; } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Server Error (generic): " + ex.Message); //MainForm.LogExceptionToFile(ex); numErr++; } } }