0

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.

2 Answers 2

0
  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
Sign up to request clarification or add additional context in comments.

3 Comments

thank you. much more to learn here. can you tell me what does this error mean and how do i fix it Exception in thread "main" java.net.BindException: Address already in use: bind ?
@Dany it simply means the port is used by some process. Kill the other process or change port and try again
@user3632894 is correct, whatever port you are binding to is already being used by another process.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.