java and c++ socket communication _ dr dobb's1

Upload: vicente-gutierrez

Post on 06-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Java and C++ Socket Communication _ Dr Dobb's1

    1/6

    /03/12 Java and C++ Socket Communication Dr Dobb's

    dobbs.com/jvm/222900697

    Cloud Mobile Parallel .NET JVM Languages C/C++ Tools Design Testing Web Dev

    JVM LANGUAGESPemalink

    Jaa ad C++ SceCicai

    By Eric Bruno, February 17, 2010

    Po a Commen

    Iegaig C++ aie Wid aicai ih Jaaaicai ia e ce

    Performing all of your application development with asingle language on a single platform may be ideal, but it's

    not always practical. There are times when you may need

    to integrate a new application with a legacy one, andcommunication between the two can be an issue. For

    instance, you may desire to isolate the two applications

    so that the new application's design isn't compromised,

    and the older one can be upgraded later without impactingthe newer application.

    Peia Si

    In the past, I've explored distributed computing solutionsthat have discussed integrating Java and C++ applications

    via the Java Native Interface (JNI), Java Message System

    (JMS), and web services; see the the "Conclusion" sectionat the end of this article. Although these approaches are

    good in the right situations, there may be times where

    these solutions are too complicated or just not ideal. Forinstance, calling into native code from Java via JNI can be

    complex, time-consuming, and error-prone. Using JMS

    requires a JMS provider be licensed, installed, andconfigured. And a web service requires significant

    development and web-based infrastructure.

    Another solution is to use socket-based network

    communication directly between the Java and C++

    applications. Although this is a relatively low-levelapproach, it's still an effective solution. With XML as the

    message protocol between them, you can maintain a

    degree of platform and language independence. Figure 1illustrates this in a very simple way; here we show that a

    C++ application that uses Windows sockets can

    communicate with a Java application that uses Java IO(scenario c) just as easily as in the two homogenous

    examples (scenarios a and b), and with no code changes.

    JVM Lagage Rece Aice

    Project of the Month: Cojac, A Numerical Problem

    SnifferSolving the Configuration Problem for Java Apps

    The JVM As Language Farm Club

    Language of the Month: Kotlin

    A Look at the Main JVM Languages Today

    Welcome Ge. Lg I Regie Beefi

    Search: Site Source Code

    He Aice Ne Bg Sce Cde Dbb' DVD Dbb' TV Webia & Ee

    Me ga ShaeShae

    Stories Blogs

    M Pa

    The Need to Rewrite Established Algorithms

    A Look at the Main JVM Languages Today

    Solving the Configuration Problem for Java Apps

    Project of the Month: Cojac, A Numerical Problem

    Sniffer

    The Pillars of Concurrency

  • 8/2/2019 Java and C++ Socket Communication _ Dr Dobb's1

    2/6

    /03/12 Java and C++ Socket Communication Dr Dobb's

    dobbs.com/jvm/222900697

    Fige 1: A Jaa aicai ca cicae diecih a C++ ic aicai.

    The Sce Si

    Let's explore a sample integration solution that includes a

    Java application that uses java.io to communicate with a

    C++ Windows application that uses Windows sockets. TheWindows application supports three simple requests forthe following data:

    The Windows host name

    The amount of memory installed

    A pseudorandom number

    These requests are simple for illustration only, but inreality they may be requests for data only available from a

    legacy application or a native platform interface. All

    requests and responses are formatted as simple XMLcharacter strings. Specifically, both the client and server

    wait for data and read bytes from the network stream,

    with each full message delineated by a NULL or '\n' character.

    The simple XML request messages are in Example 1. Each

    message has the same basic XML structure, with only the

    request name differing between them.

    Eae 1: The XML ee eage e f hecie, he ee.

    If you need to send data along with each request, simplyadd one or more XML elements to the request message.

    For example, if you want to change the GetMemor

    message to indicate the type of memory (physical or

    virtual) to request, the XML can be changed to look like

    the following:

    The response messages are similar to the requests with

    some obvious changes; see Example 2. The main

    Feaed Re

    Strategy: The UC Identity Crisis

    Strategy: Mobile Device-Borne Malware

    Fundamentals: Windows Azure: Clear Enterprise

    View

    IT Pro Impact: Windows Developer Road Map

    IT Pro Impact: NFC and Mobile Commerce

    123456789

    1011

  • 8/2/2019 Java and C++ Socket Communication _ Dr Dobb's1

    3/6

    /03/12 Java and C++ Socket Communication Dr Dobb's

    dobbs.com/jvm/222900697

    differences are the XML response types, and the inclusion

    of the data being requested.

    Eae 2: The XML ee eage e f

    he ee, i ee cie ee.

    In this sample implementation, the actual data returned in

    each response will vary according to the computer it's runon. Let's begin to dive into the implementation of the

    solution, beginning with the Java client application.

    123Next

    Reaed Readig

    News

    Most Popular

    "Mbie Fi" Deee Dead Cea

    Re T PaaS

    Oiiig Agih Ve GPU

    Pgaig, Which I Be?

    Kicig iPhe A Tie F Bee

    Reiee

    Cei: Oe Sce Cde Qai O Paih Piea

    More News

    Commentary

    On the Web

    Secha Tch 2: Eaded HTML5 Paf

    Vedic Via Sdi 11 Bea: N Read

    f Pie Tie

    CFEgie 3 Na: Cfigai Maagee

    A Wa Seed?Bighce: Hbid HTML5/Naie

    Deee Wi The Da

    More Commentary

    Video

    Slideshow

    Caaciie Cig De

    MPLabX Li

    Wiig Ki Cde (ide)

    Feaed Whieae

    Mobile BI: Ac tionable Intelligence for the Agile

    Enterprise

    Creating the Enterprise-Class Tablet Environment -

    by Yankee Group

    How To Regain IT Control In An Increasingly Mobile

    World - by BlackBerry

    Red Alert: Why Tablet Security Matters - by

    BlackBerry

    New Visual and Wizard-Driven Paradigms for

    Exploring Data and Developing Analytic Workflows

    Feaed Webca

    Best Practices for Estimating the ROI of

    Development Testing

    The Cloud is Real: Are You Ready? Learn how to

    choose the right cloud solution for your business

    Self-Service without the Chaos

    Creating the Foundation for BI Success

    Maximize ROI with Database Consolidation onto

    Private Clouds

    123456789

    1011121314

  • 8/2/2019 Java and C++ Socket Communication _ Dr Dobb's1

    4/6

    /03/12 Java and C++ Socket Communication Dr Dobb's

    dobbs.com/jvm/222900697

    I, Rb

    More Videos

    Me Iigh

    Whie Pae

    How To Regain IT Control In An Increasingly MobileWorld - by BlackBerry

    The BlackBerry PlayBook tablet's Good Bones - byBlackBerry

    More >>

    Re

    Strategy: The UC Identity CrisisResearch: Data Encryption

    More >>

    Webca

    Maximizing performance, ease of use, and high-availability for your Microsoft Environment

    Best Practices for Estimating the ROI ofDevelopment Testing

    More >>

    INFO-LINK

    Free article: Acce

    integration w ith In

    Plus

    Free article on fas

    The Worlds First

    Thirty-Niner

    Lie

    M Subscribe by email RSS

    Sort by oldest first

    THE WEB

    ig a A Ccec

    way of passing data between a producer and a

    at different rates. It ensures that the consumer

    e data with minimal lag.

    GDB Beai i C Sce

    ted to embed GDB breakpoints in C source

    ike this:

    ,\n");

    NT;

    !\n");

    e Ei

    Shig 0

    ce

    Lgi Regie Ce

  • 8/2/2019 Java and C++ Socket Communication _ Dr Dobb's1

    5/6

    /03/12 Java and C++ Socket Communication Dr Dobb's

    dobbs.com/jvm/222900697

    CIO & IT Pfeia

    Black Hat

    BYTE

    Cloud ConnectDark Reading

    Enterprise 2.0

    Enterprise Connect

    Enterprise Eff iciency

    HDI

    InformationWeek

    InformationWeek 500

    InformationWeek 500 Conference

    InformationWeek Events

    InformationWeek Global CIO

    InformationWeek Healthcare

    InformationWeek India

    Sfae Deee

    Dr. Dobb's

    Dr. Dobb's M-Dev

    Dr. Dobb's JournalDr. Dobb's Update

    TechWeb.com

    Web & Digia Pfeia

    Internet Evolution

    Online Marketing Summit

    TechWeb.com

    Gee Officia

    GTEC Ottaw a

    InformationWeek Government

    TechWeb.com

    Veica Mae

    Advanced Trading

    Bank Systems & Technology

    CreateYourNextCustomerInformationWeek Government

    InformationWeek Healthcare

    Insurance & Technology

    Light Reading / Telecom

    The CMO Site

    Wall Street & Technology

    Gae Id Pfeia

    Gamasutra.com

    Game Developers Conference (GDC)

    Independent Games Festival

    Game Developer Magazine

    GDC Europe

    Gba Cicai

    Seice Pide

    4G World

    Heavy Reading

    Heavy Reading Insiders

    Pyramid Research

    Light Reading

    Light Reading India

    Light Reading Mobile

    Light Reading Cable

    Light Reading Europe

    Light Reading Asia

    Ethernet Expo

    TelcoTV

    Tow er Summit

    Light Reading Live & Virtual Events

    M P

    Cable Cat

    Cloud Con

    Digital LifeEvil Bytes

    Informatio

    Interop Blo

    Monkey B

    Over the A

    Personal T

    The Philter

    Valley Wo

    Enabling People and Organiations to Harness the Transformative Power of Tech

    rnel? Because it has a huge attack surface with

    teresting bugs. This presentation (pdf) takes a

    recently reported Linux-kernel exploits .

    he JaaSci Egiend for out-of-browser JavaScript (e.g., server

    knowledge of JavaScript engines is becoming

    Web" >>

  • 8/2/2019 Java and C++ Socket Communication _ Dr Dobb's1

    6/6

    /03/12 Java and C++ Socket Communication Dr Dobb's

    dobbs.com/jvm/222900697

    InformationWeek Reports

    InformationWeek SMB

    Interop

    Mobile Connect

    Network Computing

    No Jitter

    TechWeb.com

    The BrainYard

    GDC China

    Game Career Guide

    Game Advertising Online

    Webinars

    About UBM TechWeb Advertising Contacts Technology Marketing Solutions Contact Us Feedback

    Reprints TechWeb Digital Library / White Papers TechWeb Events Calendar TechWeb.com

    UBM TechWeb Reade Seice

    Terms of Service | Privacy Statement | Copyright 2012 UBM TechWeb, All rights reserved.

    Pow ered by Zend/PHP

    Dr. Dobb's Home Articles News Blogs Source Code Dobb's on DVD Dobb's TV Webinars

    About Us Contact Us Site Map Editorial Calendar