Skip to main content

Heavy Coin

0 likes • Oct 4, 2023 • 2 views
C
Loading...

More C Posts

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;
}

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 */

wait() example

0 likes • Nov 19, 2022 • 0 views
C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
char * cmd[] = {
"who",
"ls",
"date"
};
int i;
while (1) {
printf("0=who 1=ls 2=date : ");
scanf("%d", & i);
if (fork() == 0) {
/* child */
execlp(cmd[i], cmd[i], (char * ) 0);
printf("execlp failed\n");
exit(1);
} else {
/* parent */
wait((int * ) 0);
printf("child finished\n");
}
} /* while */
} /* main */

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 */
}
}

socketTCPServer.c

0 likes • Aug 5, 2023 • 0 views
C
// 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;
}

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;
}