user4559332
user4559332

Reputation:

setting up an http server using java

I am trying to set up an http server using java. I am currently using the Vert.x package. This is my first experience with http server so i am kind of lost and don't really know how to proceed, hence i am seeking guidance. I have scrambled a a piece of code (probably not full) from some examples that i found on the subject.

public class SimpleFileServer extends AbstractVerticle {

private HttpServer httpServer = null;

@Override
public void start() throws Exception {
    httpServer = vertx.createHttpServer();

    httpServer.requestHandler(new Handler<HttpServerRequest>() {
        @Override
        public void handle(HttpServerRequest request) {
            System.out.println("incoming request!");

            Buffer fullRequestBody = Buffer.buffer();
            if(request.method() == HttpMethod.POST){

              request.handler(new Handler<Buffer>() {
                 @Override
                 public void handle(Buffer buffer) {
                      fullRequestBody.appendBuffer(buffer);
                 }
              }
            }

        }
    });

    httpServer.listen(3030);

}}

i have many question on this matter, for example: what should be added to the code in order to send files (once every specific time interval) to the server? how can i check whether the server is working properly? assuming i don't have another computer to connect to it as a client? Should i try to set up TCP server instead?

I am basically seeking for any guidance on this matter, very basic stuff. any pointers, suggestions and good examples would be highly appreciated. thank you.

Upvotes: 0

Views: 631

Answers (2)

Paulo Lopes
Paulo Lopes

Reputation: 5801

If you're looking for examples then I'd recommend you to have a look at the examples repo. It contains Hello World size examples showing almost all the API.

From your code you can see in the core examples how to make a HTTP server to handle file uploads. And the respective client code to upload to the server.

Of course the core API is quite low level and there are easier and more productive ways to work with web applications, you should look at vertx-web examples.

Vertx-web covers almost all you need for modern web-apps from simple HTTP servers and routing to, realtime websockets and integration with javascript frameworks as Angular and React.

Upvotes: 0

RJN
RJN

Reputation: 93

  1. Why would you want to send a file periodically to the server? This sounds like you want to have a server send a file to another server. You can do this if you really want, you'd have to have another server, or some other service to send a file to your server.
  2. To check if you're server is working properly you'd have to define what "working properly" is, i.e. values are within this range, number of connections is below this threshold, stuff like that. Then you could every night, or whenever have your server check itself against these rules and do something if an error is found.
  3. There is no such thing as a TCP server, TCP (Transmission Control Protocol) is how the data is transmitted between servers and clients. HTTP (Hypertext Transfer Protocol) is an application protocol, it does not describe how data is transmitted between servers and clients. TCP/IP HTTP

Upvotes: 0

Related Questions