Hello all.
Firstly, I wasn't sure if this should go in the Networking forum or in the C++ forum, so I posted it here as I figured the C++ people are more likely to look here than the networking one. We'll seoon see
I've been toying with C for a while, just brought C++ for Dummies figuring I'd consider moving over. Anyway, looking at some socket stuff (not covered by the book, annoyingly) and have followed Beej's network tutorial. However, I'm wondering how I can recieve more that one line of input from a server. I'm assuming I need a while() look in here somewhere, chekcing for multiple lines. Not entirely sure where, and seeing as the tutorial didn't cover it I figured I'd ask
main.cpp
Code:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "Config.cpp"
using namespace std;
int main(int argc, char *argv[])
{
/* Setup the configration options. */
Configuration.Server = "irc.blitzed.org";
Configuration.Channel = "#testing12345";
Configuration.RealName = "Test, from DaveHope";
Configuration.Port = 6667;
Configuration.DataSize = 100;
Configuration.Debug = 1;
int sockfd, numbytes;
char buf[Configuration.DataSize];
struct hostent *he;
struct sockaddr_in their_addr; // connector's address information
if ((he=gethostbyname(Configuration.Server)) == NULL) {
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(Configuration.Port); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
if ((numbytes=recv(sockfd, buf, Configuration.DataSize-1, 0)) == -1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("%s\n",buf);
/* Close the connection. */
close(sockfd);
/* Exit. */
return 0;
}
Config.cpp
Code:
/*
* Configuration Array.
* The below struct will hold all configuration information.
*/
struct Configuration
{
/* General Configuration. */
int Port;
char *Server;
char *Channel;
char *Nickname;
char *RealName;
int DataSize;
/* Debug Mode ? */
int Debug;
/* Socket/Connection Configuration. */
int state;
};
struct Configuration Configuration;
As always, you guys are stars. Thanks for putting up with my so far!