Pages

Wednesday, January 19, 2011

Telnet Server with Java

This code sends a string to a client whenever client connects it and sends the response to client when command "1" is received.


-Watch video
https://youtu.be/sX0XAROhuiQ
import java.lang.*;

import java.io.*;

import java.net.*;

class TelnetServer {

   public static void main(String args[]) {

      String data = "Hello Client!! ";

      try {

          // Create object of Server Socket 

         ServerSocket srvr = new ServerSocket(8088);

          // Socket object that listens the port (8088) and accepts the incoming connection

          //requests 

         Socket skt = srvr.accept();

         System.out.println("Client Connected!");

         // gets output stream object  

         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);

         //gets input stream object    

         DataInputStream din=new DataInputStream(skt.getInputStream());        

         if(din.readUTF().equals("1")){ 

         // sends response to incoming request if command is '1'    



         System.out.println("String: '" + data);

         out.print(data);

         }

         out.close();// clos out

         skt.close();// close skt

         srvr.close();// close srvr

         din.close(); // close din

      }

      catch(Exception e) {

         System.out.print(e);

      }

   }

}
Watch video
Telnet Client Source

1 comment: