The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Special Forums > IP Networking
Google UNIX.COM


IP Networking Questions involving TCP/IP, Routers, Hubs, Network protocols, etc go here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sendto invalid argument Puntino IP Networking 0 06-16-2008 02:06 AM
Permission denied with sendto in Ubuntu Puntino Linux 0 06-12-2008 02:43 PM
sendto failing "resource temporarily unavailable" Naanu SUN Solaris 6 03-16-2007 01:41 PM
sendto in packet socket Rakesh Ranjan High Level Programming 5 09-02-2005 04:32 AM
Problem in address field of recvfrom Rakesh Ranjan High Level Programming 0 08-30-2005 09:41 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-16-2008
Registered User
 

Join Date: Jun 2008
Posts: 1
Stumble this Post!
Error in sendto and recvfrom

Hi guys below is the code which I wrote to send some message to the server and the receive a a reply message from the server.

Initially, I wrote one program to sendto and one program to receive from, it is working fine seperately. When I tried coding sendto and recvfrom in a sinlge program then the problem started. I am able to send but not able to receive any reply.

*******************The code for sendto and recvfrom*********

/* Program to send packets using UDP*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MAXBUFLEN 300 // Maximum message length

int main(argc,argv)
int argc; // argc for specifying the receving computer IP address
char **argv; // argv for specifying the receving computer port address
{
int sockfd, nw_yr,num_bytes;
unsigned short port;
socklen_t addr_len;
char buf[MAXBUFLEN];
//int nw_yr; int num_bytes;
port=htons(atoi(argv[2]));



struct new_struct
{
u_int8_t MessageType;
u_int8_t Offset;
u_int16_t Year;
u_int8_t Month;
u_int8_t Day;
u_int8_t Hour;
u_int8_t Mins;
u_int8_t Secs;
};
struct new_struct *myptr;
myptr=(struct new_struct *)buf;
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // receving computer's address information

//create socket and do some error checking
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) //do some error checking
{
perror("socket"); // print error with error number
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port =port;
their_addr.sin_addr.s_addr = inet_addr(argv[1]);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
//addr_len=sizeof(struct sockaddr);

strcpy(buf,"Hope so this program works");
printf(" Sending data to the socket.\n");
if((num_bytes=sendto(sockfd, buf, (strlen(buf)+1), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)))==-1)
{
perror("sendto");
exit(1);
}

printf("Data sent %d bytes to %s at %02d:%02d:%02d \n", num_bytes, inet_ntoa(their_addr.sin_addr), myptr->Hour, myptr->Mins, myptr->Secs);
//printf("Data sent %d bytes to %s\n", num_bytes, inet_ntoa(their_addr.sin_addr));

// ********** Receive back again*********************

addr_len = sizeof(struct sockaddr);
if ((num_bytes=recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
nw_yr=ntohs(myptr->Year);
buf[num_bytes-1]='\0';
printf("\n At %02d:%02d:%02d on %02d:%02d:%d message received : \n", myptr->Hour, myptr->Mins, myptr->Secs, myptr->Day, myptr->Month, nw_yr);
printf("<%s>\n", buf+myptr->Offset);
printf("\n");

printf("Closing the socket\n");
close(sockfd);
printf("You have done it, Socket closed\n");
return 0;
}



Please tell me if I have done some mistakes, Why am i not able to receive anything from the server.
Reply With Quote
Google The UNIX and Linux Forums
Forum Sponsor
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0