Re: how to wait for socket communications
- From: Ananya <Ananya@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 20 May 2007 23:06:01 -0700
Also, when I read the file of chars or bytes, can I quickly make an array of
chars or bytes out of it?
Thanks!
"Ananya" wrote:
Thanks, I really appreciate all your help..
Well, in C++ I am converting the unsigned chars to chars, but I don't know
how to communicate the chars to Java.
(I actually posted another question: how to communicate unsigned char* to
Java.)
I wish that in Java I would be able to read a file of bytes rather than a
file of chars, because it seems that C++ chars are the same as Java bytes.
So, can I make a file of chars in C++ and read it as a file of bytes in Java?
Thanks again!
"Ali" wrote:
On May 20, 9:59 am, Ananya <Ana...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Thank you so much for your detailed answer!
Well, actually at this point I am ready to try out your file I/O suggestion.
The picture information is an unsigned char*, with first all the red values
listed for each pixel, and then all green values, and finally all the blue
values. So if I can just make a file out of that, the communication from C++
to Java might actually be pretty fast.
Could you please tell me where I can learn about the file I/O method? It's
probably very simple, I am just a beginner at it.
Thanks!
"Ali" wrote:
On May 17, 11:32 pm, Ananya <Ana...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
No, that's exactly what I want to change in my programs. I no longer want to
use JNI, since there are some disadvantages such as not being able to destroy
the JVM when the Java program gets closed, and ending up with memory leaks.
So I was trying to call the Java native executable from my C++ program and
to use socket communication between my C++ program and my Java program.
Unfortunately, in connection with getting advised not to use sockets, my
original question about how to wait for socket communications both in my C++
program and in my Java program was never answered.
May I still receive an answer for my original question at the beginning of
this topic?
Thanks!
"Alexander Nickolov" wrote:
Well, naturally I assumed the Java program would use JNI
for retrieving the file data via a C/C++ DLL...
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnicko...@xxxxxxxx
MVP VC FAQ:http://vcfaq.mvps.org
=====================================
"Ali" <abdulra...@xxxxxxxxx> wrote in message
news:1179310425.550307.225650@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On May 15, 7:53 pm, Ananya <Ana...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Ok, thanks! But where can I learn about memory-mapped files?
You said, you suggested that somewhere. Did you write something about it
or
can you refer me to a turorial? Sorry, but this is new for me.
"Alexander Nickolov" wrote:
I think OP is worried about performance and housekeeping
(not leaving temporary files around). However, with memory-
mapped file you don't need an actual file to back the mapping -
there's a standard approach of using the swap file for backing
the data. Then even file I/O may not occur and all communication
be in memory. Memory-mapped files should really be considered
in this context as memory allocation technique, not file I/O.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnicko...@xxxxxxxx
MVP VC FAQ:http://vcfaq.mvps.org
=====================================
"Ali" <abdulra...@xxxxxxxxx> wrote in message
news:1179210849.716145.295730@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On May 14, 10:55 pm, Ananya <Ana...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I am thinking of using sockets, because this was suggested in the
last
post
to my question "Connecting Java to C++" in the Java Programming
Forum at
forum.java.sun.com, the exact link
being:http://forum.java.sun.com/thread.jspa?threadID=5133663&tstart=0.
I am not sure why Alexander thought that sockets are not the right
tool.
I
think that the advantage using sockets might be that it is faster
than
writing a file, since you can communicate a whole array.
Thanks for all your communications!
"Ali" wrote:
On May 14, 9:02 am, Ananya <Ana...@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
I am trying to do socket communications between my C++ and my
Java
program.
I created a Java program for drawing curves and I connected it
to
Adobe's
C++ program for making a Photoshop plugin.
I am calling ShellExecuteEx for calling my Java program from
C++, and
it
brings up a separate window.
First I would like to communicate the Photoshop picture
information
into my
Java window. Originally I tried to to that with the
lpParameters
string in
ShellExecuteInfo, but it is too slow to build this string.
So now I am trying to communicate this with socket
communication. I
first
create a server in C++ before calling ShellExecuteEx, then I
create a
client
in Java in the main method, but how can I wait in C++ for this
Java
client to
be created before calling accept and sending the Photoshop
picture
information?
I know I could just wait for a few seconds to make sure that my
Java
program
came up by calling:
if ( ei.hProcess != NULL )
{
WaitForSingleObject(ei.hProcess, 10*1000 );}
But I would rather not lose any time and just wait exactly until
the
Java
client is created.
And how can I wait in Java before receiving the picture
information?
Finally, how can I wait in C++ for the Java program to close, at
which point
the information of curves that were drawn in Java is sent to C++
(to
be
received for the workpath in Photoshop)?
Thanks for your time looking at this.
Hmm, i though someone else will jump in to make this point clear.
As
pointed by Alexander that using socket is not a good design
approach.
His suggestion for mapped file is indeed flexible and commercial
grade
solution BUT you can do that with simple file operation as i told
in
previous thread. Why simple file I/O cant be your solution? and
what
makes you think that sockets are good to use?
ali
Snip:
I am not sure why Alexander thought that sockets are not the right
tool.
I
think that the advantage using sockets might be that it is faster
than
writing a file, since you can communicate a whole array.
You must be kidding;-) BTW sockets are also no more than files with
over head of clumsy protocol (tcp/udp anyother) on the top . Yeah,
sockets can work and if you familiar with that then go head and do
it.
I bet socket design can't be never faster then file I/O given the
facts that you will doing all the stuff on single machine. If you are
not familiar with files thats another story but usually writing or
reading to file wont take more than 3 to 4 lines. Easy peasy
Japanese ;-)
ali
Snip form Alexander:
I think OP is worried about performance and housekeeping (not leaving
temporary files around).
But you can always delete the file once you are done. BTW the data
that OP wants to store is few Kbs anyway.
Then even file I/O may not occur and all communication be in memory.
Memory-mapped files should really be considered in this context as memory
allocation >technique, not file I/O.
IIRC zwCreateFile file be called at the end to associate the mapped
memory with an address space ;-) maybe i'm missing something here. I
think file I/O is more appropriate for non-programmer and its easy to
do. Where as mapping file in C++ and reading that one in Java would be
another task;-) but worth doing for robust design.
http://support.microsoft.com/kb/q142377/
ali
Snip:
May I still receive an answer for my original question at the beginning of this topic?
l0l
Your server () should do something like:
YourServer{
int j ,i=0;
int sockfd;
struct sockaddr_in dest;
int flag =0;
int clflag =0;
char buffer[] = "This String is from server Dude! \n\0";
char rbuffer[256];
int clientfd=0;
struct sockaddr_in client_addr;
int addrlen;
/* create socket , same as client */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&dest, sizeof(dest));
dest.sin_family =AF_INET;
dest.sin_port = htons(8889);
dest.sin_addr.s_addr = INADDR_ANY;
/* Assign a port number to socket */
j = bind(sockfd, (struct sockaddr*)&dest, sizeof(dest));
listen(sockfd, 20);
printf("\nWaiting for client...........");
while(1)
{
printf("\n %d ) Waiting for connection..........." , i++);
addrlen = sizeof(client_addr);
clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen);
/* Receive message from the server and print to screen */
bzero(rbuffer, 256);
recv(clientfd, rbuffer, sizeof(rbuffer), 0);
...
read more »
glad to hear that you are considering this option;-) found this
[ http://www.codersource.net/cpp_file_io.html ] quick tutorial for c++
file handling.
#include<fstream.h>
int main()
{
char* str = new char[256];
/*
Now do what ever you want to do with your str , i mean putting your
pixel values in that. Following code expects char* so if you really
want to use unsigned char* then use reinterpret_cast function for
conversion. Though casting is debatable issue but i hope should not
matter in your case and if it is an issue to you then search for
"unsigned char to char".
*/
fstream file_op("c:\\test_file.txt",ios::in);
- Follow-Ups:
- References:
- Re: how to wait for socket communications
- From: Ali
- Re: how to wait for socket communications
- From: Ananya
- Re: how to wait for socket communications
- From: Ali
- Re: how to wait for socket communications
- From: Alexander Nickolov
- Re: how to wait for socket communications
- From: Ananya
- Re: how to wait for socket communications
- From: Ali
- Re: how to wait for socket communications
- From: Alexander Nickolov
- Re: how to wait for socket communications
- From: Ananya
- Re: how to wait for socket communications
- From: Ali
- Re: how to wait for socket communications
- From: Ananya
- Re: how to wait for socket communications
- From: Ali
- Re: how to wait for socket communications
- From: Ananya
- Re: how to wait for socket communications
- Prev by Date: Re: how to wait for socket communications
- Next by Date: Re: how to wait for socket communications
- Previous by thread: Re: how to wait for socket communications
- Next by thread: Re: how to wait for socket communications
- Index(es):
Relevant Pages
|