Loading...
More C Posts
// Client side implementation of UDP client-server model#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>#include <netinet/in.h>#define PORT 8008#define MAXLINE 1024// Driver codeint main() {char buffer[MAXLINE];struct sockaddr_in servaddr;// Creating socket file descriptorint sockfd = socket(AF_INET, SOCK_DGRAM, 0);if ( sockfd < 0 ) {perror("socket creation failed");exit(EXIT_FAILURE);}struct timeval timeout;timeout.tv_sec = 1; //Wait 1 secondtimeout.tv_usec = 0;if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) < 0) perror("setsockopt failed\n");if (setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof timeout) < 0) perror("setsockopt failed\n");memset(&servaddr, 0, sizeof(servaddr));// Filling server informationservaddr.sin_family = AF_INET;servaddr.sin_port = htons(PORT);servaddr.sin_addr.s_addr = inet_addr("192.168.4.65");for(int i = 0; i < 10; ++i){char* message = "PING";int sendStatus = sendto(sockfd, (const char *)message, strlen(message), MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr));if(sendStatus >= 0){printf("Sent PING\n");}else{printf("Send failed\n");continue;}int len = sizeof(struct sockaddr_in);int bytesRecieved = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *) &servaddr, &len); //We can reuse servaddr because the port the server sends messages from is the same one we send toif(bytesRecieved >= 0){buffer[bytesRecieved] = '\0';printf("Recieved %s\n", buffer);}else{printf("Recieved nothing: Packet Dropped\n", buffer);}}close(sockfd);return 0;}
#include <stdio.h>#include <string.h>#include <unistd.h>#define READ 0 /* The index of the read end of the pipe */#define WRITE 1 /* The index of the write end of the pipe */char * phrase = "This goes in the pipe";int main() {int fd[2], bytesRead;char message[100]; /* Parent process' message buffer */pipe(fd); /* Create unnamed pipe */if (fork() == 0) /* Child, writer */ {close(fd[READ]); /* Close unused end */write(fd[WRITE], phrase, strlen(phrase) + 1); /* Include NULL */close(fd[WRITE]); /* Close used end */} else /* Parent, reader */ {close(fd[WRITE]); /* Close unused end */bytesRead = read(fd[READ], message, 100);printf("Parent just read %i bytes: %s\n", bytesRead, message);close(fd[READ]); /* Close used end */}}
// compile: gcc socketTCPServer.c -o socketTCPServer.out// usage : ./socketTCPServer.out port// Server#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>#include <sys/types.h>int main(int argc, char *argv[]){int listenfd = 0, connfd = 0, cli_size, portno;struct sockaddr_in serv_addr, cli_addr;char sendBuff[1025];if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){printf("socket error\n");exit(EXIT_FAILURE);}memset(&serv_addr, '0', sizeof(serv_addr));memset(sendBuff, '0', sizeof(sendBuff));serv_addr.sin_family = AF_INET;serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);portno = atoi(argv[1]);serv_addr.sin_port = htons(portno);const int on = 1;setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1){printf("bind error\n");exit(EXIT_FAILURE);}if(listen(listenfd, 5) == -1){printf("listen error\n");exit(EXIT_FAILURE);}while(1){cli_size = sizeof(cli_addr);if((connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &cli_size)) == -1){printf("accept error\n");exit(EXIT_FAILURE);}strcpy(sendBuff, "Server Message: SUCCESS\n");write(connfd, sendBuff, strlen(sendBuff));close(connfd);sleep(1);}return 0;}
// The following code is the fast inverse square root implementation from Quake III Arena// this code has been stripped of C preprocessor directives, but includes the exact original comment textfloat Q_rsqrt( float number ){long i;float x2, y;const float threehalfs = 1.5F;x2 = number * 0.5F;y = number;i = * ( long * ) &y; // evil floating point bit level hackingi = 0x5f3759df - ( i >> 1 ); // what the fuck?y = * ( float * ) &i;y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removedreturn y;}
#include <stdio.h>#include <stdlib.h>// give an array of identical coins (ints), represented by 1's and one 2// we can find the heavy coin (2) by summing the contents of the thirdsint sum_subarr(int arr[], int l, int r){int sum = 0;for(int i = l; i <= r; i++){sum += arr[i];}return sum;}int heavy_coin(int arr[], int l, int r){if(r == l){return r;}int midl = l + (r - l)/3;int midr = r - (r - l)/3;int lw = sum_subarr(arr, l, midl);int mw = sum_subarr(arr, midl + 1, midr);if ( lw > mw ){return heavy_coin(arr, l, midl);}else if ( lw < mw ){return heavy_coin(arr, midl + 1, midr);}else{return heavy_coin(arr, midr + 1, r);}}int main(int argc, char *argv[]){int arr[] = {1,1,1,1,2,1,1,1,1};int l = 0;int r = 8;int res = -1;res = heavy_coin(arr, l, r);printf("Hevy coin is at index: %d\n", res);return EXIT_SUCCESS;}
// Server side implementation of UDP client-server model#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>#include <netinet/in.h>#define PORT 8008#define MAXLINE 1024// Driver codeint main(){int sockfd;char buffer[MAXLINE];char *hello = "Hello from server";struct sockaddr_in servaddr, cliaddr;// Creating socket file descriptorif ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {perror("socket creation failed");exit(EXIT_FAILURE);}memset(&servaddr, 0, sizeof(servaddr));memset(&cliaddr, 0, sizeof(cliaddr));// Filling server informationservaddr.sin_family = AF_INET; // IPv4servaddr.sin_addr.s_addr = INADDR_ANY;servaddr.sin_port = htons(PORT);// Bind the socket with the server addressif ( bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ){perror("bind failed");exit(EXIT_FAILURE);}int len, n;len = sizeof(cliaddr); //len is value/resultn = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, ( struct sockaddr *) &cliaddr, &len);buffer[n] = '\0';printf("Client : %s\n", buffer);sendto(sockfd, (const char *)hello, strlen(hello), MSG_CONFIRM, (const struct sockaddr *) &cliaddr, len);printf("Hello message sent.\n");return 0;}