Hello all,
I am trying to make a small server which is connectable trough a telnet session. I am following Beej's guide but I am stuck with a error I do not seem to get fixed.
when I run this source I am able to connect, I see the msges in server console and on the client. But when I start typing it only displays the first letter. What I would like is that it receives data when I hit enter.
At this moment when I type data for example it shows a d immediatly but not the rest
Code:
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MYPORT 3490
#define MAXDATASIZE 100
int main()
{
char buf[MAXDATASIZE];
int sockfd, new_fd, aant_bytes; /* luisteren op sock_fd, nieuwe verbinding op new_fd */
struct sockaddr_in mijn_addr; /* mijn adresinformatie */
struct sockaddr_in hun_addr; /* connector's adresinformatie */
int sin_size;
// vergeet de foutafhandeling voor socket() niet | errno is fout | geeft -1:
sockfd = socket(AF_INET, SOCK_STREAM, 0); // doe wat foutafhandeling!
mijn_addr.sin_family = AF_INET; // host byte volgorde
mijn_addr.sin_port = htons(MYPORT); // short, netwerk byte volgorde
mijn_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(mijn_addr.sin_zero), '\0', 8); // de rest van de struct op nul
// vergeet de foutafhandeling voor bind() niet | errno is fout | geeft -1:
bind(sockfd, (struct sockaddr *)&mijn_addr, sizeof(struct sockaddr));
//server laten luisteren | TODO Errorhandling | errno is fout | value -1
printf("server: I am now listening on %d \n", MYPORT);
listen(sockfd, 10);
while(1){
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sockfd,(struct sockaddr*)&hun_addr,&sin_size))== -1){
perror("accept");
continue;
}
printf("server: Received connection from: %s \n",inet_ntoa(hun_addr.sin_addr));
if (!fork()) { /* dit is het kind proces */
if (send(new_fd, "Welcome to my postFix Calculator. \n \n", 36, 0) == -1){
perror("send");
close(new_fd);
exit(0);
}
if ((aant_bytes = recv(new_fd, buf, MAXDATASIZE-1, 0)) <= 0) {
// got error or connection closed by client
if (aant_bytes == 0) {
// connection closed
printf("selectserver: socket hung up\n");
}
else {
perror("recv");
}
}
else{
// we got some data from a client
printf("server: %s \n ",buf);
}
}
}
return 0;
}
I've been messing with this all night and I just don't seem to get it right.
Kind regards,
Yuushi Celeritas