🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #7918

Re: UDP socket - simple question

#7918
From: Mikel Irazabal
Date: Mon, 20 Jan 2003 09:02
80 lines
3193 bytes
MSF wrote:
> Hi
> 
> I've just started programing, using C language and UDP Sockets (in Linux);
> (so I have a few problems)
> 
> 
> I have two programs (client and server);
>     -Client send (interactive) request information about server network
> configuration (/sbin/ifconfig)
> 
>         -Server
>                 if(!strcasecmp(dgram,"interface")){
>           should do this request
>                 system("/sbin/ifconfig")
>            and send it to client
>                 ??????? HOW ??????
> 
> Simple use
>             variable1 = system("/sbin/ifconfig")
>             sprintf("%s",variable1)
>  doesn't work (problem with buffer, even I set it bigger),
>   if I change sprintf into printf - information about network configuration
>     will be appear on server screen - of course,
> 
>     but how send it to Client?
> (maybe first write to file, and then to buffer - gradually, after this send
> it to Client
>  - if it's good idea - how write it?

First of all, this is not a good place to post questions in English...

Well, after that... no estas usando bien la funci�n "system", su 
declaraci�n es:

int system(const char *string);

�sta funci�n ejecuta un int�rprete de comandos, generalmente /bin/sh y 
le pasa el comando entregado en la cadena "string". Una vez ejecutado, 
devuelve un "int" con el estado de finalizaci�n de la ejecuci�n (NO UN 
PUNTERO A CADENA).

Todo lo que el comando "system" saca por la salida est�ndar, saldr� por 
la salida est�ndar, y no ser� puesto en ninguna cadena.

Posible soluci�n:
lo que dices de escribir al fichero puede funcionar:

1. Primero escribe al fichero
2. Luego, lee y manda

Algo as�:

-----8<----------------------

FILE *file;
char buffer[512];
int i;

(...)

system("/sbin/ifconfig > /tmp/ifconfig.txt");

file = fopen("/tmp/ifconfig.txt", "r");


for (i=0; fread(buffer, sizeof(char), 512, file)==512 ; i+=512)
{
    sendto(socket, buffer, 512*sizeof(char), 0,
	&Client_address, sizeof(struct sockaddr));
}

close(socket);
fclose(file);

(...)

-------8<-------------------

Mejor si le a�ades tratamiento de errores y esas cosas...
Technical Details
Message-ID: <3E2BBB2B.2070807@yahoo.es>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!fu-berlin.de!uni-berlin.de!eu100-132.clientes.euskaltel.ES!not-for-mail
References: <b0f39s$1g8p$1@foka.acn.pl>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f86c8,3672983a83160639
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2003-01-20 01:01:38 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!fu-berlin.de!uni-berlin.de!eu100-132.clientes.euskaltel.ES!not-for-mail
From: Mikel Irazabal <m_irazabal@yahoo.es>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: UDP socket - simple question
Date: Mon, 20 Jan 2003 10:02:35 +0100
Lines: 82
Message-ID: <3E2BBB2B.2070807@yahoo.es>
References: <b0f39s$1g8p$1@foka.acn.pl>
NNTP-Posting-Host: eu100-132.clientes.euskaltel.es (212.8.100.132)
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fu-berlin.de 1043053296 26360382 212.8.100.132 (16 [172074])
User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.0.1) Gecko/20020913
X-Accept-Language: eu, es, en, fr, de