nunesalpha
(usa Outra)
Enviado em 01/05/2016 - 09:50h
Alguém saberia dizer porque o cliente não consegue se conectar ao servidor quando é retirada a linha de código de número 57 do servidor? Abaixo código do servidor e do cliente
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define SERVER_PORT 5432
#define MAX_PENDING 5
#define MAX_LINE 256
int main (){
struct sockaddr_in sin, client;
char buf[MAX_LINE];
char str[MAX_LINE];
int s, new_s;
int len;
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERVER_PORT);
sin.sin_addr.s_addr = INADDR_ANY;
if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if((bind(s, (struct sockaddr*)&sin, sizeof(sin))) < 0) {
perror("simplex-talk: bind");
exit(1);
}
listen(s, MAX_PENDING);
printf("Servidor inicializado.\n");
printf("Esperando por conexoes...\n\n");
while(1) {
socklen_t sin_size;
sin_size = sizeof(struct sockaddr_in);
if((new_s = accept(s, (struct sockaddr *)&sin, &sin_size)) < 0) {
perror("simplex-talk: accept");
exit(1);
}
if(new_s > 0) printf("recebi uma conexão\n");
while(len = recv(new_s, buf, sizeof(buf), 0)) {
printf(" imprimir sem o fputs");
//fputs(buf, stdout); //SEM ESTA LINHA O CLIENTE NÃO CONSEGUE SE CONECTAR
}
close(new_s);
}
}
cliente
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE 256
#define SERVER_PORT 5432
int main (){
//FILE *fp;
//struct hostent *hp;
struct sockaddr_in sin;
char *host;
char buf[MAX_LINE];
int s;
int len;
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(SERVER_PORT);
inet_aton("127.0.0.1", &sin.sin_addr.s_addr);
if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("simplex-talk: socket");
exit(1);
}
if(connect(s, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
perror("simplex-talk: connect");
close(s);
exit(1);
}
while(fgets(buf, sizeof(buf), stdin)) {
buf[MAX_LINE-1] = '\0';
len = strlen(buf) + 1;
send(s, buf, len, 0);
}
}