Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Socket ProgrammingRSS Feeds

Program of telnet server - client

Posted By: Easy Tutor     Category: Java     Views: 25416

Write a program of telnet server - client.

Code for Program of telnet server - client in Java

// Telnet Client

import java.net.*;
import java.io.*;

class TelnetClient
{
    publicstaticvoid main(String args[]) throws Exception
    {
        Socket soc=new Socket("127.0.0.1",5217);
        String LoginName;
        String Password;
        String Command;
        
        
        DataInputStream din=new DataInputStream(soc.getInputStream());        
        DataOutputStream dout=new DataOutputStream(soc.getOutputStream());
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Welcome to Telnet Client");
        System.out.println("Your Credential Please...");
        System.out.print("Login Name :");

        LoginName=br.readLine();
        
        System.out.print("Password :");
        Password=br.readLine();
        
        dout.writeUTF(LoginName);
        dout.writeUTF(Password);

        if (din.readUTF().equals("ALLOWED"))
        {
            do
            {
            System.out.print("< Telnet Prompt >");
            Command=br.readLine();            
            dout.writeUTF(Command);
            if(!Command.equals("quit"))
            {
                System.out.println(din.readUTF());        
            }                
            }while(!Command.equals("quit"));
        }
        soc.close();        
    }
}

// Telnet server

import java.net.*;
import java.io.*;
import java.lang.*;
import java.io.*;
import java.util.*;

class TelnetServer
{
    publicstaticvoid main(String args[]) throws Exception
    {
        ServerSocket Soc=new ServerSocket(5217);
        while(true)
        {
            Socket CSoc=Soc.accept();
            AcceptTelnetClient ob=new AcceptTelnetClient(CSoc);
        }
    }
}

class AcceptTelnetClient extends Thread
{
    Socket ClientSocket;
    DataInputStream din;
    DataOutputStream dout;
    String LoginName;
    String Password;

    AcceptTelnetClient(Socket CSoc) throws Exception
    {
        ClientSocket=CSoc;
        System.out.println("Client Connected ...");
        DataInputStream din=new DataInputStream(ClientSocket.getInputStream());
        DataOutputStream dout=new DataOutputStream(ClientSocket.getOutputStream());
        
        System.out.println("Waiting for UserName And Password");
        
        LoginName=din.readUTF();
        Password=din.readUTF();
            
        start();        
    }
    publicvoid run()
    {
        try
        {    
        DataInputStream din=new DataInputStream(ClientSocket.getInputStream());
        DataOutputStream dout=new DataOutputStream(ClientSocket.getOutputStream());

        BufferedReader brFin=new BufferedReader(new FileReader("Passwords.txt"));

        String LoginInfo=new String("");
        boolean allow=false;
        
        while((LoginInfo=brFin.readLine())!=null)
        {
            StringTokenizer st=new StringTokenizer(LoginInfo);
            if(LoginName.equals(st.nextToken()) && Password.equals(st.nextToken()))
            {
                dout.writeUTF("ALLOWED");
                allow=true;
                break;
            }
        }
        
        brFin.close();

        if (allow==false)
        {
            dout.writeUTF("NOT_ALLOWED");            
        }
        
    

        while(allow)
        {
            String strCommand;
            strCommand=din.readUTF();
            if(strCommand.equals("quit"))
            {
                allow=false;
            }
            else
            {
                Runtime rt=Runtime.getRuntime();
            
                Process p=rt.exec("TelnetServer.bat " + strCommand);
                
                String stdout=new String("");
                String st;
                DataInputStream dstdin=new DataInputStream(p.getInputStream());
                while((st=dstdin.readLine())!=null)
                {
                    stdout=stdout +st + "\n";
                }
                dstdin.close();
                dout.writeUTF(stdout);                        
            }                        
        }
        ClientSocket.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    } 
}
  
Share: 


Didn't find what you were looking for? Find more on Program of telnet server - client Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of telnet server - client is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Akash Shah from India Comment on: Mar 31
Can i know what TelnetServer.bat file consists of??

Akash Shah from India Comment on: Mar 31
Can i know what TelnetServer.bat file consists of??

View All Comments