Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] EINVAL on socket read


Michael Still was once rumoured to have said:
> 
> Hey all.
> 
> I have some socket code, which binds to a port and then accepts
> connections. Every time I try to read() from the socket, I get a EINVAL. I
> am sure that I am doing something grossly stupid, and was wondering if
> anyone could tell me what I had missed.
> 
> Sorry about the big code dump, I have tried to trim it down a bit.

[Snip]
> 
>   while(1){
>     if(accept(socketfd, (struct sockaddr *) &connaddr, &connaddrlen) ==
> -1)

This is where your error lies.

this should read:

	while(1) {
		int	newfd = 0;

		if ((newfd = accept(socketfd, (struct sockaddr*) &connaddr, &connaddrlen) == -1) {
			// error handling here...
			...
		}

And use newfd as the socket to read from/write to.

C.
-- 
--==============================================--
  Crossfire      | This email was brought to you
  xfire@xxxxxxxx | on 100% Recycled Electrons
--==============================================--