CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2003
    Posts
    5

    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.

  2. #2
    Join Date
    Dec 2002
    Posts
    47
    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

  3. #3
    Join Date
    Oct 2003
    Posts
    5
    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

  4. #4
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    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
  •  





Click Here to Expand Forum to Full Width

Featured