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....");
}
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....");
}