Unix Programming - Problem in a client/server program

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2006 > Problem in a client/server program





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Problem in a client/server program
ziche77@infinito.it

2006-06-26, 7:34 am

I'm new in this kid of application and this is my first network
program...
I would like to enstablish a connection betwen a server and a client in
local.
The problem is on the function bind. When I lauch the server or the
client the error is alway the same: "Address already in use".
I don't know why and I don't know what I can do to revolve it, can
anybody help me, please?
Thank you.

The scripts are the following:

// SERVER

#define PATH "/home/rosenbergt/Daniele/Progetto_Metro/socket"


//======================================
void vett_caso(int nfvett[DIM_VETT]);
//======================================


/* Programma Server -- Inizio*/

int main(int argc, char * argv[])
{
int s = 0;
int i = 0;
int on = 1;
int legame = 0;
int rlen = 0;
int slen = 0;
int size_from = 0;
int vc[DIM_VETT];
char sdata[5000]; // Pacchetto di informazioni spedito
char rdata[5000]; // Pacchetto di informazioni ricevuto
char *p;
struct sockaddr_un servaddr; // indirizzo del server
struct sockaddr_un from;

// creazione socket

s = socket(AF_UNIX,SOCK_STREAM,0);
if(s<0)
{
perror("Errore sulla creazione del socket lato SERVER");
exit (1);
}
else
printf("Il numero identificativo del bind vale: %d\n", legame);

// Passaggio vettore sul vettore dati trasmesso sdata - Inizio
vett_caso(vc);
for(i=0; i<DIM_VETT; i++)
{
sprintf(sdata,"%d ", vc[i]);
}
for(i=0; i<DIM_VETT; i++)
{
p = strchr(sdata, ' ');
if(!p)
{
printf("Errore sforato il puntatore");
}
p = p + 1;
if(i == (DIM_VETT-1))
{
*p = '\0';
}
}
// Passaggio vettore sul vettore dati trasmesso sdata - Fine

// Ricezione pacchetti
// Invio pacchetti

size_from = sizeof(from);
for(;;)
{
printf("...sto aspettando info dal client...\n");
// Ricezione a dire il vero in questo caso non e' necessaria
rlen = recvfrom(s, rdata, sizeof(sdata), 0, (sockaddr *) &from,
(socklen_t *) &size_from);
if(rlen == -1)
{
perror("Problema in ricezione");
exit (3);
}
// Trasmissione
printf("...sto mandando info al client...\n");
slen = sendto(s, sdata, sizeof(sdata), 0, (sockaddr *) &from,
size_from);
if (slen <0)
{
perror ("Problema in trasmissione");
exit (4);
}
sleep(2);
}
unlink(PATH);
close(s);
return 0;
}

/* Programma Server -- Fine*/

/* Generazione casuale vettore -- INIZIO */
void vett_caso(int nfvett[DIM_VETT]) // numero dalla vettura = nfvett
{
int n_casuale = 0;
int i;
for(i=0; i<DIM_VETT; i++)
{
n_casuale = rand()%1000;
nfvett[i]=n_casuale;
}
}
/* Generazione casuale vettore -- FINE */



// CLIENT


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <unistd.h>

#define DIM_BUFF 256
#define DIM_VETT 7
#define DIM_DATA 1000
#define PATH "/home/rosenbergt/Daniele/Progetto_Metro/socket"
#define PATH_CLNT "/home/rosenbergt/Daniele/Progetto_Metro/socket"

// ========================================
====
void crea_file(int nfvett[DIM_VETT]);
void conversion_data(char data[DIM_DATA], int data_int[DIM_VETT]);
// ========================================
====

/* Programma Client */

int main(int argc, char * argv[])
{
int s = 0;
int num_bind = 0;
int rlen = 0;
//int conn = 0;
int rdata_conv[DIM_VETT];
char rdata[DIM_DATA];
struct sockaddr_un servaddr; // indirizzo del server
struct sockaddr_un clntaddr; // indirizzo del client
struct sockaddr_un from;
int size_from = 0;

// creazione socket

s = socket(AF_UNIX,SOCK_STREAM,0);
if(s<0)
{
perror("Errore sulla creazione del socket lato CLIENT");
exit (1);
}
else
{
printf("Creazione del socket numero <%d> effettuata con
successo!\n", s);
}

// creazione socket

// Inizializzzazione variabili Client

memset(&clntaddr,0,sizeof(clntaddr));
clntaddr.sun_family = AF_UNIX;
strcpy(clntaddr.sun_path, PATH_CLNT);

// Inizializzzazione variabili Server

memset(&clntaddr,0,sizeof(servaddr));
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, PATH);


// creazione bind

num_bind = bind(s,(struct sockaddr *) &servaddr, sizeof(servaddr));
if(num_bind < 0)
{
close(s);
perror("Errore sul bind");
exit (2);
}

// creazione bind

/*
conn = connect(s, (struct sockaddr *) &servaddr, sizeof(servaddr));
if(conn<0)
{
perror("Errore sulla connessione");
exit(3);
}
*/
for(;;)
{

// ricezione dal server

rlen = recvfrom(s, rdata, sizeof(rdata), 0, (sockaddr *) &from,
(socklen_t *) &size_from);
if(rlen < 0)
{
perror("Errore in ricezione");
exit(3);
}

// ricezione dal server
printf("...sto ricevendo info dal server...\n");
conversion_data(rdata, rdata_conv);
crea_file(rdata_conv);
sleep(2); // sleeping time
}
return 0;
}


void crea_file(int nfvett[DIM_VETT])
{
FILE *sc;
char *nome_file = "dati_vett.js";
sc = fopen(nome_file,"w");

/* Creazione del file da ricreare ogni volta -- INIZIO */
fprintf(sc,"function LoadVet(v)\n");
fprintf(sc,"{\n");
fprintf(sc,"v[0] = %d;\n",nfvett[0]);
fprintf(sc,"v[1] = %d;\n",nfvett[1]);
fprintf(sc,"v[2] = %d;\n",nfvett[2]);
fprintf(sc,"v[3] = %d;\n",nfvett[3]);
fprintf(sc,"v[4] = %d;\n",nfvett[4]);
fprintf(sc,"v[5] = %d;\n",nfvett[5]);
fprintf(sc,"v[6] = %d;\n",nfvett[6]);
fprintf(sc,"}\n");
fclose(sc);
/* Creazione del file da ricreare ogni volta -- FINE */
return;
}

void conversion_data(char data[DIM_DATA], int data_int[DIM_VETT])
{
int i = 0;
char *ps = NULL; // puntatori alla stringa...che finira' al settimo
valore
char *p = NULL; // puntatori alla stringa...che finira' al settimo
valore
p = data;
for(i=0; i<DIM_VETT; i++)
{
ps = strchr(data, ' ');
if(!ps)
{
printf("Errore sul puntatore...");
exit (1);
}
*ps = '\0';
data_int[i] = atoi(p);
p = ps;
p++;
ps++;
}
return;
}

Barry Margolin

2006-06-26, 7:20 pm

In article <1151323972.109318.325500@b68g2000cwa.googlegroups.com>,
ziche77@infinito.it wrote:

> I'm new in this kid of application and this is my first network
> program...
> I would like to enstablish a connection betwen a server and a client in
> local.
> The problem is on the function bind. When I lauch the server or the
> client the error is alway the same: "Address already in use".
> I don't know why and I don't know what I can do to revolve it, can
> anybody help me, please?


Google for SO_REUSEADDR.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com