Compare commits
2 Commits
c787162c82
...
3b34468314
| Author | SHA1 | Date |
|---|---|---|
|
|
3b34468314 | |
|
|
fe43eafb5c |
|
|
@ -5,7 +5,7 @@
|
||||||
This project implements a daytime TCP/UDP server and client.
|
This project implements a daytime TCP/UDP server and client.
|
||||||
The server listens for incoming connections and responds with the current date
|
The server listens for incoming connections and responds with the current date
|
||||||
and time. The client sends a message to the choosing the output format of the
|
and time. The client sends a message to the choosing the output format of the
|
||||||
date and time. The server responnds with
|
date and time.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|
@ -14,6 +14,7 @@ $ make
|
||||||
# start tcp server
|
# start tcp server
|
||||||
$ bin/daytimeTCPSrv <port>
|
$ bin/daytimeTCPSrv <port>
|
||||||
# run tcp client
|
# run tcp client
|
||||||
|
# message 1 -> get localtime otherwise get gmtime
|
||||||
$ bin/daytimeTCPCli <server-ip> <server-port> <message>
|
$ bin/daytimeTCPCli <server-ip> <server-port> <message>
|
||||||
|
|
||||||
# start udp server
|
# start udp server
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ static void error_handling(char *message, int sock) {
|
||||||
perror(message);
|
perror(message);
|
||||||
if (sock != -1)
|
if (sock != -1)
|
||||||
close(sock);
|
close(sock);
|
||||||
close(sock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int tcp_create_socket() {
|
int tcp_create_socket() {
|
||||||
|
|
@ -39,12 +38,14 @@ void connect_to_server(int sock, char *ip, int port) {
|
||||||
error_handling("connect() error", sock);
|
error_handling("connect() error", sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* communicate(int sock, char *message) {
|
int tcp_write(int sock, char* message) {
|
||||||
// Sending the ASCII string
|
// Sending the ASCII string
|
||||||
if (write(sock, message, strlen(message)) == -1)
|
if (write(sock, message, strlen(message)) == -1)
|
||||||
error_handling("write() error", sock);
|
error_handling("write() error", sock);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Receiving response
|
char *tcp_read(int sock) {
|
||||||
char* response = malloc(BUFFER_SIZE);
|
char* response = malloc(BUFFER_SIZE);
|
||||||
if (!response)
|
if (!response)
|
||||||
error_handling("Failed to allocate memory for response", sock);
|
error_handling("Failed to allocate memory for response", sock);
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,9 @@ int tcp_create_socket();
|
||||||
// Function to connect to a server
|
// Function to connect to a server
|
||||||
void connect_to_server(int sock, char *ip, int port);
|
void connect_to_server(int sock, char *ip, int port);
|
||||||
|
|
||||||
// Function to communicate with the server
|
int tcp_write(int sock, char* message);
|
||||||
char* communicate(int sock, char *message);
|
|
||||||
|
char *tcp_read(int sock);
|
||||||
|
|
||||||
// Function to create a TCP server socket
|
// Function to create a TCP server socket
|
||||||
int tcp_listen(int port);
|
int tcp_listen(int port);
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
int sock = tcp_create_socket();
|
int sock = tcp_create_socket();
|
||||||
connect_to_server(sock, argv[1], atoi(argv[2]));
|
connect_to_server(sock, argv[1], atoi(argv[2]));
|
||||||
char* received_data = communicate(sock, argv[3]);
|
tcp_write(sock, argv[3]);
|
||||||
|
char* received_data = tcp_read(sock);
|
||||||
if (received_data) {
|
if (received_data) {
|
||||||
printf("Received from server: %s\n", received_data);
|
printf("Received from server: %s\n", received_data);
|
||||||
free(received_data);
|
free(received_data);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue