Article View: es.comp.os.linux.programacion
Article #13183Re: Problema de sockets
From: Adrian Rossiter
Date: Tue, 13 Mar 2001 11:46
Date: Tue, 13 Mar 2001 11:46
154 lines
4846 bytes
4846 bytes
On Tue, 13 Mar 2001 09:12:23 +0100, Cron wrote:
>Fue creado en otro proceso, pero el descriptor se guarda en la memoria
>compartida, por lo tanto deberia ser accesible por todos los procesos aunque
>se creen m�s tarde.
>
>Estube haciendo pruebas ayer y s� que falla la memora compartida. Si la
He cambiado tu programa un poco para que mostre lo que hace. La memoria
compartida parece funcionar pero los descriptores que fueron creados
en otros procesos dan errores con "send".
Saludos
Adrian.
Adrian Rossiter - adrian_r@teleline.es
http://teleline.terra.es/personal/adrian_r
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXUSERS 10
#define IP "127.0.0.1"
#define PORT 4000
struct info
{
int nsfd;
char nombre[13];
};
struct info *usuario;
main()
{
int sfd, nsfd, pid;
struct sockaddr_in ser_addr, cli_addr;
int cli_addr_len;
char buf[1000];
char msg[1000];
int shmid;
key_t llave;
int i, char_cnt, me;
if ((llave = ftok ("tsock.c", 'K')) == (key_t)-1)
{
perror ("Error ftok");
exit (-1);
}
if ((shmid = shmget (llave, MAXUSERS * sizeof (struct info), IPC_CREAT |
O_RDWR)) == -1)
{
perror ("Error shmget");
exit (-1);
}
if((usuario = shmat (shmid, 0, 0)) == (void *)-1)
{
perror ("Error shmat");
exit (-1);
}
for (i = 0; i<MAXUSERS; i++)
usuario[i].nsfd = -1;
if ((sfd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
{
perror ("Error socket");
exit (-1);
}
ser_addr.sin_family = AF_INET;
ser_addr.sin_addr.s_addr = inet_addr (IP);
ser_addr.sin_port = htons (PORT);
if (bind (sfd, &ser_addr, sizeof (ser_addr)) == -1)
{
perror ("Error bind");
exit (-1);
}
listen (sfd, 5);
while (1)
{
cli_addr_len = sizeof(cli_addr);
if ((nsfd = accept (sfd, &cli_addr, &cli_addr_len)) == -1)
{
perror ("Error accept");
exit (-1);
}
if ((pid = fork ()) == -1)
perror ("Error fork");
else if (pid == 0)
{
close (sfd);
/* C�digo del proceso hijo. */
for(i=0; i<MAXUSERS; i++) {
sprintf(buf, "usuario[%d].nsfd = %d\n", i, usuario[i].nsfd);
send (nsfd, buf, strlen (buf), 0);
}
i = 0;
while (usuario[i].nsfd != -1)
i++;
usuario[i].nsfd = nsfd;
me=i;
char_cnt = recv (nsfd, buf, 1000, 0);
buf[char_cnt] = '\0';
send (nsfd, buf, strlen (buf), 0);
strcpy (usuario[i].nombre, buf);
sprintf(buf, "nsfd = %d\n", nsfd);
send (nsfd, buf, strlen (buf), 0);
strcpy (buf, "Bienvenido\n");
send (nsfd, buf, strlen (buf), 0);
while (1)
{
char_cnt = recv (nsfd, buf, 1000, 0);
buf[char_cnt] = '\0';
for(i=0; i<MAXUSERS; i++) {
sprintf(msg, "usuario[%d].nsfd = %d\n", i, usuario[i].nsfd);
send (nsfd, msg, strlen (msg), 0);
}
i = 0;
while (i < MAXUSERS)
{
if (usuario[i].nsfd != -1){
if(send (usuario[i].nsfd, buf, strlen (buf), 0) == -1) {
sprintf(msg, "cliente %d envia a cliente %d", me, i);
perror(msg);
}
}
i++;
}
}
close (nsfd);
exit(0);
}
}
}
Technical Details
Message-ID:
<slrn9as244.vu.adrianr@localhost.adrian_r>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!nntp.cs.ubc.ca!fu-berlin.de!uni-berlin.de!212.106.228.32!not-for-mailReferences:
<98h49d$neg$1@diana.bcn.ttd.net> <98i9o8$6dm$2@news.unizar.es> <98id9l$s2g$1@diana.bcn.ttd.net> <slrn9arind.v8.adrianr@localhost.adrian_r> <98kksi$8g2$1@diana.bcn.ttd.net>Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: f86c8,1e9b2d04f4f0632 X-Google-Attributes: gidf86c8,public X-Google-ArrivalTime: 2001-03-13 03:50:29 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!nntp.cs.ubc.ca!fu-berlin.de!uni-berlin.de!212.106.228.32!not-for-mail From: adrianr@jazzfree.com (Adrian Rossiter) Newsgroups: es.comp.os.linux.programacion Subject: Re: Problema de sockets Date: Tue, 13 Mar 2001 11:46:14 +0000 Lines: 157 Message-ID: <slrn9as244.vu.adrianr@localhost.adrian_r> References: <98h49d$neg$1@diana.bcn.ttd.net> <98i9o8$6dm$2@news.unizar.es> <98id9l$s2g$1@diana.bcn.ttd.net> <slrn9arind.v8.adrianr@localhost.adrian_r> <98kksi$8g2$1@diana.bcn.ttd.net> Reply-To: adrian_r@teleline.es NNTP-Posting-Host: 212.106.228.32 Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: fu-berlin.de 984483945 2614691 212.106.228.32 (16 [70272]) User-Agent: slrn/0.9.6.2 (Linux)