Hello all,
I do not have a lot of expierence with C and at this moment I am kinda stuck. What I am trying to do is make a server in c which is able to communicate with a telnet session. At this moment I can connect with telnet and send a msg from the server to the client and the client can send a msg to the server.
Code:
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MYPORT 3490
#define MAXDATASIZE 100
main()
{
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", 34, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
char buf[MAXDATASIZE];
while((aant_bytes = recv(new_fd, buf, MAXDATASIZE - 1, 0)) > 0) {
buf[aant_bytes] = '\0';
printf("%s",buf);
}
}
}
My problem
My problem is located in the last bit of code in the while statement. the server receives chars from the client and it gets printed on the server window. This is fun and all but what want to do is convert all the char value's into one intiger. alltought this doesn't sound to hard I can't seem to figure it out.
at this moment if I type 100 in the client I will get the following result in buf '1' '0' '0'. is there a way to convert this into a int saying 100?
Kind regards,
Yuushi Celeritas