Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TCP_SERVER_datetime:
- #include <stdio.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <time.h>
- #define MAX 80
- #define PORT 8080
- #define SA struct sockaddr
- void func(int connectID) {
- char buffer[MAX];
- int n;
- char datetime[MAX];
- time_t rawtime;
- struct tm *timeinfo;
- for (;;) {
- bzero(buffer, MAX);
- read(connectID, buffer, sizeof(buffer));
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- strftime(datetime, MAX, "%Y-%m-%d %H:%M:%S", timeinfo);
- printf("[%s] From the client: %s\t To the client : ", datetime, buffer);
- bzero(buffer, MAX);
- n = 0;
- while ((buffer[n++] = getchar()) != '\n');
- write(connectID, buffer, sizeof(buffer));
- if (strncmp("exit", buffer, 4) == 0) {
- printf("Closing Server\n");
- break;
- }
- }
- }
- int main() {
- int sockfd, connectID, length;
- struct sockaddr_in serverAddress, client;
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd == -1) {
- printf("Socket not created!\n");
- exit(0);
- } else {
- printf("Socket successfully created!\n");
- }
- bzero(&serverAddress, sizeof(serverAddress));
- serverAddress.sin_family = AF_INET;
- serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
- serverAddress.sin_port = htons(PORT);
- if ((bind(sockfd, (SA *)&serverAddress, sizeof(serverAddress))) != 0) {
- printf("Socket binding failed!\n");
- exit(0);
- } else {
- printf("Socket binding done successfully!\n");
- }
- if ((listen(sockfd, 5)) != 0) {
- printf("Listen failed!\n");
- exit(0);
- } else {
- printf("Server listening!\n");
- }
- length = sizeof(client);
- connectID = accept(sockfd, (SA *)&client, &length);
- if (connectID < 0) {
- printf("Server accept failed!\n");
- exit(0);
- } else {
- printf("Server accepted the client!\n");
- }
- func(connectID);
- close(sockfd);
- }
- //TCP_CLIENT_datetime:
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <time.h>
- #define MAX 80
- #define PORT 8080
- #define SA struct sockaddr
- void func(int sockfd) {
- char buffer[MAX];
- int n;
- char datetime[MAX];
- time_t rawtime;
- struct tm *timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- strftime(datetime, MAX, "%Y-%m-%d %H:%M:%S", timeinfo);
- for (;;) {
- bzero(buffer, sizeof(buffer));
- printf("Enter the string: ");
- n = 0;
- while ((buffer[n++] = getchar()) != '\n');
- int message_length = strlen(buffer);
- write(sockfd, datetime, strlen(datetime));
- write(sockfd, buffer, message_length);
- read(sockfd, buffer, MAX);
- printf("[%s] From the server : %s", datetime, buffer);
- if ((strncmp(buffer, "exit", 4)) == 0) {
- printf("Closing Client\n");
- break;
- }
- }
- }
- int main() {
- int sockfd, connfd;
- struct sockaddr_in socketAddress, cli;
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd == -1) {
- printf("Socket not created!\n");
- exit(0);
- } else {
- printf("Socket successfully created!\n");
- }
- bzero(&socketAddress, sizeof(socketAddress));
- socketAddress.sin_family = AF_INET;
- socketAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
- socketAddress.sin_port = htons(PORT);
- if (connect(sockfd, (SA *)&socketAddress, sizeof(socketAddress)) != 0) {
- printf("Connection with the server failed!\n");
- exit(0);
- } else {
- printf("Connected to the server!\n");
- }
- func(sockfd);
- close(sockfd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement