The UNIX and Linux Forums  
Ciao e benvenuto da al UNIX e Linux Forum! Grazie per la visita ed unirsi alla nostra Comunità Globale.

Go Back   UNIX e Linux Forum > Inizio Forum > Di programmazione ad alto livello
.
google unix.com



Di programmazione ad alto livello Pubblica domande su C, C + +, Java, SQL, e di altri linguaggi di programmazione qui.

Più di UNIX e Linux Forum Argomenti potreste trovare utili
Filo Thread Starter Forum Risposte Ultimo Post
non riesci ad accedere in gdm, aiuto urgente bisogno! pls wrapster Shell scripting e di programmazione 0 04-25-2008 01:48 AM
urgente bisogno manish1 Shell scripting e di programmazione 0 03-13-2008 05:09 AM
Urgent help needed skyineyes UNIX for Dummies Domande & Risposte 5 06-19-2007 04:31 AM
aiuto urgente bisogno. Lotto Shell scripting e di programmazione 4 12-08-2006 11:31 AM
aiuto urgente bisogno! guhas Shell scripting e di programmazione 0 09-10-2005 02:01 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Cerca in questo Thread Rate Thread Modalità di visualizzazione
  #1 (permalink)  
Old 05-19-2005
mgchato's Avatar
mgchato mgchato is offline
Utente Registrato
  
 

Iscriviti Data: maggio 2005
Interventi: 7
Code di messaggi e il coordinamento dei programmi di nuovo ...

Ciao a tutti!

Ho supposto di progettare ed attuare una serie di programmi che permettono agli utenti connessi a un host per inviare messaggi a vicenda.

Ho già iniziato con un programma di messaggistica che dovrebbe essere fatta di un server e un client (Grazie molto per il vostro aiuto Blowtorch). Ho dovuto aggiungere a questo programma di coordinamento del programma. Il coordinatore dovrebbe accettare i messaggi provenienti da clienti e li trasmette in allegato a tutti i clienti. Sono anche previsti per aggiungere un comando per il client che consente di accedere a un file di testo per tutti i messaggi ricevuti.

Penso che un buon modo per gestire il suo potrebbe essere quella di integrare la coda di messaggi in un modulo che i clienti e coordinatore può chiamare, ma la cosa è che non ho la benché minima idea da dove cominciare! Do u guys know qualsiasi libri o siti dove posso trovare il codice di esempio e spiegazioni? Credo che l'unico modo per me di imparare è attraverso esempi ...

PD Quante. C file che ho bisogno?

Grazie molto per il vostro aiuto!

Oh, e qui sono i miei programmi di messaggistica:

Codice:
/*q.h*/
#ifndef Q_H
	#define Q_H
	#define QKEY ftok("q.h",1)
	#define MSGSZ 128

	#ifdef DEBUG
		#define DEBUG_PRINT(...) fprintf(stderr,__VA_ARGS__)
		/* this __VA_ARGS__ thing allows a macro to
		take a varialble number of arguments */
	#else
		#define DEBUG_PRINT(...) /* __VA_ARGS__ */
	#endif

	int init_queue();
	int exchange_pids();
	void remove_queue();
	int send_mesg(char*,int);
	int send_mesg_signal(char*,int);
	int recv_mesg(char*,int);
#endif



/*signalmsg.c*/
#include<signal.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include"q.c"
#include"q.h"

int cnt;
void hndlr(int);

main()
{
        struct sigaction buf;
        char msg[MSGSZ];
        int stop=0,n;
        sigfillset(&buf.sa_mask);
        buf.sa_flags=0;
        buf.sa_handler=hndlr;
        sigaction(SIGUSR1,&buf,NULL);
        sigaction(SIGINT,&buf,NULL);
        init_queue();
        exchange_pids();
        
while(!stop)
        {
                printf("\nEnter message: ");
                while (-1 == (n=read(0,msg,MSGSZ-1) ))
                {
                        if (errno != EINTR )
                        {
                           perror("read");
                           send_mesg("bye\n",0);
                           exit(1);
                        }
                        for(;cnt>0;cnt--)
                        {
                           recv_mesg(msg,0);
                           printf("\nReceived Message: %s\n", msg);
                           if (0==strcmp(msg,"bye\n"))
                           {
                                remove_queue();
                                exit(0);
                           }
                        }
                        printf("\nEnter message: ");
                }
                msg[n]=0;
                send_mesg_signal(msg,0);
                if (0==strcmp(msg,"bye\n"))
                        stop=1;
        }
        return 0;
}

void hndlr(int sig)
{
        DEBUG_PRINT("Caught signal: %d\n",sig);
        switch(sig)
        {
        case SIGUSR1:   DEBUG_PRINT("USR1 branch\n");
                        cnt++;
                        break;
        case SIGINT:    DEBUG_PRINT("SIGINT branch\n");
                        send_mesg_signal("bye\n",0);
                        exit(0);
                        break;
        default:        fprintf(stderr,"Yikes how did I get here?!!!\n");
                        exit(1);
        }

}



/*q.c*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<unistd.h>
#include<signal.h>
#include<errno.h>
#include"q.h"

struct mymsg
{
        long mtype;
        char mtext[MSGSZ];
};

static int qid;
static int channel_no;
static pid_t remote_pid;

void remove_queue()
{
        msgctl(qid,IPC_RMID,0);
}

int init_queue()
{
        char  msg[MSGSZ];
        int rv;
        if( -1 != ( qid = msgget(QKEY,IPC_CREAT|IPC_EXCL|0600) ))
        {
                /* am entity A using channel 1 */
                channel_no=1;
                DEBUG_PRINT("Am entity A\n");
                return 0;
        }
        if ( errno != EEXIST )
        {
                perror("MSGGET:");
                return -1;
        }
        if (-1 != ( qid = msgget(QKEY,IPC_CREAT|0600) ))
        {
                /* am entity B using channel 2 */
                 channel_no=2;
                DEBUG_PRINT("Am entity B\n");
                return 0;
        }

        perror("MSGGET:");
        return -1;
}

int exchange_pids()
{
        char msg[MSGSZ];
        if(channel_no==1)
        {
                /* sending PID to other end */
                sprintf(msg,"%d",getpid());
                send_mesg(msg,0);
                DEBUG_PRINT("My pid is %s\n",msg);
                /* grab pid from other end */
                recv_mesg(msg,0);
                sscanf(msg,"%d",&remote_pid); 
                DEBUG_PRINT("Received PID is %s\n",msg);
        }
        else
        {
                /* grab pid from other end */
                recv_mesg(msg,0);
                sscanf(msg,"%d",&remote_pid);
                DEBUG_PRINT("Received PID is %s\n",msg);
                
                /* sending PID to other end */
                sprintf(msg,"%d",getpid());
                send_mesg(msg,0);
                DEBUG_PRINT("My pid is %s\n",msg);
        }
        return remote_pid;
}

int recv_mesg(char *msg, int nb)
{
        struct mymsg m;
        int options=0,len;
        int recv=((channel_no==1)?2:1);
        DEBUG_PRINT("Receiving on %d\n",recv);
        if (nb) options|=IPC_NOWAIT;
        if( -1==(len=msgrcv(qid,&m,MSGSZ,recv,options)))
                return -1;
        strcpy(msg,m.mtext);
        return len;
}

int send_mesg(char* msg,int nb)
{
        struct mymsg m;
        int options,len;
        m.mtype=channel_no;
        strcpy(m.mtext,msg);
        len=strlen(msg)+1;
        options=0;
        if (nb) options|=IPC_NOWAIT;
        DEBUG_PRINT("Sending type %d\n",m.mtype);
        return msgsnd(qid,&m,len,options);
}

int send_mesg_signal(char*msg,int nb)
{
        int rv;

        rv=send_mesg(msg,nb);
        kill(remote_pid,SIGUSR1);
        return rv;
}

Ultimo a cura di mgchato; al 05/19/2005 04:34 AM..
Closed Thread

Segnalibri

Thread Tools Cerca in questo Thread
Cerca in questo Thread:

Ricerca Avanzata
Modalità di visualizzazione Vota questo thread
Vota questo thread:

Distacco regolamento
Tu non può post nuovo thread
Tu non può inviare una risposta
Tu non può postare allegati
Tu non può modificare i tuoi post

BB codice è Su
Smilies sono Su
[IMG] codice Su
Codice HTML è Chiuso
Trackbacks sono Su
Pingbacks sono Su
Refbacks sono Su




Tutti gli orari sono GMT -4. La data di oggi è 12:25 AM.


Powered by: vBulletin, Copyright © 2000 - 2006, Jelsoft Enterprises Limited. Traduzioni Powered by .
vBCredits v1.4 Copyright © 2007 - 2008, PixelFX Studios
UNIX e Linux Forum Content Copyright © 1993-2009. Tutti i diritti Reserved.Ad di gestione da RedTyger

Contenuti pertinenti URL da vBSEO 3.2.0