Michael
Michael

Reputation: 35341

Java Sockets over the Internet: ConnectException (operation timed out)

I'm trying to open a socket to a website over the Internet, but can't. After about a minute, a ConnectException is thrown saying that the operation timed out.

Socket clientSocket = new Socket(InetAddress.getByName("gmail.com"), 25);

My computer is connected to a router, which is connected to the Internet. My router is configured to direct all incoming port 25 data to port 2550 on my local machine (192.168.2.2). So, I thought maybe if I set the "local address" and "local port" parameters on the Socket constructor it might work...but this also gives me an "operation timed out" error.

Socket clientSocket = new Socket(InetAddress.getByName("gmail.com"), 25, InetAddress.getByName("192.168.2.2"), 2550);

I saw this SO question, but was wondering if anyone could shed any more light on the issue. Thanks.

Upvotes: 2

Views: 417

Answers (2)

chubbsondubs
chubbsondubs

Reputation: 38817

So you want to receive the mail messages sent from Google to your router? That's the only reason to forward incoming traffic from port 25 to 2550. If you want to do that then you're going to need to use a ServerSocket to receive the incoming connection on your 192.168.2.2 server. What you're having problems with is that Socket initiates a connection to a server. It's a client socket. A ServerSocket waits for incoming connections. Do a little research in how ServerSockets work, but if you really want to receive incoming connections you'll have to implement the SMTP protocol which is no small feat. There are SMTP libraries out there that you could reuse. Check the Apache James project which is like Servlets for Mail servers.

http://james.apache.org/

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 993791

You are trying to connect to port 25 on gmail.com, but that machine is not an email server. You must first look up the MX record for gmail.com, and then try to connect to one of the delivery servers mentioned in the returned MX record.

For example, with dig mx gmail.com on my machine, I get:

; <<>> DiG 9.3.6-P1-RedHat-9.3.6-16.P1.el5 <<>> mx gmail.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34063
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 4, ADDITIONAL: 4

;; QUESTION SECTION:
;gmail.com.                     IN      MX

;; ANSWER SECTION:
gmail.com.              1697    IN      MX      30 alt3.gmail-smtp-in.l.google.com.
gmail.com.              1697    IN      MX      40 alt4.gmail-smtp-in.l.google.com.
gmail.com.              1697    IN      MX      5 gmail-smtp-in.l.google.com.
gmail.com.              1697    IN      MX      10 alt1.gmail-smtp-in.l.google.com.
gmail.com.              1697    IN      MX      20 alt2.gmail-smtp-in.l.google.com.

;; AUTHORITY SECTION:
gmail.com.              266895  IN      NS      ns1.google.com.
gmail.com.              266895  IN      NS      ns2.google.com.
gmail.com.              266895  IN      NS      ns3.google.com.
gmail.com.              266895  IN      NS      ns4.google.com.

;; ADDITIONAL SECTION:
ns1.google.com.         262525  IN      A       216.239.32.10
ns2.google.com.         262525  IN      A       216.239.34.10
ns3.google.com.         262525  IN      A       216.239.36.10
ns4.google.com.         262525  IN      A       216.239.38.10

;; Query time: 65 msec
;; SERVER: 144.52.10.15#53(144.52.10.15)
;; WHEN: Tue Feb 28 14:30:04 2012
;; MSG SIZE  rcvd: 295

So try connecting to port 25 on gmail-smtp-in.l.google.com (which is the server with the lowest MX priority number).

Upvotes: 7

Related Questions