Unix Programming - Plz help again: sendto problem !

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > Plz help again: sendto problem !





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Plz help again: sendto problem !
Gaurav

2006-03-03, 6:43 pm

Hi ,

Im using unix domain Datagram socket to meet the following:
The client will send a one byte mesg (a blank space ) to the server.
Upon recieving the one byte mesg, the server will send the client a
"OK" mesg . But, before recieving anything,
I am getting the --- send::bad address error. Im unable to track why it
is happening.
Please help me out in solving this.What should I do to sendto and
recvfrom properly to meet the requirments ?

best regards,
Gaurav

The client code :

#include <stdio.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/un.h>
#include <unistd.h>

#define CLIENT_PATH "cli_sock"
#define SERVER_PATH "serv_sock"

#define SUCCESS 1
#define FAILURE 0

#define ACK_LEN 3
int sockfd = 0;
struct sockaddr_un servaddr;

int SockInit(int *pSocket)
{
int sock = 0;

struct sockaddr_un sockaddr;

sock = socket (AF_UNIX,SOCK_DGRAM, 0);

if ( sock < 0)
{
printf("Error in creating socket\n");
return FAILURE;
}
memset (&sockaddr, 0, sizeof(sockaddr));
sockaddr.sun_family = AF_UNIX;
strcpy (sockaddr.sun_path, CLIENT_PATH);

if (bind (sock,(struct sockaddr *)&sockaddr, sizeof(sockaddr))<0 )
{
perror("client :bind");
return FAILURE;
}
*pSocket = sock;
return SUCCESS;
}

int main()
{

int sendsize =0;
int recvsize = 0;
char datatosend = ' ' ; /*one byte blank mesg */
int serverlen = 0;
char mesgarray[ACK_LEN];
char ackmsg[] = "OK";

if ( SockInit(&sockfd)==FAILURE)
{
return;
}
unlink(SERVER_PATH);
serverlen = sizeof(servaddr);
memset(&servaddr,0,serverlen);
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, SERVER_PATH);

sendsize = sendto(sockfd, datatosend,2, 0, (struct sockaddr
*)&servaddr, serverlen);

if ( sendsize < 0)
{
perror("send:");
return;
}
for(;;)
{
recvsize = recvfrom(sockfd,mesgarray,ACK_LEN,0, (struct sockaddr
*)&servaddr, &serverlen);
if (recvsize <0)
{
continue;
}
else
{
if ((recvsize == sizeof(ackmsg)) && (strcmp
(mesgarray,ackmsg)==0))
{
printf("Got the ack from server\n");
break;
}
}
}
}


The server code :

#include <stdio.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/un.h>
#include <unistd.h>

#define SERVER_PATH "serv_sock"
int main()
{

int sock = 0;
int clientlen = 0;
int recvsize = 0;
int sendsize = 0;
struct sockaddr_un server, client;
char onebytemesg ;

char ackmesg[] = "OK";
sock = socket (AF_UNIX, SOCK_DGRAM, 0);
unlink (SERVER_PATH);

memset (&server,0, sizeof (server));
server.sun_family = AF_UNIX;

strcpy (server.sun_path, SERVER_PATH);

if (bind (sock, (struct sockaddr *)&server, sizeof (server)) < 0)
{
perror("bind:");
return;
}

clientlen = sizeof (client);
for (;;)
{
recvsize = recvfrom (sock, onebytemesg, 2, 0, &client,&clientlen);
if (recvsize <0)
{
continue;
}
else
{
if ((recvsize == 1) && (onebytemesg == ' '))
{
sendsize = sendto (sock, ackmesg,strlen(ackmesg), 0, &client,
clientlen);
if ( sendsize <0)
{
break;
}
printf("Success in sending ack mesg\n");
break;
}
}
}
}

Ulrich Eckhardt

2006-03-05, 7:51 am

Gaurav wrote:
> Im using unix domain Datagram socket to meet the following:
> The client will send a one byte mesg (a blank space ) to the server.
> Upon recieving the one byte mesg, the server will send the client a
> "OK" mesg . But, before recieving anything,
> I am getting the --- send::bad address error. Im unable to track why it
> is happening.


> char datatosend = ' ' ; /*one byte blank mesg */


> sendsize = sendto(sockfd, datatosend,2, 0,
> (struct sockaddr*)&servaddr, serverlen);


Somewhere there is the size of the data to send (the '2') but your data
only has size one.

> char onebytemesg ;


> recvsize = recvfrom (sock, onebytemesg, 2, 0, &client,&clientlen);


Same problem here.

Please don't make multiple posts for the same subject. Postings sometimes
need time to appear and this is not a failure.

Uli

--
http://www.erlenstar.demon.co.uk/unix/
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com