Heya. So I asked and recieved tutorials (thanks to *all* of the submitters if you are reading this)
Now I followed the descriptions and winges out the best code that I could do (in other words: I took a shot at it, no guarantee it's nifty or even half-way decent to look at!)
When I compile this server I get the following error:
Code:
localfilename.cpp:43: invalid conversion from 'int*' to 'socklen_t*'
The code is further down here, but the linenumbers have changed (due to me removing a bit of commented-out code that wasn't useable for a server)
The line:
Code:
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
Now sin_size is a pointer to an int variable that uses sizeof() to get the size of the struct sockaddr_in.
I have no idea what is wrong here, it seems like I have debugged the rest of my code and wrote a pretty clean first try. This is my only bug left
All the code:
Code:
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
using namespace std;
#define MYPORT 3490
#define BACKLOG 10
int main()
{
int sockfd, new_fd; //socket file-descriptor
int sin_size; // will hold the size of a struct sockaddr_in
struct sockaddr_in my_addr; //!paralel! sockaddr struct
struct sockaddr_in their_addr; //again
//synopsis AF_INET, SOCK_STREAM(TCP) 0(auto-select fitting protocol)
sockfd = socket(AF_INET, SOCK_STREAM, 0);
//Localhost settings
my_addr.sin_family = AF_INET; //host byte order
my_addr.sin_port = htons(MYPORT); //network byte order
my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // network byte order: INADDR_ANY = localhost
bzero(&(my_addr.sin_zero), 8); //zero the rest of the struct
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)); //binding, useful
//only if you have intentions of making a connection to a special port
listen(sockfd, BACKLOG);
while (true)
{
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
char* connect_from = "not implemented yet";
cout << "server got a connection from: " << connect_from << endl;
if (!fork())
{
close(sockfd);
char *msg = "Wow hello from the other side!"; //msg to send
int len = strlen(msg); //counts the size of the msg
send(new_fd, msg, len, 0);
close(new_fd);
exit(0);
}
close(new_fd);
}
return 0;
}
So what am I doing wrong here ? I read the guide's synopsis a few times and I *think* I wrote it as it should be written, however the error says otherwise :'(
Thanks in advance!