|
-
October 14th, 2003, 09:28 AM
#1
Network Programming (Java to C)
How would I be able to simply establish a connection between a Java Application on one machine and a C application on the other. I am not that familiar with C code...but I need to be able to send strings from my Java application into an application that a team member of mine is writing in C.
How would I set up the communication on the Java and C side. Do I still use the same ServerSocket and Socket classes on the Java side. If so how do I set up the communication on the C side.
I know if I did Java to Java communication I would set up the client side like:
Socket socket = new Socket (addr, SERVER.PORT);
out = socket.getOutputStream();
in = socket.getInputStream();
then just write to the input and output stream like normal I/O.
and the Server side Using the ServerSocket and Server Class.
-
October 14th, 2003, 09:59 AM
#2
Using BSD style sockets ...
1. create socket using "socket()", specify AF_INET for the address family, IP_PROTO for protocol (I think), and SOCK_STREAM for TCP socket (SOCK_DGRAM for UDP)
2. optionally "bind()" the socket to a local interface.
3. connect the socket to the remote host.
4. use send() to send data and recv() to receive data.
5. calls normally block but you can enable non-blocking IO and use select() to determine which data has arrived or socket is ready to send again.
Plenty of code samples for the above online.
-rick
-
October 14th, 2003, 10:17 AM
#3
Thanks for the help. I found an example that uses read() to read data but it appears that it reads an integer at a time. If i am sending strings from the Java app how can I read this on the client side???
Could you send a link to a good example...I really appreciate it. Thanks
-
October 15th, 2003, 10:58 AM
#4
set up a socket (to make connections) or a serversocket (to recieve connections, depending if your mate is making or receiving the conenctions. if either or both happens, you need both a socket and a server socket
a serversocket, when told to accept(), will turn intoa normal socket when it receives a connection, so from ehre, your code is the same:
sockets have getinputstream and getoutputstream, you can use these when creating the relevant readersand writers:
BufferedReader in
= new BufferedReader(
new InputStreamReader(socket.getInputStream()));
now calling in.readLine() will read a line from your socket. the method blocks until a line is avaliable
PrintWriter out =
new PrintWriter(new BufferedOutputStream(socket.getOutputStream()), true);
the true is important, as it will automatically cause the PW to flush the buffer whenever you do an out.println("mystring")
without buffer flushing, your program will only send the info when it feels like it, usually when teh buffer is full
note, for asyncronous operation (i.e. sending messages and receiving messages in no particular order) you must use 2 threads, or investigate the nio packages
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|