Problema con sockets

Iniciado por Fastolfe, 5 Enero 2012, 20:52 PM

0 Miembros y 1 Visitante están viendo este tema.

Fastolfe

Buenas!! acabo de empezar con los sockets y mi  programa solo envia y recibe una cadena (lo estoy probando con el netcat), lo que quiero es que sea una especie de "chat", aunque no sabiendo por que falla no sigo programando para no acumular errores.

El código es este:

#include <stdio.h>          
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define MAXR 100

int main(){

int fd, fd2;

struct sockaddr_in server;

struct sockaddr_in client;

int sin_size;

if ((fd=socket(AF_INET, SOCK_STREAM, 0)) == -1 ) {  
printf("error en socket()\n");
exit(-1);
}

printf("Introduce el puerto en el que quieres escuchar: ");
int puerto = 0;
scanf("%d", &puerto);

char cadena[100];
char *pcadena = &cadena;
int tcadena = 0;

server.sin_family = AF_INET;
server.sin_port = htons(puerto);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(server.sin_zero),8);

bind(fd,(struct sockaddr*)&server,sizeof(struct sockaddr));
listen(fd,1);

char recibido[MAXR];
char *precibido = &recibido;
int i = 0;

while (1){
sin_size=sizeof(struct sockaddr_in);
fd2 = accept(fd, (struct sockaddr *)&client, &sin_size);

scanf("%s", &cadena);

tcadena = 0;

while (cadena[tcadena] != '\0' && tcadena < 100){
tcadena++;
}

send(fd2,pcadena,tcadena,0);

recv(fd2, precibido, MAXR, 0);

printf("\n %s", recibido);
}

return 0;

}


Muchas gracias de antemano!! :)

PD: programo en ubuntu 11.04

Sagrini

http://wiki.elhacker.net/programacion/cc/articulos/introducion-a-los-sockets-en-ansi-c
http://wiki.elhacker.net/programacion/cc/articulos/introducion-a-los-sockets-en-ansi-c/Tutorial_Sockets.pdf?attredirects=0&d=1

#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>

int socketfd, newsocket;
char *filename;

void shutup (int signal)
{
FILE *log;
log=fopen (filename, "a+");
times ();
fprintf (log, "Shutting down...\n\n");
fclose (log);
close (newsocket);
close (socketfd);
exit (0);
}

int times ()
{
FILE *log;
time_t now=time (0);
struct tm *ahora;
char buffer [40];
ahora=localtime ((const time_t*)&now);
strftime (buffer, 40, "%d/%m/%Y %H:%M:%S", ahora);
log=fopen (filename, "a+");
printf ("%s  ", buffer);
fprintf (log,"%s ", buffer);
fclose (log);
return 0;
}

int main (int argc, char *argv [])
{
time_t now=time (0);
struct tm *ahora;
char hora [40];
ahora=localtime ((const time_t*)&now);
strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
printf ("SmallServ 2.0 : By Sagrini : Sagrini2010 : %s\n", hora);
if (getuid()!=0)
{
printf ("This proccess must be run by root.\n\n");
return 1;
}
if (argc<3)
{
printf ("Use: %s <PORT> <LOG>\n\n", argv[0]);
return 1;
}
int cont;
FILE *log;
struct sockaddr_in client, host;
char buffer [1024];
int size=sizeof (client);
filename = argv [2];
socketfd=socket (2, 1 , 0);
host.sin_family=AF_INET;
host.sin_port=htons (atoi (argv [1]));
host.sin_addr.s_addr=0;
bind (socketfd, (struct sockaddr*)&host, sizeof(struct sockaddr));
listen (socketfd, 3);
log=fopen (filename, "a+");
times ();
fprintf (log, "Starting up...\n\n");
fclose (log);
signal (SIGTERM, shutup);
signal (SIGINT, shutup);
daemon (1, 0);
while (1)
{
newsocket=accept (socketfd, (struct sockaddr*)&client, &size);
log=fopen (filename, "a+");
times ();
fprintf (log, "Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
fclose (log);
cont=recv (newsocket, &buffer, 1024, 0);
while (cont>2)
{
log=fopen (filename, "a+");
times ();
buffer [cont-1] = '\0';
fprintf (log, "RECV %d bytes: %s\n", cont-2, buffer);
fclose (log);
cont=recv (newsocket, &buffer, 1024, 0);
}
log=fopen (filename, "a+");
times ();
fprintf (log, "Finishing connection from %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs(client.sin_port));
fclose (log);
close (newsocket);
}
close (socketfd);
return 0;
}

Fastolfe

Muchas gracias Sagrini!! Me pondré a comprender tu programa y repasar de nuevo el tutorial.
Gracias! ;)