I have a problem with writing down the data I recieve through my C# server in an Android app. The app sends the server the function to give back a list of all running processes.
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
sw.Write(p.ProcessName + "\n");
sw.Flush();
Console.Write(p.ProcessName + "\n");
}
The Console output works perfectly fine, but at the recieving end I just get the first process in the list. The problem lies in the \n: I tried first building up a string, then flushing and with for-loops printing numbers. It's always the \n that fails.
I'm printing in out into a TextView, if that helps.
Thanks for any suggestions.
Instead of sending "\n" to your client send "%d%n%s%n" Source: \n won't work, not going to a new line
Last option would be instead of sending the "\n" from the server you could write a byte and then on the android side you could interpret it as
a "\n"
if(DataInputStream.readByte()==1){//Replace the 1 with whatever byte you want to represent the \n.
runOnUiThread(new Runnable(){//Runs the code on the UiThread
public void run()
{
MainActiviy.TextView.append("\n");//When you get the byte you append a \n to your textview
}
});
}
You would run this piece of code on a Network thread and it would update the Ui.
Related
I`m building a discord bot with jda, made a method to use mXparser to get a math operation as an input from the chat e.g: /math 5+1
wrote everything to get the message, separate the arguments from the input on the chat, everything works until I put the code inside an IF statement that checks if it actually starts with "/math", the code inside it uses mXparser to calculate everything and send it back to chat.
Tried just about everything I could think of, taking all variables off the method, rewriting everything, I don`t get any errors either as stack trace or in the code editor, it just doesnt go through, tried just printing everything and it works fine as well, printing all the values on the console, everything seems to be right.
This part is where I check for the message, get the Strings and trim everything
public void onMessageReceived(MessageReceivedEvent event) {
this.messageReceived = event;
this.prefix = Main.prefix;
checkPrefix = messageReceived.getMessage().getContentRaw().split("\\s" + prefix);
mainArg = checkPrefix[0];
checkArgs = messageReceived.getMessage().getContentRaw().split(" ");
callAllCommands();
}
Here is the command to take the actual expression from the chat input calculate it and send it back.
private void mathCommand() {
mathexp = new Expression(checkArgs[1]);
messageReceived.getChannel().sendTyping().queue();
essageReceived.getChannel().sendMessage(Double.toString(mathexp.calculate())).queue();
}
This is inside the callAllCommands() method, that is how it is supposed to work, if the command on the chat is /math then the expression e.g: /math 1+1 it will send the result back, if I take off the IF statement it works just fine but then I can't check for the command. The other commands do work fine with the IF statement
if (mainArg.contentEquals(prefix + "math")) {
mathCommand();
}
I don't really get any errors, it just does not work, sorry if I missed something really simple, i`m not that experienced yet.
I used java to make a pretty neat chatbot a while ago. Recently, I discovered I could use VBS to read text out loud. I used what I discovered to have the chatbot actually speak instead of just printing the response.
That all works fine, but the user can type responses faster than the text-to-speech can finish talking. This causes a backup, as the user enters messages, the bot's responses just get added to the tts queue and won't be spoken until the first response(s) are finished being read.
When the script to read the response is called, I want all speech to be stopped before the response is read. I don't have a clue as how to do this, help would be appreciated. Thanks!
textSpeech.vbs
//I want all speech to be stopped here
Dim sapi
Set sapi=CreateObject("sapi.spvoice")
//speaks the string passed to script
sapi.Speak Wscript.Arguments(0)
Chatbot.java (only relevant code is shown)
try {
//textSpeech.vbs is executed with sayString as an argument
Runtime.getRuntime().exec( "wscript \"" + path + "\" \"" + sayString + "\"");
} catch( IOException e ) {
System.out.println(e);
System.exit(0);
}
VBS has just one thread, it means that once you start a process, the processor stops at that line and does not go forward in your code til the process is finished.
Inside vbs you cant do anything. But, for our luck, you can do this at java.
When users start typing again, you kill the runtime process from java:
Process rp = Runtime.getRuntime()
.exec( "wscript \"" + path + "\" \"" + sayString + "\"");
// whenever user starts typing{
if(user.isTyping()){
rp.destroy();
}
I hope it works for you!
I'm writing a Java console GUI that should run in Windows and Linux. The characters typed into the console window should go to the process as soon as they are typed. I am using the write() function (with length 1) followed by a flush(). Without the flush it doesn't go to process until I press <Enter>. The process is echoing back characters, so I should see them in console. Everything works fine in Windows, but in Linux, apparently it sends a <CR> for each flush(). Because the first thing that the process does is getting a password, it gets the wrong password after first character (because is submitted by <CR>).
If I skip the flush, it works, but the text typed is not visible in the console till the next <Enter>.
Could anyone help with how to avoid those <CR>s to be sent with the flush()?
Thanks a lot!
Code sample:
BufferedWriter writer;
......
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
switch(e.getID()) {
case KeyEvent.KEY_TYPED:
byte b;
b = (byte)e.getKeyChar();
try {
writer.write(b);
writer.flush();
} catch (IOException ex) {
}
break;
}
return true;
}
Are you reading the inputs by line? Could you give us the code where you do the flush() and what you do before it? From the way you've explained, it's as if the flush() pushing the to the stream when the flush() will not push anything that is not already in the buffer. So, I'd probably check if the got in there before you even start flushing it.
Please don't hesitate to edit the question or ask more details if I missed anything.
I know it's bad to use Scriptlets in JSP.
But I am assigned to maintain the existing JAVA project which is build only with only JSP and servlets(No framework).
My task is to implement the load balancing for my applicaiton using Apache HTTP Server.
The application works fine with out load balancing. When I implement the load balancing using the Apache HTTP Server, I am facing the problem with JSP.
I will give a scenario. My JSP has one while loop and it runs the javascript to update the content .
My JSP has,
<%
String jsPreAppend = "<script language=JavaScript >push('";
String jsPostAppend = "')</script> ";
String s=null;
int i = 0;
try {
while (true) {
System.out.println("count :"+i);
out.print(jsPreAppend + i + jsPostAppend);
out.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
out.print(jsPreAppend + "InterruptedException: " + e + jsPostAppend);
}
i++;
}
} catch (Exception e) {
out.print(jsPreAppend + "Exception: " + e + jsPostAppend);
}
%>
My JavaScript has,
function push(content) {
document.getElementById('update').innerHTML = content;
}
The console output will be,
count :1
count :2
count :3
.
.
.
count : n
count :n+1
But the content will not updated in JSP. I thing the javascript fails in while loop.
But the SysOut() works because the updated content will be printed for every sec in the console .
But the same applicaiton work's fine with out load balancing(only one tomcat).
Hope our stack users will help me.
When your HTML gets rendered, JSP would have already got executed. So what you are trying to do cannot be achieved by that code.
You need to write a Java script method which does some update once in sometime. Check this thread to write the same logic using Javascript
Take into account that the while(true) loop will be executed server side. At that point, the response document (the HTML) is being built, and it can't be yet interpreted by the client. This loop is only writing javascript calls to a kind of buffer where the response is stored before it is sent to the client.
As an example, what that loop is doing is writing ad-infinitum to the response:
<script language=JavaScript >push('1')')</script>
...
<script language=JavaScript >push('n')')</script>
The fact that every line is being written at every second is irrelevant. You see the traces in the standard output at the correct times because that's what is being executed on the server.
This will make the request get stuck in that infinite loop unless there's an exception of some kind. Even if the loop ended at some point, and the request finished processing, when these statements would get executed by a client, they would be executed sequentially without any delay.
You should move those calls to client side, and schedule its execution with a client-side mechanism such as setTimeout(), like #sanbhat suggested in his answer.
I am trying to do a programming exercise here making a client and a server that work with sockets.For the communication between them i use PrintWriter and InputReader.What im stuck with is : How can i check if the Server is trying to send something to the client,while the client is waiting for input ? At this point the client loop is something like this :
do {
outToServer.println(inFromUser.readLine());
String fromServer=inFromServer.readLine();
if(fromServer.equals("OK")){
clientSocket.close();
}else{
System.out.println(fromServer);
}
} while (!clientSocket.isClosed());
The problem is that in some cases the server needs to print multiple lines ,before needed an input again.Instead it prints 1 line then ill have to type something,then another line comes etc . Is there a way to get around that problem ? Thanks .
So you're going to need to encapsulate the
outToServer.readLine();
in a while loop that has a boolean conditional similar to a hasNext() function. You may want to set it up as follows:
String x = outToServer.readLine();
//while x doesn't equal whatever readLine would return if there's nothing to read
//I'm not sure if this is a null String or not
while(x!=null){
System.out.print(x);//print it however you wish
String x = outToServer.readLine();
}
And then do something similar for inFromServer if needed.