Compare commits

..

No commits in common. "3b3446831482134b74621ac0dcf8c3348a14219a" and "c787162c823cafae777eb97f76de796d74003ac3" have entirely different histories.

4 changed files with 8 additions and 12 deletions

View File

@ -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. date and time. The server responnds with
## Usage ## Usage
@ -13,8 +13,7 @@ date and time.
$ make $ 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

View File

@ -13,6 +13,7 @@ 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() {
@ -38,14 +39,12 @@ void connect_to_server(int sock, char *ip, int port) {
error_handling("connect() error", sock); error_handling("connect() error", sock);
} }
int tcp_write(int sock, char* message) { char* communicate(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;
}
char *tcp_read(int sock) { // Receiving response
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);

View File

@ -7,9 +7,8 @@ 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);
int tcp_write(int sock, char* message); // Function to communicate with the server
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);

View File

@ -11,8 +11,7 @@ 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]));
tcp_write(sock, argv[3]); char* received_data = communicate(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);