File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes socket prog in different languages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "socket prog in different languages" Watch "socket prog in different languages" New topic
Author

socket prog in different languages

ajse ruku
Ranch Hand

Joined: May 06, 2005
Posts: 193
Hi all,
Can we have server(written in java) and client(written in c++) in socket programming?Please help.

with regards,
ajse
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Yes. Programs written in different languages can easily communicate so long as they are both using the same protocols.

The protocol (IP, TCP, UDP, ICMP, etc.) defines the language and grammar of how a client and server communicate. So long as the protocol is the same, the programming language the programs are written in is irrelevant.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi

It is not as easy as just to follow a particular protocol and it will work. Java datatypes and semantics of object structure etc are different from C++ and that will create lot of issues.

e.g. How would we deal with the Union/Enum type in C++ when it comes to Java side?
How would we deal with Integer coming from Java on a C++ compiler which might have different number of bytes for the Integer (its not always 4),

There are many other issues if we have complex data being exchanged.

Though I believe that String and char * would work fine in C++ and Java for communication.

To solve this complex issues we have things like CORBA (Common Object Request Broker Architecture)

Regards
Maulin
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
It is not as easy as just to follow a particular protocol and it will work.


I believe you misunderstood my post. Let me attempt to clarify.

For example, if both client and server are using the HyperText Transfer Protocol (HTTP), the format of the data transferred is well defined and can be created and used by any language. (And there are usually libraries available to do it for you.)

You are correct that if you decide to send native data types, the client/server will have to interpret/convert the data to be able to process it. (So the client or server or both will have to know implementation details of the other. This is not good design.)

With the technology standards available today like web services and, as you mentioned, CORBA, there are lots of options for cross-platform (and cross-language) interoperability.

Combine that with the need to interoperate with future (and unknown) clients and I would question the need to send native datatypes.

Ajse, stick with the standard network and application protocols and you should have no issues.
ajse ruku
Ranch Hand

Joined: May 06, 2005
Posts: 193
Hi,
actually there is an application written in c++.This application is exposing activeX and APIs for other programms to communicate with it.But we can do these things in C++ or VB or .NET.But I want to use the above application through java.How to do this?

with regards,
ajse
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Before I could make a recommendation, I'd need to know more about the application.

For example, how is it architected? Is it a client/server app, a fat client Windows app?

What are the performance requirements? (Is adding network latency to these API calls going to be a problem?)

Who owns the APIs? (i.e., does someone else in your organization own and maintain the APIs?)

What do these APIs do? Are equivalent Java APIs available?

Do many others in your organization use the APIs? What platforms are they on? (Would there be an advantage to making the APIs accessible from any platform?)

What advantages do you forsee by using Java instead of C++?

What are the skill sets of your development team?

Off the top of my head, I can think of three major options here (in no particular order):
1. Ditch Java and write the application in C/C++.
2. Expose the APIs as web services and call them from your Java app.
3. Write the application in Java and call the APIs through JNI.

Each has advantages and disadvantages.

If you can tell us more, maybe we can make a recommendation.
ajse ruku
Ranch Hand

Joined: May 06, 2005
Posts: 193
Hi Scott Johnson,
Thanks for your help.The application is reading COM port and collecting data from a device connected on the port.My job is to get the data from the application and send it to the server using web services(java).As I am a java developer I have to get the data from the application using java.So according to you what could be the best way to get the data from the application.

with regards,
ajse
ajse ruku
Ranch Hand

Joined: May 06, 2005
Posts: 193
Hi Scott,
I can not use java comm api to collect data from the device becouse I do not have header files for that particular device.

One thing I would also like to tell you that the application reading the COM port and writing to a socket(server) port.I have tried to read the port through socket(client) and I read the contents but the buffersize and frequency cannot be controlled by my socket programm.That is why I am not getting the complete data sent by device.Like two records and half of third record then half of third record and other record in the next time.I cannot decide what to do.

with regards,
ajse
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
I think your socket solution is the best option here.

You'll need to buffer the incoming bytes until you get full records.

Something like this:

1. read some bytes from the socket and append them to the buffer
2. process any full records
3. move any remaining bytes (partial records) to the front of the buffer
4. loop

You could avoid moving the data if you implemented a circular byte buffer.
Elizabeth King
Ranch Hand

Joined: Jul 11, 2002
Posts: 191
Originally posted by Maulin Vasavada:
Though I believe that String and char * would work fine in C++ and Java for communication.


Even String will not work fine. It is a Java class, not a simple text.
It will be serialized before sending out...

The Java ServerSocket is only for Java Socket!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: socket prog in different languages
 
Similar Threads
WA #1.....word association
copying files over a network
java client for a VB web service
Secure FTP Using SSL
VB Socket to Java Socket