LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-08-2005, 08:55 AM   #1
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Rep: Reputation: 30
Problem in recvfrom()


HI Guys,
I am developing code for a simple application server which uses raw sockets to which clients connect using raw sockets.I have used recvfrom() in the server which for some reason just refuses to block and keeps getting junk in its buffers ; namely the letter 'E' although this is not part of the code at all .Please help :-

Code:
#include <iostream>
using namespace std;
//#include <stdio.h>      /*has perror, in addition to other essentials*/
#include <netinet/in.h>  /*for the struct sockaddr_in and also htons etc */
#include <arpa/inet.h>   /*for inet_aton, inet_pton etc*/
#include <sys/socket.h>  /*for socket, connect, bind, listen*/
#include <sys/types.h>   /*for AF_INET etc*/
#include <unistd.h>      /*for close, fork, exec etc*/
#include <strings.h>     /*for bzero etc*/
#include <stdlib.h>      /*for atoi*/
#include <errno.h>       /*for errno*/
#include <sys/select.h>  /*for select*/

/* ECHO SERVER APPLICATION */
int main(int argc, char **argv)
{
	int sockfd, nbytes;
        socklen_t cliaddr_len;
        char mesg[4096];
        struct sockaddr_in servaddr, cliaddr;
        nbytes=0; 
	if( (sockfd = socket(AF_INET, SOCK_RAW, 6))<0)   /*create a RAW Socket with protocol value = 6 (TCP)*/
        {
                perror("Error in socket()");
                exit(1);
        }
        bzero(&cliaddr, sizeof(cliaddr));
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
	  	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(8888);
		cliaddr_len = sizeof(cliaddr);
                                                                                                                             
        if( (bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) < 0)
        {
                perror("Error in bind()");
                exit(1);
        }
	for( ; ; )
	{
		cout<<"Before the recvfrom"<<endl;
		cout<<"mesg"<<mesg<<endl;

		nbytes = recvfrom(sockfd, mesg, sizeof(mesg) , 0, (struct sockaddr *)&cliaddr, &cliaddr_len);
		//sleep(5);
		if(nbytes == -1)
		{
			perror("Error in recvfrom");
			exit(1);
		}
	} // end of for() loop		
} // end of main()
I'm using g++ on Redhatlinux 9.0 . The code compiles alright but does not block in recvfrom() even if no client connects to it.
Awaiting a response eagerly
Thanks a lot
Arvind

Last edited by live_dont_exist; 05-08-2005 at 09:35 AM.
 
Old 05-08-2005, 12:28 PM   #2
introuble
Member
 
Registered: Apr 2004
Distribution: Debian -unstable
Posts: 700

Rep: Reputation: 31
try a: fcntl(sockfd, F_SETFL, 0) i.e:

if( (sockfd = socket(AF_INET, SOCK_RAW, 6))<0) /*create a RAW Socket with protocol value = 6 (TCP)*/
{
perror("Error in socket()");
exit(1);
}
fcntl(sockfd, F_SETFL, 0);
 
Old 05-08-2005, 01:25 PM   #3
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
thnx 4 the suggestion...I see what you are getting at...unset the non blocking flag...good thought.
.but it didnt solve the problem..I still get a series of extremely frustrating mesg E lines.... ...
I think this could be something to do with the socket() call itself...could it be

socket(fd,SOCK_RAW,special protocol no for RAW sockets)..???

Just wondering...or could there be anything in setsockopt...any ideas...
Thnx
Arvind
 
Old 05-08-2005, 01:38 PM   #4
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
Hi,
Think I've found a solution here....If you go ahead and do the following :-

socket(AF_INET,SOCK_RAW,IPPROTO_RAW); //it does the trick....

Its happily blocked for over 5 minutes now....Now I'm gonna test the client and come back if I
have problems...
Thnx
Arvind
 
Old 05-08-2005, 03:50 PM   #5
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
okay...so now I have another problem...now the server runs fine and is blocking properly waiting for input..but when I run my client it doesnt reflect on the server side at all...compiling and running the client code individually ...and printing out the header and data seems okay...I mean everything prints out fine...but nothing ever comes on to the server...

I've actually made sure that the client and server are both running on 127.0.0.1 and using the same port 8888 and the socket call is using IPPROTO_RAW as its 3rd field ...socket(AF_INET,SOCK_RAW,IPPROTO_RAW)...netstat doesnt show anything cause its a raw socket and Richard STevens says raw sockets dont have ports....so I'm in a rut here...
I saw another page online saying that you cant pass data over a raw socket at all..because its the stack which is handling data and not the kernel like normal TCP or UDP...
So if someone could clear stuff out 4 me..and tell me ..whether I can use raw sockets to actually pass data
btw a client and a server I would be much obliged....any links I could read up on would be great as well
Thnx
Arvind
 
Old 05-08-2005, 04:25 PM   #6
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Original Poster
Rep: Reputation: 30
just one more thing of note here....read in Richard Stevens that you're supposed to read
raw socket packets at the data link layer using the pcap library...do I actually have to go down
to the Data LL to just read an ACK for a SYN??????.....any suggestions ...I'm at my wits end here
Thnx
Arvind
 
  


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
Why does a multithreaded program gets hanged at recvfrom()? kranti Programming 1 10-17-2005 10:44 AM
Why does a multithreaded program gets hanged at recvfrom()? kranti Linux - Newbie 1 10-17-2005 04:40 AM
Why does a multithreaded program gets hanged at recvfrom()? kranti Linux - General 1 10-17-2005 04:09 AM
problem with recvfrom() in linux kernel .4.21-15.EL?? nkshirsagar Red Hat 0 06-27-2005 09:59 AM
recvfrom() takes 20 ms to return? pingswept Programming 3 05-25-2004 03:18 AM

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

All times are GMT -5. The time now is 10:50 AM.

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