whichgift.blogg.se

Simple ftp server c code
Simple ftp server c code











simple ftp server c code

You don't have to write explicit error handling code (this is what exceptions do they unwind the code). You have a single place to catch and log exceptions. They are part of a bigger framework so in C code you need to add error logic explicitly to the code to force the stack to unwind to the point where the user can take an action based on the error, or in a server you have a single place that is doing the error logging (so that it is consistent otherwise each library will end up generating logs in a different way).Įxceptions provide the best of both worlds. Have you been consistent with the error messages that are generated? You have to search and check every exit location.Ī lot of code (especially bigger apps or libraries) don't want want to exit. There is a disadvantage in that you have exit locations scattered through your code. Which is fine as it makes the rest of the code easy to read as you are not writing code to pass error message back through many layers of code so it keeps the code nice and simple. In your code you have places that you call exit(). ExceptionsĮxceptions make code easier to understand. If you see this in your code you should be using RAII.

simple ftp server c code

So even if you throw an exception that causes the stack to unwind as it unwinds the stack the destructors for all automatic objects are being called. You should also note that an objects destructor is called when the control leaves the scope in which the object is created. This is how we stop memory leaks in C++ that happen in C when things don't got as planned. When learning about the idiom people mostly talk about memory and smart pointers. So when an object is created it will create and hold onto resources and when it is destroyed it will clean up those resources. In C++ we have this concept that an object should clean up its own resources. You still need a C review! Idioms/Patterns RAII idiom Printf("Connected with %s encryption\n", SSL_get_cipher(ssl)) Host is hardcoded to localhost for testing purposesĬonst int sfd = OpenConnection("127.0.0.1", argv) ĮRR_print_errors_fp(stderr) //High probability this doesn't do anythingįprintf(stderr, "SSL_connect failed with SSL_get_error code %d\n", status) Printf("Info: No client certificates configured.\n") X509 *cert = SSL_get_peer_certificate(ssl) /* get the server's certificate */Ĭhar *line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0) If (connect(sfd, addr->ai_addr, addr->ai_addrlen) = 0)įprintf(stderr, "%s: %s\n", hostname, strerror(err)) Sfd = socket(addrs->ai_family, addrs->ai_socktype, addrs->ai_protocol) If ((host = gethostbyname(hostname)) = nullptr)Ĭonst int status = getaddrinfo(hostname, port, &hints, &addrs) įprintf(stderr, "%s: %s\n", hostname, gai_strerror(status)) įor (struct addrinfo *addr = addrs addr != nullptr addr = addr->ai_next) Int OpenConnection(const char *hostname, const char *port) The OpenConnection method was heavily based on the answer to this post: Ĭonst SSL_METHOD *method = TLS_client_method() /* Create new client-method instance */ compiled with g++ -Wall -o client.out client.cpp -L/usr/lib -lssl -lcrypto

simple ftp server c code

This code (theoretically) writes "Hello World, 123" to a socket over a secure TLS connection Not sure what headers are needed or not I'm very new to C++, so I don't know if I'm using good naming conventions or other basic practices. It also serves as a base for more complex applications. As such, I decided to make a simple client that opens a TLS connection and writes some data as practice. I am learning C++ and socket programming and OpenSSL.













Simple ftp server c code