Compare commits

..

2 Commits

4 changed files with 12 additions and 8 deletions

View File

@ -5,7 +5,7 @@
This project implements a daytime TCP/UDP server and client.
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
date and time. The server responnds with
date and time.
## Usage
@ -14,6 +14,7 @@ $ make
# start tcp server
$ bin/daytimeTCPSrv <port>
# run tcp client
# message 1 -> get localtime otherwise get gmtime
$ bin/daytimeTCPCli <server-ip> <server-port> <message>
# start udp server

View File

@ -13,7 +13,6 @@ static void error_handling(char *message, int sock) {
perror(message);
if (sock != -1)
close(sock);
close(sock);
}
int tcp_create_socket() {
@ -39,12 +38,14 @@ void connect_to_server(int sock, char *ip, int port) {
error_handling("connect() error", sock);
}
char* communicate(int sock, char *message) {
int tcp_write(int sock, char* message) {
// Sending the ASCII string
if (write(sock, message, strlen(message)) == -1)
error_handling("write() error", sock);
return 0;
}
// Receiving response
char *tcp_read(int sock) {
char* response = malloc(BUFFER_SIZE);
if (!response)
error_handling("Failed to allocate memory for response", sock);

View File

@ -7,8 +7,9 @@ int tcp_create_socket();
// Function to connect to a server
void connect_to_server(int sock, char *ip, int port);
// Function to communicate with the server
char* communicate(int sock, char *message);
int tcp_write(int sock, char* message);
char *tcp_read(int sock);
// Function to create a TCP server socket
int tcp_listen(int port);

View File

@ -11,7 +11,8 @@ int main(int argc, char *argv[]) {
int sock = tcp_create_socket();
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) {
printf("Received from server: %s\n", received_data);
free(received_data);