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
|
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <iomanip>
#include <iostream>
#include <string>
int main(void){
//Structure for address of server
struct sockaddr_in myaddr;
int sock;
//Construct the server sockaddr_ structure
memset(&myaddr, 0, sizeof(myaddr));
myaddr.sin_family=AF_INET;
//-----------------------------------------------------------------
// This line is correct because you want to bind an address on
// you machine
//-----------------------------------------------------------------
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
//-----------------------------------------------------------------
// This is the port on your machine that you are going to bind.
// This doesn't matter because the server just returns to the port
// it received the request on. Port zero is any port.
//-----------------------------------------------------------------
//myaddr.sin_port=htons(9045);
myaddr.sin_port=htons(0);
//Create the socket
if((sock=socket(AF_INET, SOCK_DGRAM, 0))<0) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}
if(bind(sock,( struct sockaddr *) &myaddr, sizeof(myaddr))<0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
//-----------------------------------------------------------------
// You need to add the code that takes the blitz.cs.niu.edu name
// and translates it to the network address.
// Google "getaddrinfo examples"
// or http://www.logix.cz/michal/devel/various/getaddrinfo.c.xp
//-----------------------------------------------------------------
std::string to("blitz.cs.niu.edu");
// Your code goes here ...
//-----------------------------------------------------------------
// But now you want to send a message to blitz.cs.niu.edu so you
// must change the value of myaddr.sin_addr.s_addr to be the network
// address of that server you want to talk to. Using nslookup I
// found that the IP address of blitz.cs.niu.edu is 131.156.145.90.
// Use inet_pton to turn 131.156.145.90 into an integer used in the
// socket functions
//-----------------------------------------------------------------
inet_pton(AF_INET,"131.156.145.90",&myaddr.sin_addr.s_addr);
//-----------------------------------------------------------------
// Here is where the port matters, because you know the server is
// listening on that port. So we must set the port number here
// for the out going message.
//-----------------------------------------------------------------
myaddr.sin_port=htons(9045);
//-----------------------------------------------------------------
// I don't know where the exact string is but this returns a message
//-----------------------------------------------------------------
std::string s("12345678910:5/15:300.00:Visa");
//send the message to server
if(sendto(sock, s.c_str(), s.size(), 0, (struct sockaddr *)&myaddr, sizeof(myaddr))!=s.size()) {
perror("Mismatch in number of bytes sent");
exit(EXIT_FAILURE);
}
//Receive the datagram back from server
int addrLength(sizeof(myaddr)),received(0);
char buffer[256] = {0};
if((received=recvfrom(sock, buffer, 256, 0, (sockaddr *)&myaddr, (socklen_t*)&addrLength)) < 0) {
perror("Mismatch in number of bytes received");
exit(EXIT_FAILURE);
}
buffer[received]='\0';
std::cout << "Server (" << inet_ntoa(myaddr.sin_addr) << ") echoed: " << buffer << std::endl;
close(sock);
return 0;
}
|
$ ./a.out
Server (131.156.145.90) echoed: Mon Nov 18 22:58:18 CST 2013: 12345678910: card type unknown
$ |