Code:
#include <stdlib.h>
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#define IPADDR "192.168.1.45"
#define PORTNUMBER 80
#define URLPATH "/mjpg/video.mjpg"
#define USERPASS "user:passwort"
#define REQUEST_AUTH "GET %s HTTP/1.0\r\nAuthorization: Basic %s\r\n\r\n"
#define REQUEST "GET %s HTTP/1.0\r\n\r\n"
void base64_encode(const char *s, char *store, int length) {
static char tbl[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
int i;
unsigned char *p = (unsigned char *) store;
for(i=0;i<length;i += 3) {
*p++ = tbl[s[0]>>2];
*p++ = tbl[((s[0] & 3) << 4) + (s[1] >>4)];
*p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
*p++ = tbl[s[2] & 0x3f];
s += 3;
}
if(i == length +1)
{ *(p-1) = '=';}
else
{ *(p-1) = *(p-2) = '='; }
*p = '\0';
}
int main()
{
SOCKET sSocket;
SOCKADDR_IN sAddr;
int i;
int result;
char *dummy;
char buffer[65536];
char *request;
if(strlen(USERPASS)){
char *userpass = (char *)malloc(strlen(USERPASS) + 4);
char *userp = (char *)malloc(strlen(USERPASS)+4);
memset(userpass,0,strlen(USERPASS)+4);
memset(userp,0,strlen(USERPASS)+4);
strcpy(userpass,USERPASS);
strcpy(userp,USERPASS);
base64_encode(userpass,userp,strlen(USERPASS));
request = (char *)malloc(strlen(REQUEST_AUTH) + strlen(URLPATH) + strlen(userp) +1);
sprintf(request, REQUEST_AUTH, URLPATH, userp);
free(userpass);
free(userp);
printf("%s",request);
}
else {
request = (char *) malloc(strlen(REQUEST) + strlen(URLPATH) + 1);
sprintf(request, REQUEST);
}
/*Netzwerk initialisieren*/
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wdData;
WSAStartup(wVersionRequested, &wdData);
sSocket = socket(AF_INET, SOCK_STREAM, 0);
if(sSocket == INVALID_SOCKET)
{
MessageBox(0,"Konicht initialisieren", "Fehler", MB_ICONEXCLAMATION == IDOK);
return 256;
}
/*Socket eigenschaften festlegen */
sAddr.sin_family = PF_INET;
sAddr.sin_addr.s_addr = inet_addr( IPADDR );
sAddr.sin_port = htons( PORTNUMBER );
if(connect(sSocket, (SOCKADDR*) &sAddr, sizeof(sAddr)) == SOCKET_ERROR) {
MessageBox(0,"Konnte nicht verbinden", "Fehler", MB_ICONEXCLAMATION == IDOK);
return 255;
}
send(sSocket, request, strlen(request),0);
free(request);
while(true){
dummy = buffer;
int n = recv(sSocket,(char *) &buffer, sizeof(buffer),0);
if (n == -1){
MessageBox(0,"Stream nicht empfangen", "Fehler", MB_ICONEXCLAMATION == IDOK);
return 255;
}
if(n == 0)
{
MessageBox(0,"Hö??", "Fehler", MB_ICONEXCLAMATION == IDOK);
break;}
MessageBox(0,(const char*)&buffer, "Fehler", MB_ICONEXCLAMATION == IDOK);
free(&buffer);
i += n;
}
return 1;
}
den Bilderstream gebe ich zur Zeit als Text aus, so konnte ich immerhin sehen, dass ich was empfangen habe. Ich hoffe jemand kann helfen.
Lesezeichen