텔넷에 익숙하지 않아서 기꺼이 도와 드리겠습니다. 텔넷 인터페이스를 통해 스위치를 켜거나 끌 수있는 스마트 플러그가 있습니다. 나는 명령 프롬프트를 통해 텔넷을 사용하여 서버에 연결합니다. Digi X4는 (텔넷을 통해) 포트를 연결합니다. 소켓을 켜고 끌고 싶으면 "12 set pow = on/off"라고 입력하고 Enter 키를 눌러야합니다. 텔넷 클라이언트를 사용하여 java를 통해이 기능을 구현하고 싶습니다. 이제 포트에 연결할 수 있습니다 (이 플랫폼에 게시 된 답변 덕택에).하지만 장치를 켜고 끌 수있는 명령을 보내는 것이 어렵습니다. 나는 여전히 "12 set pow = on/off"라고 타이프하고 엔터를 누른다. 자바가이 명령을 보내길 바란다. 아래 자바 코드입니다. 도와 주셔서 감사합니다. 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;
try
{
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;
do
{
try
{
out.print(buff);
outstr.flush();
}
catch (IOException e)
{
System.err.println("Error");
end_loop = true;
}
}
while((true) && (end_loop == false));
try
{
tc.disconnect();
}
catch (IOException e)
{
System.err.println("Error");
}
}
catch (IOException e)
{
System.err.println("Exception while connecting:" + e.getMessage());
System.exit(1);
}
}
}
}