gpt4 book ai didi

java - java中的套接字编程

转载 作者:行者123 更新时间:2023-12-02 08:27:12 25 4
gpt4 key购买 nike

我正在执行一个套接字程序。该程序的作用是,仅回显用户输入,由服务器发送。即,如果用户以 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();
}

}

最佳答案

如果您也想连接到 SSH 服务器,则必须使用 ssh 协议(protocol):http://javassh.org

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

关于java - java中的套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4275069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com