gpt4 book ai didi

java - 客户端代码中的套接字连接被拒绝

转载 作者:行者123 更新时间:2023-12-01 14:15:25 25 4
gpt4 key购买 nike

以下程序导致此问题

编辑:

import java.io.*;
import java.net.*;
public class smtpClient {
public static void main(String[] args) {
// declaration section:
// smtpClient: our client socket
// os: output stream
// is: input stream
Socket smtpSocket = null;
DataOutputStream os = null;
DataInputStream is = null;
// Initialization section:
// Try to open a socket on port 25 : step 1
// Try to open input and output streams: step 2
try {
smtpSocket = new Socket("192.168.1.2", 1024);
os = new DataOutputStream(smtpSocket.getOutputStream());
is = new DataInputStream(smtpSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host: hostname");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: hostname");
}
// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on port 25
if (smtpSocket != null && os != null && is != null) {
try {
// The capital string before each colon has a special meaning to SMTP
// you may want to read the SMTP specification, RFC1822/3
os.writeBytes("HELO\n");
os.writeBytes("MAIL From: k3is@fundy.csd.unbsj.ca\n");
os.writeBytes("RCPT To: k3is@fundy.csd.unbsj.ca\n");
os.writeBytes("DATA\n");
os.writeBytes("From: k3is@fundy.csd.unbsj.ca\n");
os.writeBytes("Subject: testing\n");
os.writeBytes("Hi there\n"); // message body
os.writeBytes("\n.\n");
os.writeBytes("QUIT");
// keep on reading from/to the socket till we receive the "Ok" from SMTP,
// once we received that then we want to break.
String responseLine;
while ((responseLine = is.readLine()) != null) {
System.out.println("Server: " + responseLine);
if (responseLine.indexOf("Ok") != -1) {
break;
}
}
// clean up:
// close the output stream
// close the input stream
// close the socket
os.close();
is.close();
smtpSocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
}

控制台日志:

Couldn't get I/O for the connection to: hostname

我采取的程序来自: http://www.javaworld.com/jw-12-1996/jw-12-sockets.html?page=4

我已经尝试将端口从 25 修改为 1024
我在本地 PC 上运行它,所以我是该系统的管理员,但不确定是否存在任何默认防火墙问题(在 Windows 7 上的 Eclipse 中运行此)

根据您下面的评论:我需要创建一个监听器,这意味着一个服务器套接字,它将监听 smtp 客户端请求

最佳答案

答案是:根据您提供的详细信息,没有正在运行的监听器或指定IP和端口号的机器。

UPD:那么您尝试连接到某个地方时,您必须确保有东西在另一端监听,要么编写您自己的服务器代码,要么使用第 3 方服务器/代码在某个服务器上提供某些服务。您尝试访问的端口号。

为什么您会期望在您提供的地址的计算机上运行邮件服务器?

关于java - 客户端代码中的套接字连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18147962/

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