Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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 客户端代码中的套接字连接被拒绝_Java_Sockets - Fatal编程技术网

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

Java 客户端代码中的套接字连接被拒绝,java,sockets,Java,Sockets,以下程序导致此问题 编辑: 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 = nu

以下程序导致此问题

编辑:

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
我参加的课程来自:

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


根据您下面的评论:我是否需要制作一个listner,也就是说一个服务器套接字,它将侦听smtp客户端请求

听起来好像其他程序正在使用端口1024


请尝试其他端口。

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

UPD:然后您尝试连接到某个地方,您必须确保另一端有侦听器,可以编写您自己的服务器代码,也可以使用第三方服务器/代码在您尝试访问的端口号上提供特定服务


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

有什么东西在该ip地址的端口1024上侦听吗?只是想知道-有什么东西在该端口上侦听并且指定了ip?1024是保留端口…它实际上不是reserver,这种情况下,较低的1024个端口只允许root@Zavior不,仅保留最多(包括)端口1023。