java - Send a string command to a socket -
i not familiar telnet appreciate help willing. have smart plugs can switch on or off through telnet interface. utilize telnet via command prompt connect server digi x4 connect port (via >telnet ). if want switch socket on/off, have type: "12 set pow=on/off" , press enter. implement through java using telnet client. able connect port (thanks answers posted on platform), send command switch devices on/off proving hard me. still have type "12 set pow=on/off" , press enter. java send command. below java code. appreciate assistance. bab
public class telnetconnection { static telnetclient tc = null; public static void main(string[] a) throws exception { string[] args = {"122.1222.181.45","8085"}; system.out.println("arg value: "+args); if(args.length < 1) { system.err.println("usage: error <remote-ip> [<remote-port>]"); system.exit(1); } string remoteip = args[0]; int remoteport; if (args.length > 1) { remoteport = (new integer(args[1])).intvalue(); } else { remoteport = 7000; } tc = new telnetclient(); while (true) { boolean end_loop = false; seek { tc.connect(remoteip, remoteport); thread reader = new thread (new telnetclientexample()); tc.registernotifhandler(new telnetclientexample()); system.out.println("telnetclientexample"); reader.start(); outputstream outstr = tc.getoutputstream(); printwriter out = new printwriter(outstr); string buff = "11 set pow=on"; //int ret_read = 0; { seek { out.print(buff); outstr.flush(); } grab (ioexception e) { system.err.println("error"); end_loop = true; } } while((true) && (end_loop == false)); seek { tc.disconnect(); } grab (ioexception e) { system.err.println("error"); } } grab (ioexception e) { system.err.println("exception while connecting:" + e.getmessage()); system.exit(1); } } } }
try tring buff = "11 set pow=on\n"
; server may need newline observe end-of-command.
by way, loop infinitely sends server looks worrisome.
java sockets
No comments:
Post a Comment