damalo
02-25-2005, 03:37 PM
Howdy folks,
Im putting together a protocol to send images over UDP at the moment. Not too easy but im having a fundamental problem.
Im reading an image file into a buffer and trying to send that over UDP. The buffer data is quite small as the image is only 1px in size.
Then using another function im outputting the buffer to a new image. Which works fine. The problem is sending that over UDP to another client.
When i go to send this buffered image info it sends only the first 4 bits. After which i think there is some stop line which the sendto seems to be interpreting as end of the buffer - so the receiving client only picks up a fraction of the intended payload. Any ideas how to get around this?
ill paste my code here...
(this code example doesnt include the function to output the buffer data to a new image - but trust me that part is working)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#define REMOTE_SERVER_PORT 1501
#define MAX_MSG 100
int main(int argc, char *argv[]) {
int sd, rc, i;
struct sockaddr_in cliAddr, remoteServAddr;
struct hostent *h;
FILE* filein;
long lSize;
char *buffer;
/* check command line args */
if(argc<3) {
printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
exit(1);
}
/* get server IP address (no check if input is IP address or DNS name */
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s' \n", argv[0], argv[1]);
exit(1);
}
printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
remoteServAddr.sin_family = h->h_addrtype;
memcpy((char *) &remoteServAddr.sin_addr.s_addr,
h->h_addr_list[0], h->h_length);
remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
/* socket creation */
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd<0) {
printf("%s: cannot open socket \n",argv[0]);
exit(1);
}
/* bind any port */
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
if(rc<0) {
printf("%s: cannot bind port\n", argv[0]);
exit(1);
}
//open file
filein = fopen("image.jpg","rb+");
//get its size
fseek(filein, 0, SEEK_END);
lSize = ftell(filein);
rewind (filein);
//malloc the buffer big enough for image data
buffer = (char*) malloc (lSize);
//read image data into buffer
fread (buffer,1,lSize,filein);
//...create duplicate image (works ok)
// send data - Have also tried sizeof(buffer)
for(i=2;i<argc;i++) {
rc = sendto(sd, buffer, sizeof(buffer)+1, 0,
(struct sockaddr *) &remoteServAddr,
sizeof(remoteServAddr));
if(rc<0) {
printf("%s: cannot send data %d \n",argv[0],i-1);
close(sd);
exit(1);
}
}
return 1;
}
Cheers!
Im putting together a protocol to send images over UDP at the moment. Not too easy but im having a fundamental problem.
Im reading an image file into a buffer and trying to send that over UDP. The buffer data is quite small as the image is only 1px in size.
Then using another function im outputting the buffer to a new image. Which works fine. The problem is sending that over UDP to another client.
When i go to send this buffered image info it sends only the first 4 bits. After which i think there is some stop line which the sendto seems to be interpreting as end of the buffer - so the receiving client only picks up a fraction of the intended payload. Any ideas how to get around this?
ill paste my code here...
(this code example doesnt include the function to output the buffer data to a new image - but trust me that part is working)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#define REMOTE_SERVER_PORT 1501
#define MAX_MSG 100
int main(int argc, char *argv[]) {
int sd, rc, i;
struct sockaddr_in cliAddr, remoteServAddr;
struct hostent *h;
FILE* filein;
long lSize;
char *buffer;
/* check command line args */
if(argc<3) {
printf("usage : %s <server> <data1> ... <dataN> \n", argv[0]);
exit(1);
}
/* get server IP address (no check if input is IP address or DNS name */
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s' \n", argv[0], argv[1]);
exit(1);
}
printf("%s: sending data to '%s' (IP : %s) \n", argv[0], h->h_name,
inet_ntoa(*(struct in_addr *)h->h_addr_list[0]));
remoteServAddr.sin_family = h->h_addrtype;
memcpy((char *) &remoteServAddr.sin_addr.s_addr,
h->h_addr_list[0], h->h_length);
remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);
/* socket creation */
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd<0) {
printf("%s: cannot open socket \n",argv[0]);
exit(1);
}
/* bind any port */
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
if(rc<0) {
printf("%s: cannot bind port\n", argv[0]);
exit(1);
}
//open file
filein = fopen("image.jpg","rb+");
//get its size
fseek(filein, 0, SEEK_END);
lSize = ftell(filein);
rewind (filein);
//malloc the buffer big enough for image data
buffer = (char*) malloc (lSize);
//read image data into buffer
fread (buffer,1,lSize,filein);
//...create duplicate image (works ok)
// send data - Have also tried sizeof(buffer)
for(i=2;i<argc;i++) {
rc = sendto(sd, buffer, sizeof(buffer)+1, 0,
(struct sockaddr *) &remoteServAddr,
sizeof(remoteServAddr));
if(rc<0) {
printf("%s: cannot send data %d \n",argv[0],i-1);
close(sd);
exit(1);
}
}
return 1;
}
Cheers!