1

I am trying creating a socket connection to a web server.

import java.io.IOException;
import java.io.PrintWriter; 
import java.net.ServerSocket;
import java.net.Socket;


public class Connection{
public final static int PORT = 1337;

public Connection(){
    ServerSocket svrSocket = null;
    try{
        svrSocket = new ServerSocket(PORT);
        System.out.println("Conected to: " + PORT);
        Socket con  =  null;
        while(true)
        {
            try{
                con = svrSocket.accept();//on this part the program stops
                System.out.println("Client request accepted");
                PrintWriter out = new PrintWriter(con.getOutputStream());

                out.flush();
            }catch(IOException ex)
            {
                ex.printStackTrace();

            }
        }
    }catch(IOException ex)
    {
        System.err.println(ex);
        System.out.println("Unable to attach to port");
    }


}
}

The client request con = svrSocket.accept(); does not run. I say this because the message after this line does not display.

Why does it not accept the client request and is it possible to test the Server using a Web browser? Please excuse my bad programming style.

Thank you.

Jonathan
  • 545
  • 1
  • 8
  • 27

2 Answers2

2

You need to have a client connect to your server, for your server to be able to accept the connection, until that happens the code will wait at that line.

If you run an instance of your code and then compile and run this code (while your code is also running), you will find that you get the client request accepted message, if you get an IOException due to the port, change the port in both of them to something that doesn't appear when you call netstat -o in cmd.

import java.io.*;
import java.net.*;
public class TestCon
{
    public static void main(String[] args)
    {
        Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            echoSocket = new Socket("127.0.0.1", 1337);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
            out.println("Hello Server!");
            System.out.println(in.readLine());
            out.close();
            in.close();
            echoSocket.close();
        } catch (UnknownHostException e) {
            System.err.println("Host Unknown");System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection.");System.exit(1);
        }
    }
}
Robadob
  • 5,319
  • 2
  • 23
  • 32
  • 1
    At `SERVER_URL` i can use any URL? – Jonathan Jul 28 '13 at 15:03
  • 2
    You should use the URL of your server, if your running this on the same computer "localhost" or "127.0.0.1" should work. – Robadob Jul 28 '13 at 15:05
  • 1
    Hen i add this as a program, I use port 80 and my URL as 127.0.0.1 bust the program still stop's at the same place. If I use port 1337 in this little program I get an Exception. (Note! I wrote this program and instantiated it in my main with the code that I gave) – Jonathan Jul 28 '13 at 15:16
  • I've compiled and ran my code and your code (I added a main method to your code), and it works. If your getting an IOException due to the port, it means you have something already attached to that port, so use a different port. – Robadob Jul 28 '13 at 15:22
  • Thank you I got mine to work now I am going to create a tread and then see if it works all in the same program. – Jonathan Jul 28 '13 at 15:35
1

The client request con = svrSocket.accept(); does not run.

This is not a Client connect request code. This is the Server setting itself up to listen on this port and block until an incoming connection is received. Create a new client program or start a new Thread that waits until the server has started and then connects using

Socket clientSocket = new Socket ("127.0.0.1", 1337);
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • When I run this I get a `IOException` because of the port. When I use port 80 it runs but it does the same thing as before where it is waiting for the incoming connection. – Jonathan Jul 28 '13 at 15:18