You are on page 1of 17

1. A) CLIENT PROGRAM TCP/IP SOCKET PROGRAMMING.

// Client side C/C++ program to demonstrate Socket programming


#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include<unistd.h>
#include<arpa/inet.h>
#define PORT 8080

int main(int argc, char const *argv[])


{
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}

memset(&serv_addr, '0', sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary form


if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}

if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) <


0)
{
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
valread = read( sock , buffer, 1024);
printf("%s\n",buffer );
return 0;
}
1 B) SERVER PROGRAM TCP/IP SOCKET PROGRAMMING.
// Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";

// Creating socket file descriptor


if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}

// Forcefully attaching socket to the port 8080


if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );

// Forcefully attaching socket to the port 8080


if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
send(new_socket , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
return 0;
}

OUTPUT:

CLIENT SIDE

SERVER SIDE

2. A) FIFO CLIENT PROGRAM

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

//
// Demonstrates blocking using UNIX open, read, write, close
//

int main(void) {
puts("Client");
char* strMessage[] = {"Welcome", "to", "sjce", "named.", "pipe",
"server", "client", "program","this","will","stop","now"};

//Open fifo for write


int fd = open("/tmp/demo6_fifo", O_WRONLY);
if (fd == -1) {
perror("Cannot open fifo");
return EXIT_FAILURE;
}

//Write each string in turn


for (unsigned int n=0; n<12; n++) {
char L = (char)strlen(strMessage[n]);
write(fd, &L, 1);
//Send string length
write(fd, strMessage[n], strlen(strMessage[n])); //Send
string characters
}

//Tidy
close(fd);
return EXIT_SUCCESS;
}

2. B) FIFO SERVER PROGRAM

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main(void) {
puts("Server - listening");

//Create fifo
unlink("/tmp/demo6_fifo");
int code = mkfifo("/tmp/demo6_fifo", 0666);
if (code == -1) {
perror("mkfifo returned an error - file may already exist");
}

//Open read end


int fd = open("/tmp/demo6_fifo", O_RDONLY);
if (fd == -1) {
perror("Cannot open FIFO for read");
return EXIT_FAILURE;
}
puts("FIFO OPEN");

//Read string (upto 255 characters)


char stringBuffer[256]; //String buffer
memset(stringBuffer, 0, 256); //Fill with zeros

/*
while (strcmp(stringBuffer, "STOP") != 0) {
char L;
read(fd, &L, 1); //Get
string length
read(fd, stringBuffer, L); //Read
string characters
stringBuffer[L] = 0; //Zero
terminator
printf("Server received: %s\n", stringBuffer);
}
*/
int res;
char Len;
while(1) {
res = read(fd, &Len, 1);
if (res == 0) {
break;
}
read(fd, stringBuffer, Len); //Read
string characters
stringBuffer[(int)Len] = 0;
//Zero terminator
printf(" %s", stringBuffer);
}

puts("EOF found");

//Tidy up
close(fd);
puts("FIFO Closed");

return EXIT_SUCCESS;
}

OUTPUT:
CLIENT OUTPUT
SERVER OUTPUT

3. A) MESSAGE QUEUE CLIENT PROGRAM


// C Program for Message Queue (Reader Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>

// structure for message queue


struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;

int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);

// msgget creates a message queue


// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);

// msgrcv to receive message


msgrcv(msgid, &message, sizeof(message), 1, 0);

// display the message


printf("Data Received is : %s \n",
message.mesg_text);

// to destroy the message queue


msgctl(msgid, IPC_RMID, NULL);

return 0;
}
3. B) MESSAGE QUEUE SERVER PROGRAM
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <arpa/inet.h>

// structure for message queue


struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;

int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);

// msgget creates a message queue


// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;

printf("Write Data : ");


gets(message.mesg_text);

// msgsnd to send message


msgsnd(msgid, &message, sizeof(message), 0);

// display the message


printf("Data send is : %s \n", message.mesg_text);

return 0;
}

OUTPUT
CLIENT OUTPUT
SERVER OUTPUT

4. A) SHARED MEMORY CLIENT PROGRAM


/* Filename: shm_read.c */
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#define BUF_SIZE 1024
#define SHM_KEY 0x1234

struct shmseg {
int cnt;
int complete;
char buf[BUF_SIZE];
};

int main(int argc, char *argv[]) {


int shmid;
struct shmseg *shmp;
shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644|IPC_CREAT);
if (shmid == -1) {
perror("Shared memory");
return 1;
}
// Attach to the segment to get a pointer to it.
shmp = shmat(shmid, NULL, 0);
if (shmp == (void *) -1) {
perror("Shared memory attach");
return 1;
}

/* Transfer blocks of data from shared memory to stdout*/


while (shmp->complete != 1) {
printf("segment contains : \n\"%s\"\n", shmp->buf);
if (shmp->cnt == -1) {
perror("read");
return 1;
}
printf("Reading Process: Shared Memory: Read %d bytes\n", shmp-
>cnt);
sleep(3);
}
printf("Reading Process: Reading Done, Detaching Shared Memory\n");
if (shmdt(shmp) == -1) {
perror("shmdt");
return 1;
}
printf("Reading Process: Complete\n");
return 0;
}

4. B) SHARED MEMORY SERVER PROGRAM


/* Filename: shm_write.c */
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

#define BUF_SIZE 1024


#define SHM_KEY 0x1234

struct shmseg {
int cnt;
int complete;
char buf[BUF_SIZE];
};
int fill_buffer(char * bufptr, int size);

int main(int argc, char *argv[]) {


int shmid, numtimes;
struct shmseg *shmp;
char *bufptr;
int spaceavailable;
shmid = shmget(SHM_KEY, sizeof(struct shmseg), 0644|IPC_CREAT);
if (shmid == -1) {
perror("Shared memory");
return 1;
}

// Attach to the segment to get a pointer to it.


shmp = shmat(shmid, NULL, 0);
if (shmp == (void *) -1) {
perror("Shared memory attach");
return 1;
}

/* Transfer blocks of data from buffer to shared memory */


bufptr = shmp->buf;
spaceavailable = BUF_SIZE;
for (numtimes = 0; numtimes < 5; numtimes++) {
shmp->cnt = fill_buffer(bufptr, spaceavailable);
shmp->complete = 0;
printf("Writing Process: Shared Memory Write: Wrote %d bytes\n",
shmp->cnt);
bufptr = shmp->buf;
spaceavailable = BUF_SIZE;
sleep(3);
}
printf("Writing Process: Wrote %d times\n", numtimes);
shmp->complete = 1;

if (shmdt(shmp) == -1) {
perror("shmdt");
return 1;
}

if (shmctl(shmid, IPC_RMID, 0) == -1) {


perror("shmctl");
return 1;
}
printf("Writing Process: Complete\n");
return 0;
}

int fill_buffer(char * bufptr, int size) {


static char ch = 'A';
int filled_count;

//printf("size is %d\n", size);


memset(bufptr, ch, size - 1);
bufptr[size-1] = '\0';
if (ch > 122)
ch = 65;
if ( (ch >= 65) && (ch <= 122) ) {
if ( (ch >= 91) && (ch <= 96) ) {
ch = 65;
}
}
filled_count = strlen(bufptr);

//printf("buffer count is: %d\n", filled_count);


//printf("buffer filled is:%s\n", bufptr);
ch++;
return filled_count;
}

OUTPUT:

CLIENT OUTPUT
SERVER OUTPUT

5. A) FORK PROGRAM
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{

// make two process which run same


// program after this instruction
fork();
fork();
fork();

printf("Hello world!\n");
return 0;
}

5. B) WAIT PROGRAM

// C program to demonstrate working of wait()


#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>

int main()
{
if (fork()== 0)

printf("HC: hello from child\n");


else
{
printf("HP: hello from parent\n");

wait(NULL);
printf("CT: child has terminated\n");
}

printf("Bye\n");
return 0;
}

OUTPUT:
FORK OUTPUT

WAIT OUTPUT
6. PIPE OPEN AND CLOSE
#include <stdio.h>
#include<stdlib.h>

int main(void)
{
FILE *pipein_fp, *pipeout_fp;
char readbuf[80];

/* Create one way pipe line with call to popen() */


if (( pipein_fp = popen("ls", "r")) == NULL)
{
perror("popen");
exit(1);
}

/* Create one way pipe line with call to popen() */


if (( pipeout_fp = popen("sort", "w")) == NULL)
{
perror("popen");
exit(1);
}

/* Processing loop */
while(fgets(readbuf, 80, pipein_fp))
fputs(readbuf, pipeout_fp);

/* Close the pipes */


pclose(pipein_fp);
pclose(pipeout_fp);

return(0);
}

OUTPUT:
7. READ PROGRAM
// C program to illustrate
// read system Call
#include<stdio.h>
#include <fcntl.h>
int main()
{
int fd, sz;
char *c = (char *) calloc(100, sizeof(char));

fd = open("foo1.txt", O_RDONLY);
if (fd < 0) { perror("r1"); exit(1); }

sz = read(fd, c, 10);
printf("called read(% d, c, 10). returned that"
" %d bytes were read.\n", fd, sz);
c[sz] = '\0';
printf("Those bytes are as follows: % s\n", c);
}

OUTPUT:
8. WRITE PROGRAM

// C program to illustrate
// write system Call
#include<stdio.h>
#include <fcntl.h>
main()
{
int sz;

int fd = open("foo.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);


if (fd < 0)
{
perror("r1");
exit(1);
}

sz = write(fd, "SJCE\n", strlen("SJCE\n"));

printf("called write(% d, \"SJCE\\n\", %d)."


" It returned %d\n", fd, strlen("SJCE\n"), sz);

close(fd);
}

OUTPUT:

You might also like