ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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);
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
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.