hoeRIZON
hoeRIZON

Reputation: 59

Connecting to your own PC using telnet and Server Sockets. Get connection rejected each time

I've pasted my whole code below, the majority of it is not related to the issue. I'm trying to create a Java server and then use telnet to connect to it (on the same PC) and put out random strings. So far, I cannot get past the face that I get my connection rejected every time I telnet to my PC. I successfully managed to connect to my university computers whilst in class but not at home, for some reason.

I've listed the code below. Are there ports that I should open or could it be an issue with Windows 10 or something? I'm really new to Sockets thus I don't really know what I'm talking about myself.

package cm3033.lab3.ex1nonthreadedechoserver;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class MainAppEx1NonThreadedEchoServer 
{ 
  public static void main(String[] args) 
  { 
    try 
    { 
      ServerSocket s = new ServerSocket(8189) ; 
      // listen for a connection request on server socket s
      // incoming is the connection socket
      for(;;)
      { 
        Socket incoming = s.accept() ; 
        // set up streams for bidirectional transfer
        // across connection socket 
        BufferedReader in = new BufferedReader 
          (new InputStreamReader(incoming.getInputStream())) ; 
        PrintWriter out = new PrintWriter 
          (incoming.getOutputStream(), true /* auto flush */) ; 
        out.println("You are connected to " + 
          incoming.getLocalAddress().getHostName() + 
          " on port " + incoming.getLocalPort() 
        ) ; 
        out.println("Type BYE to quit") ; 
        boolean done = false ; 
        while(!done) 
        { 
          String str = in.readLine() ; 
          if (str == null) 
            done = true ; 
          else 
          { 
            out.println("ECHO: " + str) ; 
            if (str.trim().equals("BYE")) 
              done = true ; 
          } 
        } 
        incoming.close() ;
      } 
    } 
    catch(Exception e) 
    { 
      System.out.println(e) ; 
    } 
  } 
}</code>

The image below is the message I get when I try to telnet.

telnet error

Upvotes: 0

Views: 1092

Answers (1)

idipous
idipous

Reputation: 2910

You are trying to do a telnet without giving the port number so it defaults to 23. However you have set up your server with port 8189.

Use telnet s-PC 8189 instead.

Upvotes: 2

Related Questions