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 116 117 118
|
#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>
#include <iostream>
#include <string>
#include <netdb.h>
#define MYPORT 4950 // portos de comunicação do emissor e do receptor
#define SERVERPORT 4950
#define MAXBUFLEN 100 // embora a mensagem possa conter até 100 caracteres, este exemplo apenas troca mensagens de 1 caracter + '\0'
using namespace std;
char ip_emissor[]="127.0.0.1"; //endereço ip do host que vai funcionar como emissor
char ip_receptor[]="127.0.0.1"; //endereço ip do host que vai funcionar como receptor
char receiver(){
int sockfd;
struct sockaddr_in my_addr; // endereço ip do receptor(obtido pela aplicação do host onde é executada)
struct sockaddr_in their_addr; // endereço ip do emissor(obtido pela aplicação do host onde é executada)
socklen_t addr_len;
int numbytes;
char buf[MAXBUFLEN];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
//cout << "Pacote recebido de "<< inet_ntoa(their_addr.sin_addr) << endl;
buf[numbytes] = '\0';
//cout << buf << endl;
close(sockfd);
//Return list:
//'0' se o pacote não é proveniente do ip_emissor
//cout<<"ip do emissor actual : "<<inet_ntoa(their_addr.sin_addr)<<endl;
//cout<<"ip do emissor esperado: "<<ip_emissor<<endl;
if(strcmp(inet_ntoa(their_addr.sin_addr),ip_emissor)!=0){
cout<<"Pacote ignorado\n";return '0';
}else return buf[0];
}
void sender(char c){
int sockfd;
struct sockaddr_in their_addr; // connector's address information
struct hostent *he;
int numbytes;
char caracter[2]={c, '\0'};
if ((he=gethostbyname(ip_receptor)) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(SERVERPORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
if ((numbytes = sendto(sockfd, caracter, strlen(caracter), 0,
(struct sockaddr *)&their_addr, sizeof their_addr)) == -1) {
perror("sendto");
exit(1);
}
printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr));
close(sockfd);
}
int main(){
char mode;
int i,numberofchar=10;
cout<<"(s) to send; (r) to receive.\nEnter mode: ";cin>>mode;
if(mode=='s'){
cout<<"Send mode ...\n";
for(i=0;i<numberofchar;i++){
sender('x');
sleep(1);
};
}
else{
cout<<"Receive mode ...\n";
for(i=0;i<numberofchar;i++){
cout<<receiver();
};
cout<<endl;
}
return 0;
}
|