vidhya
vidhya

Reputation: 2901

Socket Programming in java

I was executing a socket program.The program is to, just echo the user input , by the server .ie if the user gives input as Apple the server reply should be Apple. But the problem now i am facing is, the server is sending a message(instead of Apple ) which used to be the Banner Message that we get when we login to the server.Once the banner message is over ,the following error gets displayed :

Exception in thread "main" java.net.SocketException: Software caused connection abort: recv failed
 at java.net.SocketInputStream.socketRead0(Native Method)
 at java.net.SocketInputStream.read(Unknown Source)
 at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
 at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
 at sun.nio.cs.StreamDecoder.read(Unknown Source)
 at java.io.InputStreamReader.read(Unknown Source)
 at java.io.BufferedReader.fill(Unknown Source)
 at java.io.BufferedReader.readLine(Unknown Source)
 at java.io.BufferedReader.readLine(Unknown Source)
 at EchoClient.main(EchoClient.java:69)

Following is my code :

import java.net.*;
import java.io.*;
public class EchoClient
{
    public static void main(String[] args) throws IOException
    {
        Socket echosocket = null;   
        PrintWriter out =null;
        BufferedReader in=null;

        //establish the socket connection between the client and the server
        // open a PrintWriter and a BufferedReader on the socket: 

        try
        {   
            echosocket = new Socket("ltesol1.comm.mot.com",22);
            out=new PrintWriter(echosocket.getOutputStream(),true);    
            in=new BufferedReader(new InputStreamReader(echosocket.getInputStream()));    

        }
        catch(UnknownHostException e) 
        {
            System.err.print("Unable to find the host dkc678-01");
            System.exit(1);
        }
        catch(IOException e)
        {
            System.err.print("No IO for host dkc678-01");
            System.exit(1);
        }

        BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in));

        String userInput;

        while((userInput =stdIn.readLine())!= null )
        {
            out.println(userInput);      
            System.out.println("echo :" + in.readLine());  
        }

        out.close();
        in.close();
        stdIn.close();
        echosocket.close();
    }   

}

Upvotes: 0

Views: 1368

Answers (3)

Christian Kuetbach
Christian Kuetbach

Reputation: 16060

If you want too connect to a SSH-Server, you have to use the ssh-protocol: http://javassh.org

You should find the sources of a ssh-client there.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533442

Port 22 is usually used for ssh which is an encrypted connection. You can't use a plain text stream.

In either case, the server is disconnecting the connection. You need to find out why it is doing that.

Upvotes: 0

Jochem
Jochem

Reputation: 2994

Can you please comment on my suggestion that another service (ie sshd/telnet server) is listening to port 22 on the server side? Or otherwise give us the server code?

Upvotes: 0

Related Questions