Sending datagrams using UDP

I have completed the program, but I do not think it is being sent to the right server. It should be sent to blitz.cs.niu.edu using port 9045. The server should respond back with whether the credit card is valid or not and an authorization code. The whole program gets a card number, card name, expiration date, and amount, but I left that portion out to make it easier to look through. All that really matters is the message being sent is in a variable called datagram. I think I am just sending it to myself but I am not sure? How would I go about sending to blitz.cs.niu.edu? Thank you in advance.

//Make datagram from credit card info
strcpy(datagram, cardNumber);
strcat(datagram, ":");
strcat(datagram, expirationDate);
strcat(datagram, ":");
strcat(datagram, amount);
strcat(datagram, ":");
strcat(datagram, cardName);

int sock;
//Structure for address of server
struct sockaddr_in myaddr;

//Create the socket
if((sock=socket(AF_INET, SOCK_DGRAM, 0))<0)
{
perror("Failed to create socket");
exit(EXIT_FAILURE);
}

//Construct the server sockaddr_ structure
memset(&myaddr, 0, sizeof(myaddr));
myaddr.sin_family=AF_INET;
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
myaddr.sin_port=htons(9045);

if(bind(sock,( struct sockaddr *) &myaddr, sizeof(myaddr))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}

//send the message to server
datagramLength=strlen(datagram);
if(sendto(sock, datagram, strlen(datagram), 0, (struct sockaddr *)&myaddr, sizeof(myaddr))!=datagramLength)
{
perror("Mismatch in number of bytes sent");
exit(EXIT_FAILURE);
}

//Receive the datagram back from server
addrLength=sizeof(myaddr);
if((received=recvfrom(sock, buffer, 256, 0, (struct sockaddr *)&myaddr, &addrLength)) != datagramLength)
{
perror("Mismatch in number of bytes received");
exit(EXIT_FAILURE);
}

buffer[received]='\0';
cout << "Server (" << inet_ntoa(myaddr.sin_addr) << ") echoed:
" << buffer << endl;

close(sock);

//Get card holder name again
cout << "Enter card holder name (or quit): ";
cin.getline(cardName,64);
}
return 0;
}







Read comments:

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
$
Last edited on
Topic archived. No new replies allowed.