Java tcp connection pass value of variable (simple echo server) - java

I have a java code where I am making a tcp connection to a site. Then, after that, I want to pass the value of a variable so that the site will permanently display that value. For example, if I define int x = 10; in my java program, I want to pass this value to the site so that it echos that value for ever until a new value is passed. I have no idea how to do this however, is this even possible? Where should I look, in terms of both java and php? Thanks

I suppose, when you are creating a tcp-connection, means you are the client and the site you are connected to is the server. I also hope that you are the one in charge of the server here. If that is the case, then what you are looking for is a simple Java server-client system. This will get you started with that.
If, you are not in charge of the server then you'll just have to create the client part and send data. But that itself probably, won't be sufficient because the server should be able to parse your data. So, you'll have to find out in what format is the server expecting data from the client.

Related

Combine two http responses into one

Is it possible to send extra data attached to a http response via Java or Php?
My Website is a homework-platform: One User enters homeworks into a database, and all users can then see the homeworks on the website. The current load is very inefficient, as the browser makes two requests for eveything to load: One for the index file and one for the homeworks. For the homeworks request the client also sends settings of the user to the server, based on which the returned homeworks are generated by a Php script.
Now, I wonder, if it is possible, to combine those two requests into one? Is it maybe possible to detect the http request with Java or Php on the server, read the cookies (where the settings are saved), then get the homeworks from the database and send the data attached to the http response to the client? Or, even better, firstly only return the index file and as soon as possible and the homework data afterwards as a second response, because the client needs some time to parse the Html & build the DOM-tree when it can't show the homeworks anyway.
While browsing the web I stumbled across terms like "Server-side rendering" and "SPDY", but I don't know if those are the right starting points.
Any help is highly appreciated, as I'm personally very interested in a solution and it would greatly improve the load time of my website.
A simple solution to your problem is to initialize your data in the index file.
You would create a javascript object, and embed it right into the html, rendered by your server. You could place this object in the global namespace (such as under window.initData), so that it can be accessed by the code in your script.
<scipt>
window.initData = {
someVariable: 23,
}; // you could use json_encode if you use php, or Jackson if you use java
</script>
However, it is not a huge problem if your data is fetched in a separate server request. Especially when it takes more time to retrieve the data from the database/web services, you can provide better user experience by first fetching the static content very quickly and displaying a spinner while the (slower) data is being loaded.

Arduino-Processing Serial.send/Serial.get interaction

I'm trying to get my Arduino environment to send data to processing for a data visualization project.
I have managed to get the handshake working and can Serial.println(...) to print the data I need from Arduino to the Processing console. What I need now is to somehow use that data to alter a variable within Processing.
I know that neither of the methods that I mention in the title exist within the Serial class but I was hoping someone would know how to manage this functionality.
Thanks in advance!
I will assume that you are using the serialEvent method in processing. Example code:
String val = myPort.readStringUntil('\n');
Where myPort is the port that your android device is on.

Argument passing to Java Socket Program

is it possible to send argument to java socket like we do in url argument passing
XXX.XXX.XXX.XXX:XXXXX?id=d1948b485d6
I have application which broadcast message from XXXX1 to XXXX10 port and every port i have assigned to specific user , so i want to restrict user from to access any other port .
XXX.XXX.XXX.XXX:XXXXX?id=d1948b485d6 by using unique id was decided to validate user with port,id buts its not possible so any other way do so.
That is a unique feature of web page access and can't be used with regular sockets. It can be used with servlets, though, because they are basically web pages.
Edit: Now that the question has been revised, I can now answer the underlying question much better than the original question. It is not a good idea to assign each user a port. Then you are limited to about 64000 users. Instead, the user should transmit authentication information with each request, or transmit it when opening the connection and then use only that connection.

Creating sessions in java socket programming with one server and multiple clients

I am a novice programmer in java.
I have created a program which is similar to a chat application using socket programming.
I haven't used threads.
My question is:
Whenever a client1 wants to communicate to another client2 via the sever how can i know which client is communicating.
I thought if i could differentiate all clients from server side by using sessions it would be easier. Just a random thought.
why this question??
I want to know this because i have stored the adressess of clients in a file along with a sequence number to make the messages visible to the pair alone globally. Whenever a client sends a message it prefixes it with a sequence number which is got from the client1 client2 pair stored in that file.
Any help would be greatly appreciated. Im just a learner in java. I apologize for any obscurity in my question.
First of all, try to make a thread to open a socket because it helps in your GUI processing. And the point that you want to see client names can be done by this steps:-
Add an editbox and enter your name (client name) and send this name along with the message and at server-side, split message and name so that you can determine which message is sent by whom.
Ex:- Client name is Alex and the message is "hi" then the data which will be sent is something like "Alex+hi". Now split this message using split() function in java at the server-side.
Hope this encoding and decoding will help you.

Passing binary data from Javascript into a Java applet via LiveConnect

I'm working on an application with a javascript front end that can receive a bunch of octets from the server (for the time being I'm using php's chr() to simulate some data).
Trying to pass the data from javascript into an applet to be manipulated is proving difficult. Since the data can have nulls mid-string, it looks like it gets terminated at the first null going in. It also looks like once the binary data touches a javascript variable the encoding messes with some of the bytes (or maybe that's just a problem with how I'm displaying it)
Either way, what options do I have for taking a block of binary data, sent from a server and putting it into a Java applet to be manipulated. Is a conversion to base64 (or some other encoding) my only option if I want to maintain data integrity?
All this is new to me, so hopefully I got things across clearly.
Ah the bane of liveconnect.
Yes, you either need or use urlencode or base64 to get your data through.
Even when passing stuff from JavaScript to Flash (or back) you need to this, because the interface in between uses null terminated strings (which is just stupid, I know).
I think that architecture when your java script gets binary data and passes it to java applet is not optimal. Did you probably think to modify applet to make it to go directly to server and get the binary data? Without any java script?

Categories