Toggle navigation
Log in
Sign Up
Log in
Sign Up
Appium
C
C#
C++
Docker
Go
Informatica
Java
JavaScript
Kafka
Numpy
Oracle
Pandas
PHP
Py Spark
Python
R
React Native
Scipy
SFTP
Tableau
Teradata
TGMC
UNIX
Introducing
Radical.sh
Forget Code launches a powerful code generator for building API's
Forget_Code.Models.CategoryViewModel
Add a new snippet
Algorithms
13
Applications
5
Arithmetic Operations
2
Array
8
Basics
27
Compiler Design
1
Control Statements
4
Conversion Functions
1
Data Structures
12
Data Type
1
Date Functions
1
File
36
Keywords
1
Loops
1
Math Functions
30
Math Snippets
43
Memory Management
3
Misc
4
Networking
4
Operators
6
Pointers
17
String Functions
30
String Snippets
29
System Software
10
Utility Snippets
1
Choose Category
TCP/IP DATE-TIME SERVER in C
Forget Code
C
TCP/IP DATE-TIME SERVER
server :
#include"netinet/in.h"
#include"sys/socket.h"
#include"stdio.h"
#include"string.h"
#include"time.h"
main() {
struct sockaddr_in sa;
struct sockaddr_in cli;
int sockfd, conntfd;
int len, ch;
char str[100];
time_t tick;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("error in socket\n");
exit(0);
} else printf("Socket opened");
bzero( & sa, sizeof(sa));
sa.sin_port = htons(4000);
sa.sin_addr.s_addr = htonl(0);
if (bind(sockfd, (struct sockaddr * ) & sa, sizeof(sa)) < 0) {
printf("Error in binding\n");
} else
printf("Binded Successfully");
listen(sockfd, 50);
for (;;) {
len = sizeof(ch);
conntfd = accept(sockfd, (struct sockaddr * ) & cli, & len);
printf(" Accepted");
tick = time(NULL);
snprintf(str, sizeof(str), "%s", ctime( & tick));
printf("%s", str);
write(conntfd, str, 100);
}
}
client:
#include"netinet/in.h"
#include"sys/socket.h"
#include"stdio.h"
main() {
struct sockaddr_in sa, cli;
int n, sockfd;
int len;
char buff[100];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("\nError in Socket");
exit(0);
} else printf("\nSocket is Opened");
bzero( & sa, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(4000);
if (connect(sockfd, (struct sockaddr * ) & sa, sizeof(sa)) < 0) {
printf("\nError in connection failed");
exit(0);
} else
printf("\nconnected successfully");
if (n = read(sockfd, buff, sizeof(buff)) < 0) {
printf("\nError in Reading");
exit(0);
} else {
printf("\nMessage Read %s", buff);
}
}
Contribute to Forget Code, help others.
Add snippet