We help IT Professionals succeed at work.
Get Started
Private
Troubleshooting Question

Socket program erroring out

Rohit Bajaj
Rohit Bajaj asked
on
45 Views
Last Modified: 2020-11-17
Hi,
I have the following basic socket program :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class JokeServer {
    static final char[] EOL = {(char) '\r', (char) '\n'};

    public static void main(String a[]) throws IOException {
        // Using try with resources as it closes everything autocloseable
        try (
                ServerSocket serverSocket = new ServerSocket(Constants.jokeServerPort, Constants.backLog);
                Socket socket = serverSocket.accept();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        ) {
            out.println("HTTP/1.1 200 OK");
            out.println("Content-type: " + "application/json");
            out.println("Content-Length: " + 10);
            out.write("\r\n");
        }
    }

}


The port here is 4545.
Now when i use http://localhost:4545/  from chrome after running the above server.
The server stops. And in chrome network tab all i see is red color request nothing else
How to find out what error exactly it is.

Also i tried doing  curl -i -H "Accept: application/json" http://localhost:4545 
The ouput was  -
curl: (7) Failed to connect to localhost port 4545: Connection refused
Dont know what that means.

what could be the reason for these errors and how to resolve them.

Thanks


Comment
Watch Question
CERTIFIED EXPERT
Distinguished Expert 2020
Commented:
This problem has been solved!
Unlock 2 Answers and 17 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE