質問

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.

役に立ちましたか?

解決

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

他のヒント

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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top