LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2004, 02:32 PM   #1
Dark Carnival
Member
 
Registered: Jun 2003
Posts: 166

Rep: Reputation: 30
(c++) network socket programming: help with accept()


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!
 
Old 08-11-2004, 02:58 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
look at man accept. the third parameter is not an integer, it is a type of socklen_t, which may or may not be typedef'd to an integer. so declare that variable as a socklen_t.
 
Old 08-11-2004, 03:22 PM   #3
Dark Carnival
Member
 
Registered: Jun 2003
Posts: 166

Original Poster
Rep: Reputation: 30
This is a *very* newbie'ish question, but man pages ? I think of man pages as the man [program] thing... where's the cpp man pages ?
 
Old 08-11-2004, 03:58 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
the manual pages have sections, read man 3 intro to learn about section 3 (the standard libraries)

<edit>oops, i forgot accept() was a sys call - yeah section 2 might be better</edit>

Last edited by kev82; 08-11-2004 at 04:28 PM.
 
Old 08-11-2004, 04:22 PM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
More specifically, try man 2 accept to get documentation on the accept() function.

You'll find the following note in this particular man page:

Quote:
NOTE
The third argument of accept was originally declared as an
`int *' (and is that under libc4 and libc5 and on many
other systems like BSD 4.*, SunOS 4, SGI); a POSIX 1003.1g
draft standard wanted to change it into a `size_t *', and
that is what it is for SunOS 5. Later POSIX drafts have
`socklen_t *', and so do the Single Unix Specification and
glibc2. Quoting Linus Torvalds: _Any_ sane library _must_
have "socklen_t" be the same size as int. Anything else
breaks any BSD socket layer stuff. POSIX initially _did_
make it a size_t, and I (and hopefully others, but obvi-
ously not too many) complained to them very loudly indeed.
Making it a size_t is completely broken, exactly because
size_t very seldom is the same size as "int" on 64-bit
architectures, for example. And it _has_ to be the same
size as "int" because that's what the BSD socket interface
is. Anyway, the POSIX people eventually got a clue, and
created "socklen_t". They shouldn't have touched it in
the first place, but once they did they felt it had to
have a named type for some unfathomable reason (probably
somebody didn't like losing face over having done the
original stupid thing, so they silently just renamed their
blunder).

Last edited by deiussum; 08-11-2004 at 04:26 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
breaking out of accept() with interrupts (socket question) hatha Programming 1 07-16-2005 05:33 AM
Socket Programming jawadhashmi Programming 4 05-24-2005 03:04 AM
Help me ... about Socket programming.. rajsun Programming 2 04-24-2005 04:50 PM
C: Socket weirdness: select() -> fork() -> accept() maxfacta Programming 1 03-01-2005 01:33 PM
Problem with accept() socket call which doesn't return jph Programming 4 08-06-2003 06:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration