Skip to main content

replaceString

0 likes • Nov 14, 2021 • 0 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;
}

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
}

socketUDPPingClient.c

0 likes • Aug 5, 2023 • 0 views
C
// 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 code
int main() {
char buffer[MAXLINE];
struct sockaddr_in servaddr;
// Creating socket file descriptor
int 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 second
timeout.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 information
servaddr.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 to
if(bytesRecieved >= 0){
buffer[bytesRecieved] = '\0';
printf("Recieved %s\n", buffer);
}else{
printf("Recieved nothing: Packet Dropped\n", buffer);
}
}
close(sockfd);
return 0;
}

socketTCPClient.c

0 likes • Aug 5, 2023 • 0 views
C
// compile: gcc socketTCPClient.c -o socketTCPClient.out
// usage : ./socketTCPClient.out port
// Doesn't send anything, just connects and prints out the response from the server
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[]){
int sockfd = 0, n = 0, portno;
char recvBuff[1025];
struct sockaddr_in serv_addr;
memset(recvBuff, '0', sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("socket error\n");
exit(EXIT_FAILURE);
}
serv_addr.sin_family = AF_INET;
portno = atoi(argv[1]);
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
printf("connect error\n");
exit(EXIT_FAILURE);
}
while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0){
recvBuff[n] = 0;
if (fputs(recvBuff, stdout) == EOF)
{
printf("fputs error\n");
}
}
if(n < 0){
printf("read error\n");
}
return 0;
}

pipe() example

0 likes • Nov 19, 2022 • 0 views
C
#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 */
}
}

socketUDPServer.c

0 likes • Aug 5, 2023 • 3 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
const char* responseMessage = "Hello from server";
// 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];
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);
sendto(sockfd, (const char*)responseMessage, strlen(responseMessage), MSG_CONFIRM, (const struct sockaddr*) &clientAddress, clientAddressSize);
printf("Hello message sent.\n");
return 0;
}