PDA

View Full Version : UNIX DOMAIN UDP CLIENT-SERVER PROGRAM


kushalkoolwal
10-15-2004, 07:30 AM
Hi,

I am developing a small ftp application on Unix platform. I am usign UNIX domain UDP socket. I have written a small code for my client and server program. I am using the sendto and recvfrom functions.

My problem is that my client is able to send a message to the server and the sever receives it properly. But again when my srver wants to send the data to the client it gives me error on 'sendto' function (-1 error). It seems that the server is not able to get the unix file path from the client.

In my program the client initiates the connection(i.e. it first sends the data).

I am attaching my server and client file.
It would be great if some one could help me on this.

Thanks

Kushal





//server.cpp
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>

int sockfd;
struct sockaddr_un server;
struct sockaddr_un from;
void error(char*);

int main (int argc, char** argv)
{
int len;
if(argc < 2)
{
fprintf(stderr,"ERROR, No argument available\n");
exit(0);
}

sockfd= socket(AF_UNIX,SOCK_DGRAM,0); //create socket
if(sockfd<0)
{
error("ERROR opening socket");
}

len=sizeof(server);
server.sun_family=AF_UNIX; //specify the Domain
strcpy(server.sun_path,argv[1]); //copy the Path Name
if(bind(sockfd,(struct sockaddr *)&server,len)<0) //bind the socket with an address
{
error("Can't Bind to socket");
}

char buffer[1024];
int length,n;
int fromlen = sizeof(struct sockaddr_un);

length=recvfrom(sockfd,buffer,1024,0,(struct sockaddr *)&from,(socklen_t *)&fromlen);
printf("\nI am printing the path %s",from.sun_path);
if(length<0)
{

error("Cannot read from the client");
}
printf("\n%s",buffer);

bzero(buffer,1024);
fgets(buffer,1024,stdin);
n=0;
printf("\nI am printing the path %s again",from.sun_path);
n=sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)&from,fromlen);
//here it gives the error
if (n = -1 )
{
error("sendtoin error mode");

}

printf("\nThat's great!!! Message received");
printf("\nExiting.....");

}

void error(char* msg)
{
perror(msg);
exit(0);
}



//client.cpp
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>


int socket_a;
struct sockaddr_un sockaddr_a;
struct sockaddr_un from;

// a way to print fatal errors
void die(char* msg)
{
printf(msg);
exit(-1);
}


int main(int argc, char** argv)
{
int len,length;
char buffer[256];
// Check the number of args
if(argc!=2)
{
die("Usage: sniff < side a > < side b >");
}

// setup and local sockets
socket_a=socket(AF_UNIX,SOCK_DGRAM,0);
if(socket_a==0)
{
die("Can't get sockets from OS");
}

// len=strlen(argv[1]);
sockaddr_a.sun_family=AF_UNIX;

strcpy(sockaddr_a.sun_path,argv[1]);

// setup a buffer
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
// pass the packet to the other side
len=0;
int length1=sizeof(struct sockaddr_un);
printf("\n\nThe path is %s",sockaddr_a.sun_path);
len=sendto(socket_a,buffer,strlen(buffer),0,(struc t sockaddr*)&sockaddr_a,length1);
if(len < 0)
{
die("bad write on sniff_b!");
}

bzero(buffer,256);
length=recvfrom(socket_a,buffer,256,0,(struct sockaddr *)&from,(socklen_t *)&length1);
printf("\nMessage Received from the server");
printf("\nExiting....");

}

RobSeace
10-15-2004, 12:49 PM
n=sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)&from,fromlen);
//here it gives the error
if (n = -1 )
{


Take a very close look at that if() test again... Keep looking, it'll
hit you eventually... ;-) You've made one of the all-time classic
C errors: using "=" (asignment) instead of "==" (equality test)...
So, your sendto() isn't failing; you're simply forcing "n" to -1, and
then falling into the error-handling case (since -1 is non-zero, and
hence will be treated as true)...

i3839
10-15-2004, 06:28 PM
So the lesson learned should be to always compile with all warning setting on max (gcc -Wall will warn about things like this).

RobSeace
10-15-2004, 09:17 PM
I usually compile with "-Wall -W -Wmissing-prototypes -Wno-main
-Wno-unused -Wno-sign-compare"... The "-W" adds a few other
useful checks in addition to "-Wall"... But, I turn off a few of the
more annoying ones... ;-) (Though, "-Wno-main" never worked,
at least with older GCCs... I haven't tried in a while; I just got in
the habit of declaring my main()'s to return int rather than void, as
I had previously gotten in the habit of... ;-) I always exit() from my
main() rather than return, so a return type is just silly...) Oh, and
I also hate the silly warning if you use strftime() with any macro
that can produce only a 2-digit year, but haven't found a way to
turn it off yet (unless you want to disable *printf()/*scanf() format
checks, as well, which I definitely DON'T want to disable)... ;-/
Oooh, I just looked in "info gcc", and I see they now have a
"-Wno-format-y2k"! Excellent, I'll have to start using that! ;-) I'm
glad to see I wasn't the only one annoyed by that... ;-)

kushalkoolwal
10-18-2004, 09:03 AM
Thank you very much!!!! :D