|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jun 2004
Messaggi: 760
|
[C] client socket
Ciao a tutti...
ho iniziato a cimentarmi con la programmazione socket... in particolare, sto seguendo "UNIX Network Programming" di Stevens, che mi sembra molto ben fatto... ora... ho provato a far girare il programma qua sotto... si tratta di un client che stabilisce una connessione tcp con un server che gli spedisce la data corrente attraverso la porta 13... questa data viene poi stampata a video... c'è un piccolo problema... una volta che il programma viene lanciato, non succede niente... a video non viene stampato nessun carattere e il programma non termina.... come mai? Codice:
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include "errlib.c"
#include "sockwrap.c"
#define MAXBUFL 255
#define DAYTIME_PORT 13
char *prog;
int main (int argc, char *argv[])
{
int sockfd, n;
char buf[MAXBUFL+1];
struct sockaddr_in servaddr;
/* for errlib to know the program name */
prog = argv[0];
/* check the arguments */
if (argc != 2)
err_quit ("usage: %s IPaddress_of_daytime_server", prog);
/* create socket */
sockfd = Socket(AF_INET, SOCK_STREAM, 0);
/* specify address of server to connect to */
memset (&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(DAYTIME_PORT);
Inet_aton(argv[1], &servaddr.sin_addr);
/* try to connect */
Connect (sockfd, (SA*) &servaddr, sizeof(servaddr));
/* read the answer */
while ( (n = Read(sockfd,buf,MAXBUFL)) > 0)
{
//buf[n] = 0; /* null terminate */
//if (printf ("Current date and time: %s\n", buf) == EOF)
//err_sys ("(%s) error - printf() failed", prog);
Write (1, buf, n);
}
return 0;
}
dimenticavo!... sto programmando sotto linux, con gcc come compilatore... grazie mille!!
__________________
Gandalf_BD -------------------------------------------- "When you aim at perfection, you discover it's a moving target" |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jun 2004
Messaggi: 760
|
up
__________________
Gandalf_BD -------------------------------------------- "When you aim at perfection, you discover it's a moving target" |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jun 2004
Messaggi: 760
|
allora... pasticciando un po' con gdb mi sembra di capire che si "pianta" quando fa la connect...
quando arrivo a quel punto, do "step e gdb si ferma... proprio come quando eseguo il programma.... da cosa può essere dovuto? ho provato ad eseguirlo con IP differenti, ma tutti mi hanno dato problemi... datemi una mano! grazie per l'attenzione!
__________________
Gandalf_BD -------------------------------------------- "When you aim at perfection, you discover it's a moving target" |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Jun 2004
Messaggi: 760
|
up
__________________
Gandalf_BD -------------------------------------------- "When you aim at perfection, you discover it's a moving target" |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 15:09.



















