теперь программа работает.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <curses.h>
#include <signal.h>
int socket_goodgame;
struct sockaddr_in parameter_of_server;
struct hostent *info_of_server;
char request [ 255 ];
char *status;
char *viewers;
char *users_in_chat;
char *games;
void configure_parameters_of_server ( )
{
parameter_of_server.sin_family = PF_INET;
memcpy ( ¶meter_of_server.sin_addr, info_of_server->h_addr_list[0], 4 );
// parameter_of_server.sin_addr.s_addr = *((unsigned long*) info_of_server->h_addr_list[0]);
parameter_of_server.sin_port = htons ( 80 );
}
void connect_to_goodgame ( )
{
info_of_server = gethostbyname ( "goodgame.ru" );
configure_parameters_of_server ( );
socket_goodgame = socket ( PF_INET, SOCK_STREAM, 0 );
int ret;
ret = connect (
socket_goodgame,
( struct sockaddr *) ¶meter_of_server,
sizeof ( struct sockaddr )
);
if ( ret == -1 ) {
perror ( "connect" );
}
}
void end_connection ( )
{
close ( socket_goodgame );
}
void build_subscribes ( )
{
memset ( request, 0, 255 );
sprintf ( request,
"GET http://goodgame.ru/api/getchannelstatus?id=xener HTTP/1.0\n"
"User-Agent: gd/0.1 (naidv88@gmail.com)\n"
"Accept: */*\n\n" );
}
char *find_argument ( char *value, char *buffer )
{
char *position_in_buffer = buffer;
for ( ; position_in_buffer != NULL; position_in_buffer++ ) {
if ( !strncmp ( value, position_in_buffer, strlen ( value ) ) ) {
position_in_buffer += strlen ( value ) + 1;
char *length_argument = position_in_buffer;
for ( int i = 0; ; i++ ) {
if ( *length_argument == '<' ) {
char *argument = calloc ( i + 1, 1 );
char *position_in_argument = argument;
for ( ; i > 0; i-- ) {
*position_in_argument = *position_in_buffer;
position_in_argument++;
position_in_buffer++;
}
return argument;
}
length_argument++;
}
}
}
return NULL;
}
int any_argument_is_null ( )
{
if ( games == NULL || status == NULL || viewers == NULL || users_in_chat == NULL ) {
if ( games ) free ( games );
if ( status ) free ( status );
if ( viewers ) free ( viewers );
if ( users_in_chat ) free ( users_in_chat );
return 1;
}
return 0;
}
void print_arguments ( )
{
clear ( );
mvprintw ( 0, 1, "gd v. 0.1" );
mvhline ( 1, 0, '_', 80 );
mvprintw ( 2, 1, "game: %s", games );
mvprintw ( 3, 1, "status: %s", status );
mvprintw ( 4, 1, "viewers: %s", viewers );
mvprintw ( 5, 1, "users: %s", users_in_chat );
mvaddch ( 6, 0, ' ' );
refresh ( );
free ( games );
free ( status );
free ( viewers );
free ( users_in_chat );
}
void get_arguments ( char *buffer )
{
status = find_argument ( "status", buffer );
viewers = find_argument ( "viewers", buffer );
users_in_chat = find_argument ( "usersinchat", buffer );
games = find_argument ( "games", buffer );
}
void parse_answer_from_server ( char *buffer )
{
get_arguments ( buffer );
if ( any_argument_is_null ( ) ) {
return;
}
print_arguments ( );
}
void get_information ( )
{
if ( send ( socket_goodgame, request, strlen ( request ), MSG_NOSIGNAL ) == -1 ) {
perror ( "send" );
}
char buf[4096];
int ret;
ret = read ( socket_goodgame, buf, 4096 );
if ( ret == 0 ) return;
parse_answer_from_server ( &buf[0] );
}
void curses_init ( )
{
initscr ( );
cbreak ( );
noecho ( );
keypad ( stdscr, FALSE );
}
void end_program ( int sig )
{
endwin ( );
exit ( EXIT_SUCCESS );
}
void signals_init ( )
{
signal ( SIGINT, end_program );
}
int main ( int argc, char *argv[] )
{
signals_init ( );
curses_init ( );
build_subscribes ( );
while ( 1 ) {
connect_to_goodgame ( );
get_information ( );
end_connection ( );
sleep ( 1 );
}
}