I'll start by saying this is an assignment for a class - so I'm not looking for a complete solution - just some guidance on a few questions. I am a C novice at best.
My assignment is to write a program using UDP/IP to send a file from one machine to another within our network. I've got the mechanics of sending a char message from one machine to the next so I'm working on figuring out how to read, parse, add the header and CRC information prior to sending.
What I have so far....
File: send.c
Code:
#include <sys/types.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#define MSG "This is a test message!"
#define BUFMAX 100
//Command Arguments:
// [0] - send
// [1] - destination machine
// [2] - destination port number
// [3] - file to be sent
int main(int argc, char* argv[]){
int sk;
char buf[BUFMAX];
FILE *fp;
fp = fopen(argv[3],"r");
fread(buf, BUFMAX, 1, fp);
// My question is in this area...how do I get binary values?
int k;
for(k=0;k<BUFMAX;k++){
printf("The buffer contains %d\n",(char *)buf[k]);
}
struct sockaddr_in remote;
struct hostent *hp;
sk = socket(AF_INET, SOCK_DGRAM, 0);
remote.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if(hp==NULL){
printf("Can't find hostname. %s\n", argv[1]);
exit(1);
}
bcopy(hp->h_addr, &remote.sin_addr.s_addr, hp->h_length);
remote.sin_port = ntohs(atoi(argv[2]));
dlsendto(sk, MSG, strlen(MSG)+1, 0, &remote,
sizeof(remote),0);
read(sk,buf,BUFMAX);
printf("%s\n", buf);
close(sk);
}
What I'm trying to figure out is this....I need to read in the file, parse it into a 100 bit message, add a header and CRC-16 codes to create a frame, then send it through the socket to be validated, assembled and saved on the other side.
I can read the file, but when I output the values I'm getting characters instead of binary. Is there another way to display the data so I can validate what's being passed? I've spent a great deal of time looking through articles on Binary files, text to binary, and can't seem to find an example that explains what I'm doing wrong.
Any guidance would be great.