This is a collection of server client code. Here we use socket with some best practise. If you want to use the code please install make first read the make file and then enjoy .
Here some tips to understand our code:
win_ : file for windows system
unix_: file for Linux system
Else it is a cross platform code
list of code :
win_init.c :Winsock initialization
win_List.c :list network adapters on Windows
To run this code, first install make. Then in shell got to repertory where the code exist(cd ...... Socket_with_c)
run:" make unix_list"
Then ". /unix_list"
list of code :
unix_List.c : list network adapters on Linux. for more info : getnameinfo_man
To run this code, first install make. Then in shell got to repertory where the code exist(cd ...... Socket_with_c)
run:" make tcpserverfork"
Then ". /tcpserverfork"
list of code :
Using IPv4. Using fork we create a child process to handle multiple client with no delay. This code is microservers that make any string just all upper case simple and make you learn a lot.
PS : to run all this code please read the make file !
list of code :
Silmple code to inti socket work for both windows and unix like system
This is a simple time server that sends the time date to simple HTTPS web page .Here you will learn how to make a cross platform code and how to make it accept IP6 and IP4 at the same time.
For a test run http://127.0.0.1:8080, simply 127.0.0.1 is the local host address and 8080 is the port
Using IPv4. Follow instructions in the code first line if you want to test it. Using some mystery technique this code can create some chile that can send a simple string to the server and wait for his response
how to use : tcp_client 127.0.0.1 8080
Simple server that just translates a string sending by client to all uppercase string. This serves serve any number of client simultaneously with no blocking or delay ,each client will get only his string that he send to server all uper .
Port fixed to 8080 , accept both ip4 and ip6.
Simple server that just translates a string sending by client to all uppercase string. This serves serve any number of client simultaneously with no blocking or delay ,each client will see all string that he send to server all uper and the other string send by the other client .
Port fixed to 8080 , accept both ip4 and ip6
Simple UDP server.It will accept any incoming msg from UDP client.
The simple UPD client handles 2 cm argument @IP and port number. Then you start sending msg .PS there's no test if the msg reaches the server or no in this code .
Its same like TCP. We just use UDP instead of TCP. We also use fork style to handle multiclient request
to test run : .\udp_client_toupper 127.0.0.1 8080
Same like the other server using simple methods .
UDP client, accept 2 argument from cmd @IP and Port number of the server to connect with, then we make any child process to make several clients and send some msg to the server we then get them from serve and print them
This code takes a name or IP address for its only argument.It then uses getaddrinfo() to resolve that name or that IP address into an address structure, and the program prints that IP address** using getnameinfo() for the text conversion. If multiple addresses are associated with a name, it prints each of them.
for linux user :./lookup domain (example.com ,google.com .... as example )
windows user via powershell : lookup.exe domain (example.com ,google.com .... as example )
We will now implement a utility to send DNS queries to a DNS server and receive the DNS response. This should not normally be needed in the field. It is, however, a good opportunity to better understand the DNS protocol and to get experience of sending binary UDP packets.
for lunix user ,how to use :./dns_query example.com a (exemple)
windows user via powershell : dns_query.exe example.com a (exemple)
This is simple lib with one function . The function takes as input a URL, and it returns as output the hostname, the port number, and the document path .
client takes as input a URL. It thenattempts to connect to the host and retrieve the resource given by the URL. The program displays the HTTP headers that are sent and received, and it attempts to parse out the requested resource content from the HTTP response.
for lunix user ,how to use : ./ webclientget http://example.com/ (exemple)
windows user via powershell : webclientget.exe http://example.com/ (exemple)
-HTTP web server that can serve static files from a local directory .Our server only supports GET typerequests .Our server only supports hosting one site. Our server expects all hosted files to be in a subdirectory called public .
how to use : after make it (mean compile it using make file) . simply run the server ./webserver (webserver.exe for windows user ) then go to your web browser and tape http://127.0.0.1:8080/
then yo got it yey .
PS: You can also drop different files into the public folder and play around with creating more complicated websites.
-It behaves exactly like the code we developed, but it avoids having global state(at the expense of a little added verbosity). This may make web_server2.c more suitable for integration into more significant projects and continued development.
PS: make 2webserver
The program we develop is used to deliver mail directly to the recipient's email provider. It is not useful to deliver mail to a relay server because we are not going to implement authentication techniques. Generally, SMTP servers do not relay mail without authenticating that the sender has an account with them.
how to use : after make it (mean compile it using make file) . simply run ./smtp_send (smtp_send.exe for windows user )
then,Connecting to host: gmail-smtp-in.k.google.com
then fill all other needed info .
to close mail that you want send just send .
PS: The SMTP protocol we describe in this code is unsecured and not encrypted. This is convenient for explanation and learning purposes, but in the real world, you may want to secure your email transfer.
test OpenSSL library installed or no
Initiation to sll . simple code
how to use :
Linux user :./https_simple example.org 443
Windows user :https_simple example.org 443
This is tcpclient.c , An In-Depth Overview of TCP Connections, but it has been modified to make TLS connections.
Linux user :./tls_client example.org 443
Windows user :tls_client example.org 443
This is the webclientget.c program from Chapter 6, Building a Simple Web Client, but it has been modified for HTTPS. You can think of it as the extended version of https_simple.c.
for lunix user ,how to use : ./ https_get https://example.com/ (exemple)
windows user via powershell : https_get.exe https://example.com/ (exemple)
This is like https_simple.c, but it simply prints the connected server's certificate and exits.
Linux user :./tls_get_cert example.org 443
Windows user :tls_get_cert example.org 443
OpenSSL asks questions about what to put on the certificate, including the subject, your name, company, location, and so on. You can use the defaults on all of these as this doesn't matter for our testing purposes.
The basic command to self-sign a certificate is as follows:
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout key.pem -out cert.pem -days 365
You can also use OpenSSL to view a certificate. The following command does this:
openssl x509 -text -noout -in cert.pem
Windows using MSYS, you may get garbled line endings from the previous command. If so, try using unix2dos to fix it, as shown by the following command:
openssl x509 -text -noout -in cert.pem | unix2dos
simple time server that displays the time to an HTTPS client. This program is an adaptation of time_server.c .Our server uses a while loop to accept multiple connections.
How to use :
Generate your certification use section "Self-signed certificates with OpenSSL"
Fisrt run the server .
You can connect to the time server by navigating your web browser to https://127.0.0.1:8080
To access the time server, you need to add an exception in the browser. The method for this is different in each browser, but generally, there is an Advanced button that leads to an option to either add a certificate exception or otherwise proceed with the insecure connection.
This program is a modification of web_server.c , Building a Simple Web Server. It can be usedto serve a simple static website over HTTPS. In the https_server.c program, the basic TLS/SSL connection is set up and established the same way as shown in tls_time_server.c. Once the secure connection is established, the connection is simply treated as HTTP
How to use :
Generate your certification use section "Self-signed certificates with OpenSSL"
Fisrt run the server .
You can connect to the time server by navigating your web browser to https://127.0.0.1:8080
To access the time server, you need to add an exception in the browser. The method for this is different in each browser, but generally, there is an Advanced button that leads to an option to either add a certificate exception or otherwise proceed with the insecure connection.
Simple code to see the libssh version
Simple code tha allow you to connect to ssh server . Note please that you need to instal open ssh to try tis project .
How to use :How to use :
-./ss_connect 127.0.0.1(@IP of the server ) 22 (Port number if you change it or it s optional )
-./ss_connect.exe 127.0.0.1(@IP of the server ) 22 (Port number if you change it or it s optional )
Simple code tha allow you to connect to ssh server and authentify . Note please that you need to instal open ssh to try tis project .
how to use :
-./ssh_auth 127.0.0.1(@IP of the server ) 22 (Port number ) alice
-ssh_auth.exe 127.0.0.1(@IP of the server ) 22 (Port number ) alice
ssh_auth was used to successfully authenticate with the locally running SSH server. The ssh_auth program used password authentication with the username alice and the password password123. Needless to say, you need to change the username and password as appropriate for your SSH server
Simple code tha allow you to connect to ssh server ,authentify and send command to remote machine . Note please that you need to instal open ssh to try tis project .
how to use :
-./ssh_command 127.0.0.1(@IP of the server ) 22 (Port number ) alice
-ssh_command.exe 127.0.0.1(@IP of the server ) 22 (Port number )alice
ssh_auth was used to successfully authenticate with the locally running SSH server. The ssh_auth program used password authentication with the username alice and the password password123. Needless to say, you need to change the username and password as appropriate for your SSH server
Simple code tha allow you to connect to ssh server ,authentify and send file to remote machine . Note please that you need to instal open ssh to try tis project .
how to use :
-./ssh_download127.0.0.1(@IP of the server ) 22 (Port number ) alice
-ssh_download.exe 127.0.0.1(@IP of the server ) 22 (Port number ) alice
ssh_auth was used to successfully authenticate with the locally running SSH server. The ssh_auth program used password authentication with the username alice and the password password123. Needless to say, you need to change the username and password as appropriate for your SSH server
display a text-based error message in addition tothe error code .
connect_timeout.c in is a working example of the connect() timeout method using select(). An additional example, connect_blocking.c, is also included for comparison .
how to use :
-./connect_blocking (or ./connect_timeout) 127.0.0.1(@IP of the server ) 22 (Port number )
-connect_blocking.exe (or connect_timeout.exe) 127.0.0.1(@IP of the server ) 22 (Port number )
server_ignore.c, implements a simple TCP server that accepts connections and then ignores them. The second file, big_send.c, initiates a TCP connection and then attempts to send lots of data. By using the big_send program to connect to the server_ignore program, you can investigate the blocking behavior of send() for yourself.
how to use :
- ./server_ignore
./big_send @IP 8080 - server_ignore.exe
big_send.exe @IP 8080
Essentially, when an application initiates a TCP socket close (or causes the disconnection by crashing), that socket goes into the TIME-WAIT state. The operating system continues to keep track of this socket for some time, potentially minutes.You can reproduce this address-in-use problem by running it, accepting a connection, and then terminating server_noreuse. To reproduce the problem, it is vital that the server is the one to terminate the open connection, not the client.
PS :You can use the netstat command to see these half-dead connections that are preventing our server from starting. The following command shows which connections are stuck in the TIME-WAIT state on Linux: netstat -na | grep TIME
As long as one of these connections is hanging on, it prevents any new process from calling bind() on the same local port and address. This failure of the bind() call can be prevented by setting the SO_REUSEADDR flag on the server socket before calling bind(). Once the SO_REUSEADDR flag is set, bind() succeeds even if a few TIME-WAIT connections are still hanging on to the same local port and address. An example program, server_reuse.c, is included to demonstrate this technique.
Just run server and use any exemeple for the client with port number 8080
This program accepts TCP connections on port 8080. It then waits for the client to disconnect, and then attempts two sends to that disconnected client. This program is useful as a tool to explore the return values, error codes, and function behavior in different scenarios.
for windows user please read the make file
To install openssl in windows :
run powershell using Administrator mode in Windows. :
choco install openssl