Skip to main content

socketUDPServer.c

0 likes • Aug 5, 2023 • 3 views
C
Loading...

More C Posts

execlp() example

0 likes • Nov 19, 2022 • 0 views
C
#include <stdio.h>
#include <unistd.h>
int main() {
char * cmd[] = {
"who",
"ls",
"date"
};
int i;
printf("0=who 1=ls 2=date : ");
scanf("%d", & i);
execlp(cmd[i], cmd[i], (char * ) 0);
printf("execlp failed\n");
return 0;
}

socketUDPPingServer.c

0 likes • Aug 5, 2023 • 2 views
C
// 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 RECIEVE_BUFFER_SIZE 1024
// Driver code
int main() {
//Create a UDP socket
//int socket(int domain, int type, int protocol);
//Domain is the place where the data goes. AF_BLUETOOTH, AF_INET6, AF_UNIX is local communication between different programs
//Type is the type of connection, so connection based (TCP), connectionless (UDP) or somewhere in between or beyond
//Protocol is an enum or flags that give some more options specific to that type. For SOCK_STREAM, you can force it to use SCTP instead of TCP. For SOCK_RAW, it can give you control over the ip frame, and even the ethernet frame.
//So this function makes a file descriptor to an IPV4 UDP 'connection' with no options.
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
// Creating socket file descriptor
if ( sockfd < 0) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
// Filling server information
serverAddress.sin_family = AF_INET; // IPv4
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT);
// Bind the socket with the server address
if(bind(sockfd, (const struct sockaddr*)&serverAddress, sizeof(serverAddress))){ //Bind returns 0 if succeeded, or -1 if failed. -1 is still true.
perror("bind failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in clientAddress; unsigned int clientAddressSize = sizeof(clientAddress);
memset(&clientAddress, 0, clientAddressSize);
char buffer[RECIEVE_BUFFER_SIZE];
while(1){
int bytesRecieved = recvfrom(sockfd, (char*)buffer, RECIEVE_BUFFER_SIZE, MSG_WAITALL, (struct sockaddr*) &clientAddress, &clientAddressSize); //sets clientAddress to the client's address and the new size
buffer[bytesRecieved] = '\0';
//printf("Client : %s\n", buffer);
//The client might be like PINGUIN or something, but this is to ensure PING\n\r also gets accepted
if(bytesRecieved >= 4 && (strncmp("PING", buffer, 4) == 0)){
const char* responseMessage = "PONG";
sendto(sockfd, (const char*)responseMessage, strlen(responseMessage), MSG_CONFIRM, (const struct sockaddr*) &clientAddress, clientAddressSize);
//printf("Hello message sent.\n");
}else{
printf("Server bytes recieved: %d\nServer message recieved: %s", bytesRecieved, buffer);
}
}
return 0;
}

Bitwise operators with pointers

0 likes • Nov 18, 2022 • 0 views
C
#include <stdio.h>
#include <stdlib.h>
int main(){
int * int_ptr;
int_ptr = (int *)malloc(2*sizeof(int));
if(!int_ptr) {
printf("Something went wrong while allocating memory. Exiting...");
exit(1);
}
printf("Enter first integer: ");
scanf("%i", &int_ptr[0]);
printf("Enter second integer: ");
scanf("%i", &int_ptr[1]);
printf("Original values: 1st = %i 2nd = %i\n",int_ptr[0], int_ptr[1]);
int_ptr[0] = int_ptr[0] ^ int_ptr[1];
//printf("%i\n", int_ptr[0]);
int_ptr[1] = int_ptr[0] ^ int_ptr[1];
//printf("%i\n", int_ptr[1]);
int_ptr[0] = int_ptr[0] ^ int_ptr[1];
//printf("%i\n", int_ptr[0]);
printf("Swapped values: 1st = %i 2nd = %i\n", int_ptr[0], int_ptr[1]);
free(int_ptr);
exit(0);
}

inner fork() variables

0 likes • Nov 19, 2022 • 2 views
C
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int glbvar = 6;
int main() {
int locvar = 88;
pid_t pid;
printf("Before fork()\n");
if ((pid = fork()) == 0) {
/* child */
glbvar++;
locvar++;
} else if (pid > 0) {
/* parent */
sleep(2);
} else
perror("fork error");
printf("pid=%d, glbvar=%d, locvar=%d\n", getpid(), glbvar, locvar);
return 0;
} /* end main */

socketUDPClient.c

0 likes • Aug 5, 2023 • 1 view
C
// 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 code
int main(){
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
// Creating socket file descriptor
if ( (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 information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ){
perror("bind failed");
exit(EXIT_FAILURE);
}
int len, n;
len = sizeof(cliaddr); //len is value/result
n = 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;
}

signal() example

0 likes • Nov 19, 2022 • 0 views
C
#include <stdio.h>
#include <assert.h>
#include <signal.h>
void myHandler(int iSig) {
printf("In myHandler with argument %d\n", iSig);
}
int main() {
void( * pfRet)(int) = signal(SIGINT, myHandler);
assert(pfRet != SIG_ERR);
printf("Entering an infinite loop\n");
while (1) {
printf(".");
}
return 0; // use CTRL+\ to exit
}