Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

09/03/12

Java and C++ Socket Communication Dr Dobb's

Welcome G e . L g I

Regi e

Be efi

Search:

Site

Source Code

A ic e Mobile

Ne Parallel

B g .NET

ce C de

D bb'

DVD C/C++

D bb' TV Tools

Webi a Design

&E e Testing Web Dev

Cloud

JVM Languages

JVM LANGUAGES
Me g a Sha e
Pe malink

Ja a a d C++ S c e C ica i
By Eric Bruno, February 17, 2010
Po a Commen

I eg a i g C++ a i e Wi d a a ica i ia e c e

ica i

i h Ja a

Performing all of your application development with a single 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, and communication 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 impacting the newer application.

JVM La g age Rece

A ic e

ia S

In the past, I've explored distributed computing solutions that have discussed integrating Java and C++ applications via the Java Native Interface (JNI), Java Message System (JMS), and web services; see the the "Conclusion" section at 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. For instance, 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, and configured. 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-level approach, 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 1 illustrates 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 aand b and with no code changes. ),

Project of the Month: Cojac, A Numerical Problem Sniffer Solving 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

a
Blogs

Stories

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

drdobbs.com/jvm/222900697

1/6

09/03/12

Java and C++ Socket Communication Dr Dobb's

Fig e 1: A Ja a a ica i ca c i h a C++ i c a ica i .

ica e di ec

The S c e S

Vie

A Vide

Let's explore a sample integration solution that includes a Java application that uses j v . oto communicate with a aai C++ Windows application that uses Windows sockets. The Windows application supports three simple requests for the following data: The Windows host name The amount of memory installed A pseudorandom number These requests are simple for illustration only, but in reality 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 XML character 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.

Thi

h' D . D bb' J

Thi h, our issue is devoted to Microsoft technologies. Learn how to create a custom output cache provider in ASP.NET 4, discover parallel queries in .NET w ith PLINQ, explore the task scheduler in Visual Studio's CRT, a d ch e! Dow nload the latest issue today. >>

1 2 3 4 5 6 7 8 9 1 0 1 1
E a c ie ,

<eus Rqet <aeGtotae/ae Nm eHsnm<Nm <Rqet /eus <eus Rqet <aeGteoy/ae Nm eMmr<Nm <Rqet /eus <eus Rqet <aeGtadmubr/ae Nm eRnoNme<Nm <Rqet /eus
e 1: The XML e he e e . e e age e f

he

If you need to send data along with each request, simply add one or more XML elements to the request message. For example, if you want to change the G t e o eMmr message to indicate the type of memory (physical or virtual) to request, the XML can be changed to look like the following:

Fea
?

ed Re

1 2 3 4

<eus Rqet <aeGteoy/ae Nm eMmr<Nm < p Pyia<Tp T e hscl/ e <Rqet /eus

The response messages are similar to the requests with some obvious changes; see Example 2. The main drdobbs.com/jvm/222900697

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
2/6

09/03/12

Java and C++ Socket Communication Dr Dobb's

differences are the XML response types, and the inclusion of the data being requested.

Fea

ed Whi e a e

1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4

Mobile BI: Actionable Intelligence for the Agile Enterprise Creating the Enterprise-Class Tablet Environment by Yankee Group How To Regain IT Control In An Increasingly Mobile <epne Rsos World - by BlackBerry <aeMmrRsos<Nm Nm eoyepne/ae Red Alert: Why Tablet Security Matters - by <oaP scleo Ttlh iaMmr 17215<Ttlhscleo 03012/oaP iaMmr BlackBerry <Rsos /epne New Visual and Wizard-Driven Paradigms for Exploring Data and Developing Analytic Workflows <epne Rsos

<epne Rsos <aeHsnmRsos<Nm Nm otaeepne/ae <otae MSre <Hsnm Hsnm yevr /otae <Rsos /epne

<aeRnoNmeRsos<Nm Nm admubrepne/ae <ubr213/ubr Nme 07<Nme <Rsos /epne


e c ie e age e e e .

Fea
f

ed Webca

E a e 2: The XML e he e e , i e e

In this sample implementation, the actual data returned in each response will vary according to the computer it's run on. Let's begin to dive into the implementation of the solution, beginning with the Java client application.

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

1 2 3 Next

Rece

Pe i

Re a ed Readi g
News Most Popular "M bi e Fi " De e e De a d C ea R e T PaaS O i i i gAg i h Ve GPU P ga i g, Which I Be ? Kic i g iPh e A Ti e F Be e Re i e e C e i :O e S ce C de Q a i O Pa ihP ie a More News Commentary On the Web Se cha T ch 2: E a ded HTML5 P a f Ve dic Vi a S di 11 Be a: N Read f P i e Ti e CFE gi e 3 N a: C fig a i Ma age e A Wa S eed? B igh c e: H b id HTML5/Na i e De e e Wi The Da More Commentary Video Slideshow Ca aci i e C i g De MPLabX Li W i i g K i C de ( ide )
drdobbs.com/jvm/222900697

Digital Issues
2011 Dr. Dob b 's Jo rnal Digital Archive Dr. Dob b 's Jo rnal January Digital Issue Dr. Dob b 's Jo rnal February Digital Issue Dr. Dob b 's Jo rnal March Digital Issue

3/6

09/03/12

Java and C++ Socket Communication Dr Dobb's

I, R b More Videos

M e I igh Whi e Pa e
How To Regain IT Control In An Increasingly Mobile World - by BlackBerry The BlackBerry PlayBook tablet's Good Bones - by BlackBerry More >>

Re
Strategy: The UC Identity Crisis Research: Data Encryption More >>

Webca
Maximizing performance, ease of use, and highavailability for your Microsoft Environment Best Practices for Estimating the ROI of Development Testing More >>
INFO-LINK Free article: Accelerate app integration w ith Intel Cilk Plus Free article on faster apps: The World s First Sudoku Thirty-Niner

Li e

L gi

Regi e

Sh c

i g0 e

Sort by oldest first

M Subscribe by email

RSS

BEST OF THE WEB


T i e B ffe i g a A C Mecha i c e c
Triple Buffering is a way of passing data between a producer and a consumer running at different rates. It ensures that the consumer sees only complete data with minimal lag.

E beddi g GDB B ea

i CS

ce

Have you ever wanted to embed GDB breakpoints in C source code? Something like this:

pit(Hlo\"; rnf"el,n) EBDBEKON; ME_RAPIT pit(wrd\"; rnf"ol!n)

W i i g Ke

e E

i
4/6

drdobbs.com/jvm/222900697

09/03/12

Java and C++ Socket Communication Dr Dobb's

Why attack the kernel? Because it has a huge attack surface with potential for very interesting bugs. This presentation (pdf) takes a code-level dive into recently reported Linux-kernel exploits.

i i g he Ja aSc i

E gi e

With growing demand for out-of-browser JavaScript (e.g., server JavaScript), a good knowledge of JavaScript engines is becoming

More "Best of the Web" >>

Enabling People and Organi ations to Harness the Transformative Power of Technolog

CIO & IT P fe
Black Hat BYTE Cloud Connect Dark Reading Enterprise 2.0 Enterprise Connect Enterprise Efficiency HDI InformationWeek

S f

a e De e

Ve ica Ma

Dr. Dobb's Dr. Dobb's M-Dev Dr. Dobb's Journal Dr. Dobb's Update TechWeb.com

Advanced Trading Bank Systems & Technology CreateYourNextCustomer InformationWeek Government InformationWeek Healthcare Insurance & Technology Light Reading / Telecom The CMO Site Wall Street & Technology

G ba C Se ice P
4G World

ica i ide

Cable Catchup Cloud Connect Blog Digital Life Evil Bytes Interop Blog Monkey Bidness Over the Air Personal Tech The Philter Valley Wonk

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

InformationWeek Repor

Web & Digi a P fe


Internet Evolution Online Marketing Summit TechWeb.com

InformationWeek 500 InformationWeek 500 Conference InformationWeek Events InformationWeek Global CIO InformationWeek Healthcare InformationWeek India

G e

Officia

Ga e I d
Gamasutra.com

P fe

GTEC Ottaw a InformationWeek Government TechWeb.com

Game Developers Conference (GDC) Independent Games Festival Game Developer Magazine GDC Europe

drdobbs.com/jvm/222900697

5/6

09/03/12
InformationWeek Reports InformationWeek SMB Interop Mobile Connect Netw ork Computing No Jitter TechWeb.com The BrainYard

Java and C++ Socket Communication Dr Dobb's


GDC China Game Career Guide Game Advertising Online Webinars

UBM TechWeb Reade Se ice


About UBM TechWeb Reprints Advertising Contacts Technology Marketing Solutions TechWeb Events Calendar Contact Us Feedback

TechWeb Digital Library / White Papers

TechWeb.com

Terms of Service

Privacy Statement

Copyright 2012 UBM TechWeb, All rights reserved.

Pow ered by Zend/PHP

Dr. Dobb's Home

Articles

News About Us

Blogs

Source Code Site Map

Dobb's on DVD

Dobb's TV

Webinars & Events

Contact Us

Editorial Calendar

drdobbs.com/jvm/222900697

6/6

You might also like