Error in sendto and recvfrom


 
Thread Tools Search this Thread
Special Forums IP Networking Error in sendto and recvfrom
# 1  
Old 06-16-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print Error in Console and both Error & Output in Log file - UNIX

I am writing a shell script with 2 run time arguments. During the execution if i got any error, then it needs to redirected to a error file and in console. Also both error and output to be redirected to a log file. But i am facing the below error. #! /bin/sh errExit () { errMsg=`cat... (1 Reply)
Discussion started by: sarathy_a35
1 Replies

2. IP Networking

NS3 SendTo

Hi, I am working to implement a basic WiFi infrastructure in NS3. I have placed 16 APs in grid and one client is moving using random walk. Client and AP can communicate through message exchange. However, when I try a message of size more than 30 bytes, it is crashed. Please help me if anyone have... (0 Replies)
Discussion started by: dandapat
0 Replies

3. UNIX for Dummies Questions & Answers

> 5 ")syntax error: operand expected (error token is " error

im kinda new to shell scripting so i need some help i try to run this script and get the error code > 5 ")syntax error: operand expected (error token is " the code for the script is #!/bin/sh # # script to see if the given value is correct # # Define errors ER_AF=86 # Var is... (4 Replies)
Discussion started by: metal005
4 Replies

4. IP Networking

recvfrom() causes segmentation fault

hi all.... i wrote a program with udp sockets.. when it reaches the function 'recvfrom()' it crashes with a core dump. the gdb trace says the following error msg... Core was generated by `/opt/fmp'. Program terminated with signal 6, Aborted. #0 0x00000055555b18e4 in recvfrom () from... (1 Reply)
Discussion started by: lameduck0123
1 Replies

5. AIX

nim mksysb error :/usr/bin/savevg[33]: 1016,07: syntax error

-------------------------------------------------------------------------------- Hello, help me please. I am trying to create a mksysb bakup using nim. I am geting this error, how to correct it ? : Command : failed stdout: yes stderr: no... (9 Replies)
Discussion started by: astjen
9 Replies

6. IP Networking

sendto invalid argument

Hi I lost a lot of time in understanding the message "sendto Invalid argument" when I execute the following code. This code is a simple UDP sender improved with some reliability feature. My goal is to send a file. I've reported only the code which may be useful. Can anyone help me? Thank you... (0 Replies)
Discussion started by: Puntino
0 Replies

7. Ubuntu

Permission denied with sendto in Ubuntu

I hope to post in the right place, otherwise I apologize for this I wrote a UDP sender and a UDP receiver, my goal is to transmit a file from th sender to the receiver. I'm using Ubuntu 8.04 Compiling goes well but when I execute the sender, its "sendto" gives me the error "Permission denied" .... (0 Replies)
Discussion started by: Puntino
0 Replies

8. Solaris

sendto failing "resource temporarily unavailable"

Well, I am not even sure if its failing, cause at the other end I have a select call and it wakes up and reads the data I sent fine. Ok here is the issue, I have a UDP socket(non blocking) through which I push some data to another port. At the other end I have select loop, waiting for this data.... (6 Replies)
Discussion started by: Naanu
6 Replies

9. Programming

sendto in packet socket

Hi, I have created a packet socket (PF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP)) to catch the ARP packets coming to my machine and send appropriate reply. Now I'm able to recieve the ARP requests using recvfrom but don't know how to send the reply. I looked into man page but I'm not able to... (5 Replies)
Discussion started by: Rakesh Ranjan
5 Replies

10. Programming

Problem in address field of recvfrom

When I'm making two sockets (connectionless) and then sending request from one using sendto and recieving form other using recvfrom I'm able to get the message (buffer field in sendto) but can't get the address of calling client in the value-result field of recvfrom (sockaddr_in* and socklen_t*). ... (0 Replies)
Discussion started by: Rakesh Ranjan
0 Replies
Login or Register to Ask a Question