Forums > Java

send jpeg thru udp


wk
February 18, 2006 | 10:24 pm

hi,

i need to send jpeg images to the device,
it works using code pasted below, but it is win specific
(and outside max),
im wondering if its possible using java or tweaking jit.broadcast
(although i couldn`t get it to work).

btw: how i can adapt it for terminal use in os x?

i`ll be grateful if someone can have a look,

thanks,
wojciech

// PSP UDP Send
// primarily for sending image data
// limited to 30KB

#include
#include // Sleep
#pragma comment(lib, "wsock32.lib")

#define MAX_DATA_SIZE 30000

// keep UDP file size small to avoid problems

//////////////////////////////////////////////////////////// /

WSADATA g_wsaData;

byte g_image[MAX_DATA_SIZE];
int g_cbImage = 0;

//////////////////////////////////////////////////////////// /

bool ParseIPAddrString(byte ipAddr[4], const char* szIPAddr)
{
int b1, b2, b3, b4;
if (sscanf(szIPAddr, "%d.%d.%d.%d", &b1, &b2, &b3, &b4) != 4)
return false;
ipAddr[0] = b1;
ipAddr[1] = b2;
ipAddr[2] = b3;
ipAddr[3] = b4;
return true;
}

int main(int argc, char* argv[])
{
if (argc != 4)
{
printf("usage: udpsend psp_ip_address port data_filen");
return -1;
}
const char* szIPForward = argv[1];
int port = atoi(argv[2]);
const char* szDataFile = argv[3];

byte ipaddrDest[4];
if (!ParseIPAddrString(ipaddrDest, szIPForward))
{
printf("udpsend: Bad IP addressn");
return -1;
}

// load file
FILE* pf = fopen(szDataFile, "rb");
if (pf == NULL)
{
printf("udpsend: missing data filen");
return -1;
}
fseek(pf, 0, SEEK_END);
g_cbImage = ftell(pf);
fseek(pf, 0, SEEK_SET);
if (g_cbImage < = 0)
{
printf("udpsend: data file too smalln");
fclose(pf);
return -1;
}
if (g_cbImage > MAX_DATA_SIZE)
{
printf("udpsend: data file too big (limited to %d)n",
MAX_DATA_SIZE);
fclose(pf);
return -1;
}
if (fread(g_image, g_cbImage, 1, pf) != 1)
{
printf("udpsend: data read errorn");
fclose(pf);
return -1;
}
fclose(pf);

// WiFi send

if (WSAStartup(MAKEWORD(1,1), &g_wsaData) != 0) // windows specific
{
printf("udpsend: winsock init errorn");
return -1;
}

// general socket stuff
// send data as on big UDP send to a specific IP address

SOCKET hSock = socket(AF_INET, SOCK_DGRAM, 0);
if (hSock == INVALID_SOCKET)
{
printf("udpsend: socket create errorn");
goto cleanup;
}

SOCKADDR_IN sinDest;
sinDest.sin_family = AF_INET;
sinDest.sin_port = htons(port);
sinDest.sin_addr.S_un.S_un_b.s_b1 = ipaddrDest[0];
sinDest.sin_addr.S_un.S_un_b.s_b2 = ipaddrDest[1];
sinDest.sin_addr.S_un.S_un_b.s_b3 = ipaddrDest[2];
sinDest.sin_addr.S_un.S_un_b.s_b4 = ipaddrDest[3];

if (sendto(hSock, (char*)g_image, g_cbImage, 0,
(struct sockaddr*)&sinDest, sizeof(sinDest)) != g_cbImage)
{
printf("udpsend: sendto errorn");
}

// that’s all folks…

cleanup:
WSACleanup(); // windows specific

return 0;
}


February 18, 2006 | 10:35 pm

Hi Wojciech,
What is receiveing your JPEGs? This is kind of funny ’cause I’m
rewriting a jpeg/png uploaded in java at this very moment. I used to
have code that did it, but I lost it when my hard drive crashed.

So, the way I did it before was to use ssh through the terminal using
key-authentication. With ssh you can call commands on the remote
server. I was calling a PHP script that took a jpeg stream and some
meta data. This script then posted the jpeg to a webpage. It worked
really well.

In java, you can’t pipe processes like you can when typing in the
terminal, so I would call:

cat jpegfilename.jpg

from java using the terminal interface and grab the stdout and in java
code "pipe" the stdout into the stdin of:

ssh login@server phpscript

Hope this helps.
best,
wes


Viewing 2 posts - 1 through 2 (of 2 total)