Advertisement
| Hall of Fame |
|
[x]
Posted via EE Mobile
|
||
Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
||
| Question |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: |
SENDER CODE:
____________________
#define PORT 2080
#define DEST_ADDR "255.255.255.255"
int main(int argc, char *argv[])
{
int sockfd;
int broadcast=1;
struct sockaddr_in sendaddr;
struct sockaddr_in recvaddr;
int numbytes;
if((sockfd = socket(PF_INET,SOCK_DGRAM,0)) == -1)
{
perror("sockfd");
exit(1);
}
if((setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,
&broadcast,sizeof broadcast)) == -1)
{
perror("setsockopt - SO_SOCKET ");
exit(1);
}
/*if((setsockopt(sockfd,SOL_SOCKET,SO_DONTROUTE,
&broadcast,sizeof broadcast)) == -1)
{
perror("setsockopt - SO_DONTROUTE");
exit(1);
}*/
sendaddr.sin_family = AF_INET;
sendaddr.sin_port = PORT;
sendaddr.sin_addr.s_addr = INADDR_ANY;
memset(sendaddr.sin_zero,'\0',sizeof sendaddr.sin_zero);
if(bind(sockfd, (struct sockaddr*) &sendaddr, sizeof sendaddr) == -1)
{
perror("bind");
exit(1);
}
recvaddr.sin_family = AF_INET;
recvaddr.sin_port = PORT;
recvaddr.sin_addr.s_addr = inet_addr(DEST_ADDR);
memset(recvaddr.sin_zero,'\0',sizeof recvaddr.sin_zero);
while((numbytes = sendto(sockfd, "abcd", 4 , 0,
(struct sockaddr *)&recvaddr, sizeof recvaddr)) != -1)
{
printf("Sent a packet");
sleep(1000);
}
perror("sendto");
exit(1);
close(sockfd);
return 0;
}
____________________
RECEIVER CODE:
____________________
#include "packetize.h"
#define PORT 2080
#define DEST_ADDR "10.50.41.208" //Server addr
int main(int argc, char *argv[])
{
int sockfd;
char buf[10];
struct hostent *he;
struct sockaddr_in sendaddr;
//struct sockaddr_in recvaddr;
int numbytes;
int addr_len;
if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}
printf("Socket created\n");
if ((he=gethostbyname(DEST_ADDR)) == NULL)
{ // get the host info
herror("gethostbyname");
exit(1);
}
printf("Host found\n");
sendaddr.sin_family = AF_INET;
sendaddr.sin_port = PORT;
sendaddr.sin_addr = *((struct in_addr *)he->h_addr);
memset(sendaddr.sin_zero,'\0', sizeof sendaddr.sin_zero);
addr_len = sizeof sendaddr;
if ((numbytes = recvfrom(sockfd, buf, sizeof buf, 0,
(struct sockaddr *)&sendaddr, (socklen_t *)&addr_len)) == -1)
{
perror("recvfrom");
exit(1);
}
printf("%s",buf);
return 0;
}
____________________
|