private static void savefile(sPAM_S pParam, Socket Socket1, IPEndPoint ip) { string fileName = pParam.getpath() + pParam.getfilename(); while (true) { FileStream filewrite; create : try { filewrite = new FileStream(fileName, FileMode.Append); } catch (Exception) { Directory.CreateDirectory(pParam.getpath()); goto create; Console.WriteLine("3"); } BinaryWriter sw = new BinaryWriter(filewrite);//二进制流写入方式非常重要 byte[] buf = ReceiveData(Socket1, 20); byte[] payloadLen_b = { 0, 0, 0, 0 }; payloadLen_b[3] = buf[4]; payloadLen_b[2] = buf[5]; payloadLen_b[1] = buf[6]; payloadLen_b[0] = buf[7]; int payloadLen = BitConverter.ToInt32(payloadLen_b, 0); if (payloadLen < 12) { throw new Exception("my exception"); } // Console.WriteLine("payload_Len: " + (payloadLen - 12)); //byte[] time_samp = { 5, 6, 7, 8 }; //time_samp[3] = buf[12]; //time_samp[2] = buf[13]; //time_samp[1] = buf[14]; //time_samp[0] = buf[15]; //int timesamp = BitConverter.ToInt32(time_samp, 0); //Console.WriteLine("timesamp: " + timesamp); byte[] video1 = ReceiveData(Socket1, payloadLen - 12); try { sw.Write(video1); } catch (System.IO.IOException) { { throw new Exception("my exception"); } } finally { sw.Close(); } } }
private static int responsecmd(sPAM_S pParam, Socket Socket1, IPEndPoint ip) { while (true) { byte[] buf = new byte[256]; try { int a = Socket1.Receive(buf); } catch (SocketException e3) { throw new Exception("my exception"); } string bufstr = Encoding.ASCII.GetString(buf); if (bufstr.Contains("200 OK")) { Console.WriteLine("校验 OK"); if (bufstr.Contains("m=video")) { int n1 = bufstr.IndexOf("m=video"); string videoformat = bufstr.Substring(n1 + 22, 8); Console.WriteLine("wight/high: " + videoformat); if (bufstr.Contains("m=audio")) { int n2 = bufstr.IndexOf("m=audio"); string audioformat = bufstr.Substring(n2 + 10, 5); Console.WriteLine("audio: " + audioformat); if (bufstr.Contains("Transport")) { int n3 = bufstr.IndexOf("Transport"); string Transport = bufstr.Substring(n3 + 10, 55); Console.WriteLine("Transport: " + Transport); return(0); } } } return(1); } } }
private static int reqsend(sPAM_S pParam, Socket Socket1, IPEndPoint ip) { string content = "Cseq: 1\r\nTransport: RTP/AVP/TCP;unicast;interleaved=0-1\r\n"; string outBufferStr = "GET http://" + pParam.getIp() + ":" + pParam.getPort() + "/livestream/12?action=play&media=video HTTP/1.1\r\n" + "User-Agent: HiIpcam/V100R003 VodClient/1.0.0\r\n" + "Connection: Keep-Alive\r\n" + "Cache-Control: no-cache\r\n" + "Authorization: " + pParam.getuname() + " " + pParam.getPword() + "\r\n" + "Content-Length: " + content.Length + "\r\n"//??? + "\r\n" + content; if (IsConnected(Socket1, ip)) { Byte[] outBuffer = Encoding.ASCII.GetBytes(outBufferStr); while (true) { int ret; try { ret = Socket1.Send(outBuffer, outBuffer.Length, 0); } catch (SocketException e) { throw new Exception("my exception"); } if (ret < 0) { Console.WriteLine("send cmd err"); return(-1); } else { return(0); break; } } } return(0); }
private static void videobegin(sPAM_S pParam, Socket Socket1, IPEndPoint ip) { byte[] buf = new byte[1]; byte[] buf2 = new byte[4]; int i = 1; while (true) { int ret; try { ret = Socket1.Receive(buf, 1, 0); i = i + ret; } catch (SocketException e4) { throw new Exception("my exception"); } if (ret < 0) { Console.WriteLine("send cmd err"); break; } else { buf2[0] = buf2[1]; buf2[1] = buf2[2]; buf2[2] = buf2[3]; buf2[3] = buf[0]; string bufstr = Encoding.ASCII.GetString(buf2); if (bufstr == "\r\n\r\n") { Console.WriteLine("video begin"); break; } else if (i > 255) { break; } } } }
public void con() { sPAM_S PAM_S = new sPAM_S(); string uname = this.name; string pwd = this.password; string host = this.camhost; int port = this.camport; string path = this.videopath; string filename = this.videoname; PAM_S.Setaddr(uname, pwd, host, port, path, filename); IPEndPoint ip = new IPEndPoint(IPAddress.Parse(host), port); Console.WriteLine("conect begin"); Console.WriteLine("ip " + host); Console.WriteLine("port " + port); Console.WriteLine("user " + uname); Console.WriteLine("pwd " + pwd); Socket camSocket; int i = 0; con: camSocket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { reqsend(PAM_S, camSocket, ip); } catch (Exception ex) { i++; Console.WriteLine("restar\r\n" + this.videopath + this.videoname + "第" + i + "次尝试1"); Thread.Sleep(5000); if (i < 20) { goto con; } } Console.WriteLine("sendover"); try { responsecmd(PAM_S, camSocket, ip); } catch (Exception ex) { i++; Console.WriteLine("restar\r\n" + this.videopath + this.videoname + "第" + i + "次尝试2"); Thread.Sleep(5000); if (i < 20) { goto con; } } try { videobegin(PAM_S, camSocket, ip); } catch (Exception ex) { i++; Console.WriteLine("restar\r\n" + this.videopath + this.videoname + "第" + i + "次尝试3"); Thread.Sleep(5000); if (i < 20) { goto con; } } try { savefile(PAM_S, camSocket, ip); } catch (Exception ex) { i++; Console.WriteLine("restar\r\n" + this.videopath + this.videoname + "第" + i + "次尝试4"); Thread.Sleep(5000); if (i < 20) { goto con; } } }