Article View: es.comp.os.linux.programacion
Article #11253Problema con sockets...
From: David Ramón Yanes Fariña
Date: Tue, 18 Dec 2001 15:36
Date: Tue, 18 Dec 2001 15:36
136 lines
4083 bytes
4083 bytes
Hola amigos...
me gustar�a saber que hay de err�neo en este
programa, su �nica intenci�n es de leer un comando
de stdin, enviarlo por el socket y recibir lo devuelto
por stdout, pero no se porque solo funciona el primer
comando y hay que apretar enter dos veces...
�que hay mal?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <errno.h>
#define FALSE 0
#define TRUE 1
int main(int argc, char **argv){
char buf[500];
char rcvbuf[8192];
// char *rcvbuf;
int i, sock, result;
int cont=1;
int ok_server=FALSE;
int Server_Alive=FALSE;
int Check_Server=TRUE;
struct sockaddr_in name;
struct hostent *hostinfo;
//rcvbuf=(char *) malloc(1000*sizeof(rcvbuf));
// Comprobamos el n�mero de par�metros pasados.
if (argc < 3){
printf ("Usage %s host port\n", argv[0]);
exit(0);
}
/*---------Comienzo del bucle principal for -----------------------*/
hostinfo=gethostbyname(argv[1]);
if (!hostinfo){
herror("Host Not Found.");
exit(-1);
}
name.sin_family=AF_INET;
name.sin_port=htons(atoi(argv[2]));
name.sin_addr=*(struct in_addr *)hostinfo->h_addr;
sock=socket(AF_INET, SOCK_STREAM, 0);
result=connect(sock, (struct sockaddr *)&name, sizeof(struct sockaddr_in));
fcntl (sock, F_SETFL, O_NONBLOCK);
//-------- Rutinas de manejo de erros de conecci�n------------------
if (result != 0) {
herror("No fue posible hacer la conexi�n.");
exit(-1);
}
if (sock < 0){
herror("Imposible establece comunicaci�n con el socket.");
exit(-1);
}
else {
if (Server_Alive==TRUE){
printf ("OK, servidor Activo.\n");
}Server_Alive=FALSE;
}
//------ Fin de rutinas de errores de conecci�n -----------------------
memset(rcvbuf,0x00,sizeof(rcvbuf));
memset(buf,0x00,sizeof(buf));
recv(sock,rcvbuf,sizeof(rcvbuf),0);
printf ("%s \n",rcvbuf);
while(1){
memset(rcvbuf,0x00,sizeof(rcvbuf));
//printf ("%d > ",(cont++));
if (strcmp(buf,"quit")==0){
printf ("Quitting...\n");
exit (0);
}
gets(buf);
send(sock,buf,sizeof(buf),0);
recv(sock,rcvbuf,sizeof(rcvbuf),0);
//write(sock,buf,sizeof(buf));
//read(sock,rcvbuf,sizeof(rcvbuf));
printf ("%s\n",rcvbuf);
memset(buf,0x00,sizeof(buf));
memset(rcvbuf,0x00,sizeof(buf));
//printf ("Last command: %s\n",buf);
}
close(sock);
}
Technical Details
Message-ID:
<iqnnv9.js.ln@127.0.0.1>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!t-online.de!newsfeeds.belnet.be!news.belnet.be!opentransit.net!newsfeed.comtenidos.com!newsfeeder.wanadoo.es!davidShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f86c8,640fd19872f2a704,start X-Google-Attributes: gidf86c8,public X-Google-ArrivalTime: 2001-12-18 07:41:02 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!t-online.de!newsfeeds.belnet.be!news.belnet.be!opentransit.net!newsfeed.comtenidos.com!newsfeeder.wanadoo.es!david From: david@elisa.kitenet.net (David Ramón Yanes Fariña) Newsgroups: es.comp.os.linux.programacion Subject: Problema con sockets... Date: Tue, 18 Dec 2001 15:36:50 +0000 Organization: Wanadoo Lines: 139 Message-ID: <iqnnv9.js.ln@127.0.0.1> NNTP-Posting-Host: 62-36-129-66.dialup.uni2.es Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: news.wanadoo.es 1008689585 29849 62.36.129.66 (18 Dec 2001 15:33:05 GMT) X-Complaints-To: usenet@wanadoo.es NNTP-Posting-Date: Tue, 18 Dec 2001 15:33:05 +0000 (UTC) User-Agent: slrn/0.9.6.2 (Linux)