简体   繁体   English

客户端代码拒绝套接字连接

[英]Socket connection refused in Client Code

The below program causes this issue 下面的程序导致此问题

EDITED: 编辑:

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

Console Log : 控制台日志:

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

The program I took is from : http://www.javaworld.com/jw-12-1996/jw-12-sockets.html?page=4 我采用的程序来自: http : //www.javaworld.com/jw-12-1996/jw-12-sockets.html?page=4

I have already tried modifying the port from 25 to 1024 我已经尝试将端口从25修改为1024
I am running it on my local PC, so I am admin on this system, but not sure if there is any default firewall issue(running this in eclipse on windows 7) 我正在本地PC上运行它,所以我是该系统的管理员,但不确定是否存在任何默认防火墙问题(在Windows 7的Eclipse中运行)

As per your comments below : DO I need to make a listner, which mean to say a Server Socket, which will listen to smtp client requests 根据您在下面的评论:我需要做一个列表器,这意味着要说一个服务器套接字,它将监​​听smtp客户端请求

Answer is: according to details what you have provided, there is no listener running or machine with specified IP and port number. 答案是:根据您提供的详细信息,没有正在运行的侦听器或具有指定IP和端口号的计算机。

UPD: then you are trying to connect to somewhere you do have to be sure that there is something which listens on other side, either writing your own server code or by using a 3rd party server/code to provide certain service on a port number you are trying to reach. UPD:那么您尝试连接到某个地方时,必须确保在另一侧监听某些内容,或者编写您自己的服务器代码,或者使用第三方服务器/代码在您的端口号上提供某些服务试图达到。

Why would you expect that there is a mail server running on machine with an address you've provided? 您为什么期望机器上运行的邮件服务器提供您提供的地址?

It sounds like some other program is using port 1024. 听起来其他程序正在使用端口1024。

Try a different port. 尝试其他端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM