Hi Im still learning how to use react and java. I am creating a project where the front end is in react and the backend is in java.
I was wondering if someone would be able to point me in the right direction or give me an example of java sending a message to the front end in react. What Im essentially trying to do is give the user a message after the backend receives a file they submit. I already have the part where the backend is processing the file.
Ive tried looking at a lot of sites but theres not a lot of documentation out there for react and java as the backend.
thank you
What you need is Websocket, he provide a full-duplex communication channels over a single TCP connection. So, when you start a comunication between client and server, a session is created, making possible to send and receive message for both side.
Some possibilities are, use javax:
https://docs.oracle.com/javaee/7/api/javax/websocket/package-summary.html
https://www.baeldung.com/java-websockets
Use spring-websocket(if are you already using spring, will be easy to use):
https://spring.io/guides/gs/messaging-stomp-websocket/
https://docs.spring.io/spring/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/websocket.html
Here is another example with javax https://www.pegaxchange.com/2018/01/28/websocket-server-java/
So, there are a lot of examples.
Websocket would open a channel between the client and the server, so you can send a server message at any time.
But, if you are already using a POST from the client to upload the file, you could just send a response to the POST.
Related
I need to write a server in java that will store integers that are used as stats for my android app. My app has an SQLite database that is already setup that would be useful if I could just pass those contents to the server. I need to do this so that in my multiplayer feature, a player's app will contact the server to know what level their opponent is. (This is not real time). So far I only have a TCP server that opens a client and server socket. Other than that I am not positive what I need to do to make it possible for the server to do what I need. Anyone have suggestions or good tutorials to help me with my problem?
Thanks.
One thing you need to keep track of:
Your SQLite DB has to be used as a player session, until the player logs in as another user. To do this, you have to have one instance of the current logged in player. If you prefer other methods, here's a good list.
I don't think you would need a TCP connection to view the stats, instead implement a REST API in PHP or any other language for back-end that would save, update and look up for a particular player. This is a recommended method. If you are familiar with PHP, this tutorial will give you an idea and full details of what I mean.
Indeed, building a server has many methods. For example, You can use either socket programming or app-server. If you are familiar with web programming, jsp/php/asp.net, you can communicate between your app and server with HTTP requset, which is easy to do.
To be more specific, someone (neme:abc,password:pwd) logging in with your mutiplayer app, when he or she leaves, send the data to server with HTTP request. Then when he or she logging in with other device, you can get the data from status and continues to play. Of course, you can save the data in local with SharedPrefence in Android, too.
By the way, the apache HTTPClient is recommend when using http connection with server.
I think this might be a very stupid question.
I have 2 servers that are going to communicate with each other via HTTPS using XML packets.
My question is this, there is no webpage just a Java application. How would I get my Java app to run when it hears an XML packet from the other server? Are the messages to be sent via POST?
I know about HTTP protocols but my knowledge goes as far as webservers to sending the webpage to browsers, server to server communication w/o a webpage is baffling me. Can anyone point me to the right direction or better still to some codes that I might be able to look at.
Thank you.
You can use HttpClient in your server applications so that they become clients to each other. Then maybe simply implement REST services for the 'clients' to call.
I would suggest trying this out firstly using HTTP before trying it in HTTPS.
Of course there are other technologies you could try E.g JMS or SOAP.
I am trying to learn how to make a multiple-client chatting program. Im following the Oracle tutorial on Custom Networking, but its not really what I am looking for. So far, I have no code of my own to share, all of it is copied from the Oracle tutorial and I think pasting it here would be a copyright infringement(??).
link at: http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html+
client code link: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockClient.java
anyway, I have the server-client working where the server tells knock knock jokes, the client reads and prints, then the user replies and so on. how would i start to edit it to have the client talk to the other clients directly?
im sorry, i have no background with networking at all. if anyone can direct me to a informative source better suited to my goals i would appreciate it.
as in the Knock-Knock example, each client connects to the server, but they not mutually directly connected.
There is a solution to make a forwarding sever :Arrange each client an id, and clients use id to identify their talking partners.
To do this, you have to modify the client to server data format from a plain string to a tuple like (String,Id). And, when the server receive the data, it parses out the id, get the corresponding client socket and then write the data.
The required level of complexity just went up a notch as your going to need some sort of "interprocess communications" infrastructure to allow client to client communication (possibly via sockets marshalled by the server?)
Since I'm a newbie to Java web server, I don't know where to begin to create a server in Java to reply from Android devices request( A WEB SERVER NOT AN ACTUAL SITE).
Which technology should I use and which document should i read?. I have searched on the Internet about Java but i found out many technologies and i don't know which one i can use.
Spring MVC,JSF,JSP,Struts,Servlet..............
Hope this question clear enough for you to reply. Thank you
It doesn't matter what be process android devices requests on server side. In this case main idea is http communication - client sends request, server process this request and sends response to client, and you can use any technology on server side - java, php etc.
If you prefer java I can suggest you try servlets, it is simplest technology of those you mentioned above.
Here is the situation. There are server and client in network. They communicate like this:
Client sends request for some function.
Server sends to client function parameters.
Client trying to perform function and sends answer to server.
Server sends to client data which it should show.
But sometimes client can't perform function and sends error. I want to catch all packets from step 2, analyze them (I've already have tools for that), prevent some of them to reach client, process them with my program and form packet like in step 3. This must be done on client side. I have no access neither to server nor to client.
So, the question is: Is there libraries for changing, injecting and removing tcp/ip packet in c++ or java? The solution should be working in both Win and Linux systems.
Also, may be you have better ideas to expand client functionality?
Thanks for any help!
I tried to google how to change packets, but all I got were unanswered questions and sniffers=(
Edit: Actually, I don't really need injecting and removing packets, I can manage it with only changing packet data. Also, there is no multiple requests in the same packet, and a single request across multiple packets is not a problem.
You have to build a Proxy for your server. The client connects to the proxy, and the proxy itself connects to the server. It just routes all the packages between client and server.
BUT it is now able to intercept specific messages and to modify them. Imagine a filtering HTTP proxy, it works the same way.
I have personal experience with libpcap on linux and freeBSD, a kind of lowlevel library that helps to catch or inject packets. I did use it in an IPV6 network bridge project... But i know there is a windows port for it.
http://sourceforge.net/projects/libpcap/
You can let the library to:
catch packets using a filter
extract data from packet
you can process the data (modify them)
reinject it again using the same library
But you would have to work with internal data in a quite raw matter. Best documentation for this library are comments inside its header file, that is the most up to date info. Maybe there are some more comfortable highlevel libraries.