简体   繁体   English

通过服务器套接字接受客户端请求

[英]Accepting client request with server Socket

I am trying creating a socket connection to a web server. 我正在尝试创建与Web服务器的套接字连接。

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(); 客户端请求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? 为什么它不接受客户端请求,并且可以使用Web浏览器测试服务器? 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. 如果运行代码的实例,然后编译并运行此代码(同时代码也在运行),则会发现您收到客户端请求接受的消息,如果由于端口而收到IOException,请在以下位置更改端口两者都无法在cmd中调用netstat -o时出现。

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(); 客户端请求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 创建一个新的客户端程序或启动一个新的Thread ,该Thread将等到服务器启动后再使用

Socket clientSocket = new Socket ("127.0.0.1", 1337);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM