Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的Socket编程_Java - Fatal编程技术网

java中的Socket编程

java中的Socket编程,java,Java,我在执行一个socket程序。这个程序就是,只回显用户的输入, 由服务器提供。即,如果用户作为Apple提供输入,则服务器回复应为Apple。 但我现在面临的问题是,服务器正在发送一条消息(而不是Apple),这曾经是我们登录服务器时收到的横幅消息。横幅消息结束后,将显示以下错误: Exception in thread "main" java.net.SocketException: Software caused connection abort: recv failed at java.

我在执行一个socket程序。这个程序就是,只回显用户的输入, 由服务器提供。即,如果用户作为Apple提供输入,则服务器回复应为Apple。 但我现在面临的问题是,服务器正在发送一条消息(而不是Apple),这曾经是我们登录服务器时收到的横幅消息。横幅消息结束后,将显示以下错误:

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)
以下是我的代码:

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();
    }   

}

我建议另一个服务(即sshd/telnet服务器)正在监听服务器端的端口22,请您对此发表意见?或者给我们服务器代码?

端口22通常用于ssh,这是一种加密连接。不能使用纯文本流


无论哪种情况,服务器都会断开连接。您需要了解它为什么这样做。

如果您想连接到SSH服务器,必须使用SSH协议:


您应该在那里找到ssh客户端的源。

请格式化代码/消息,使用101010按钮查看代码。谢谢。请努力将代码减少到显示错误的最小程序。你现在有一堆不相关的东西混在一起。请停止在每次编辑时对文章进行反格式化。考虑到当前的代码示例,我认为“问题”很可能出在服务器上。客户端失败是因为连接出现问题,导致.readLine()失败。PS:等等,可能是sshd正在服务器上运行吗?为什么这是一个社区wiki?你是对的Jochem…这是一个SSH连接…你的意思是我需要更改端口…?那么端口可能是哪一个?嗯,默认的echo端口是端口7,也许这就是你希望使用的端口。否则,您的客户端应该连接到与您的服务器应用程序正在侦听的端口相同的端口(如果您确实有一个端口)