I was wondering how, if possible, could the task of creating/emulating a java serversocket in C++ be performed? I'm new to C++ but I'm fairly proficient in Java. My server (written in java) needs to receive data from all both Java/C++ clients (data is transferred using JSON Strings) but I'm not sure how to establish a connection in C++ with the NIO Server.
Thanks in advance for any help!
Start by reading the following man pages:
socket(2)
bind(2)
listen(2)
accept(2)
connect(2)
After you determine whether you need to create a server or a client-side socket, you will then proceed to implement it using the appropriate combination of these system calls.
A socket is a socket. Whether or not the other end of the socket is an application written in Java, C++, Perl, Ruby, or any other language, it makes no difference. All sockets are created the same way. It does make a difference in terms of the format of the data exchanged across the socket, but looks like you have that covered.
Related
I have a requirement where I need to send message from multiple clients and those clients can be C clients or Java clients to server written in java.
Why I need to send over socket? Because there can be any process in any app on a same system who wants my app to capture there alarms and react accordingly.
Now, I can implement multiple Java clients who can connect to single Java server but how to do the same thing irrespective of it is Java client r C client.
If not socket programming, then what could be the other best way to make this communication happen
All low level network programming is written using sockets. you can have server written in any language communicate with client written in any language. To make this happen we have certain protocol which governs the communication like udp, tcp etc.
high level language provides api which will you to connect any server with single line of code without you needing to create socket. But in C you can create socket and connect it to the server. see this. All you need to know connect server and client is server ip and port...
While your requirements are very broad it looks a lot like a prime candidate for protocol buffers.
https://developers.google.com/protocol-buffers/
Language agnostic.
Platform neutral.
Fast.
Running on ZeroMQ (http://zeromq.org/) where you can push and pretty much run on top of everything.
I am about to write a client-server application (a simple game) and I want to use Erlang for the server side and Java for the client side. How should I make the connection between client and server? I have never worked with network programming before...
I think that "sockets" is the thing that I should use, am I right? In that case, could I use Java's sockets-class to send/recieve messages on the client and some corresponding sockets-module in Erlang on the server? Or do I have to have some kind of Java receiver on the server and then translate to Erlang? Is sockets some general protocol sent over the network that all languages interprets?
Depends on what you want to learn.
Sockets are low level and you will need design a protocol.
HTTP Rest API is more general, requires web server (yaws for example)
or Jinterface Erlang<->Java bridge
http://www.slideshare.net/dennisbyrne/integrating-java-and-erlang-with-jinterface-presentation
Indeed, Erlang provides a module for Socket programming that you can use. Go with your option number one.
But is such low level programming is a must for you? If not for your game, then consider creating a REST API. It is HTTP after all. Thus you can achieve:
general protocol sent over the network that all languages interprets
Another alternative to sockets and HTTP for creating
some general protocol sent over the network that all languages interprets
is something like Apache Thrift or other interface description languages (IDL). While it doesn't support all languages, it does support both Java and Erlang.
I'm using a Java library that uses a socket to communicate with the world. I'd like to get a reference to that socket so I can monitor the data the library is sending and receiving. Is there any way to hook Java's sockets system so I can monitor socket communications when I can't modify the code that creates the socket?
Have a look at that idea:
Finding out what network sockets are open in the current Java VM
I haven't tested it, but it looks interesting as it presents a way to hook into the socket creation process of Java.
I've a doubt in java socket programming. The tcp server is created Java Socket Programming and running in different machine. Now I want to create a client for the server to request some data from server. Am I need to use same Java socket programming or I can use different APIs to communicate with server and get response back?
Thanks,
Pramod
You must also use sockets, but not necesserily in Java. Virtually any programming language can talk to different machines based on TCP/IP or UDP sockets. You can even use tools like nc to test your server.
If the server is using sockets, then yes, the client should also be written using the sockets API. This can be done in any programming language, not necessarily Java.
To give you a broader perspective, there exist other technologies that allow two processes to communicate (RMI, zeromq etc). However, it is almost always the case that both sides of the communication have to use the same technology to be able to talk to each other.
I have a system I am implementing where I have a MATLAB Server who uses a socket to accept a TCP connection, and a Java Client which connects to that server.
My problem is that when the server accepts the client's connection, apparently, the client manages to send the input before the server manages to reach a line of code that locks it into reading the expected input from the client...
Assuming I do not know how much time to wait would be safe, in a generic case, is there any way to solve this problem for all situations?
Could I use some sort of lock object, shared between MATLAB and Java? Should I assume that the client always awaits some sort of confirmation from the server? and if so, how exactly can i have a guarantee that the server will rush to listen after sending such a notification to the client, fast enough?
Thanks in advance!
By the way, if anyone knows of a simple way of getting the system time from Java (System.currentTimeMilis()) in MATLAB, it would be useful to further test this. I know there are quite some functions to access time in MATLAB, but I don't really know if there is any (or any way) to get it exactly the same way as in Java.
There are easier ways to call Matlab from Java - JMI for example:
http://undocumentedmatlab.com/blog/jmi-java-to-matlab-interface/
Regarding the system time, run this in Matlab:
javaTime = java.lang.System.currentTimeMillis