0% found this document useful (0 votes)
19 views

VND Openxmlformats-Officedocument Wordprocessingml Document&rendition 1

Uploaded by

sanjayyalla4661
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

VND Openxmlformats-Officedocument Wordprocessingml Document&rendition 1

Uploaded by

sanjayyalla4661
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

2) Implementation of Connection oriented concurrent service (TCP).

DESCRIPTION: A sequential server, a concurrent server has to be able to serve more than one client
at a time. For example, a chat server may be serving a specific client for hours--it cannot wait till it stops
serving a client before it serves the next one. This requires a significant change in our flowchart:
We moved the serve from the daemon process to its own server process. However, because each
child process inherits all open files (and a socket is treated just like a file), the new process
inherits not only the “accepted handle,” i.e., the socket returned by the accept call, but also the
top socket, i.e., the one opened by the top process right at the beginning.

However, the server process does not need this socket and should close it immediately.
Similarly, the daemon process no longer needs the accepted socket, and not only should, but
must close it--otherwise, it will run out of available file descriptors sooner or later.

After the server process is done serving, it should close the accepted socket. Instead of returning
to accept, it now exits.

 Under UNIX®, a process does not really exit. Instead, it returns to its parent. Typically, a
parent process waits for its child process, and obtains a return value. However, our daemon
process cannot simply stop and wait. That would defeat the whole purpose of creating additional
processes. But if it never does wait, its children will become zombies--no longer functional but
still roaming around.
 For that reason, the daemon process needs to set signal handlers in its initialize daemon
phase. At least a SIGCHLD signal has to be processed, so the daemon can remove the zombie
return values from the system and release the system resources they are taking up.
 That is why our flowchart now contains a process signals box, which is not connected to
any other box. By the way, many servers also process SIGHUP, and typically interpret as the
signal from the superuser that they should reread their configuration files. This allows us to
change settings without having to kill and restart these servers.

Concurrent Connection-Oriented Servers

• Use TCP for connection-oriented communication.

• Call _process () to process one thread to serve each Client.

ALGORITHM:
1. The concurrent client server model is a model which communicates with the client in
the parallel method of communication .For example let us see at the following syntax i.e
…,”listen(sfd,5);”

2. This means that the server is bind to listen for the clients of maximum 5 at a time. In this
the internal process is done as below.

3. Not like in the normal or iterative client server model the server in the concurrent client
server model communicates with all the 5 clients at the same time using the ability of “fork()

4. That is the fork() is a command that develops duplicate process or a parent and child
process.

5. Like wise the server is like a parent process which creates a child process separately
for all the incoming clients .So that the communication process is completed as in normal or
iterative client server process.

6. This is the main functionality of concurrent client and server model.

(a) Concurrent Server Connection Oriented:

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

main(int argc, char *argv[])

{
int sockfd,new_sockfd,rval,pid;

char buff1[20],buff2[20];

struct sockaddr_in server, client;

int len;

sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

if(sockfd==-1)

perror("\n SOCK_ERR\n");

exit(1);

server.sin_family=AF_INET;

server.sin_addr.s_addr=inet_addr("192.168.0.5");

server.sin_port=htons(3339);

rval=bind(sockfd,(struct sockaddr*)&server,sizeof(server));

if(rval!=-1)

listen(sockfd,5);

while(1)

new_sockfd=accept(sockfd,(struct sockaddr*)&client,&len);
if(new_sockfd!=-1)

pid=fork();

if(pid==0)

printf("\n child process executing \n");

printf("\n child process is is %d", getpid());

len=sizeof(server);

rval=recv(new_sockfd, buff1,sizeof(buff1),0);

if(rval==-1)

perror("\n RECV_ERR\n");

exit(1);

else

printf("\n received message is %s\n", buff1);

rval=send(new_sockfd,buff1,sizeof(buff1),0);

if(rval!=-1)
{

printf("\n message sent successfully \n");

else

perror("\nSEND_ERR\n");

exit(1);

else

printf("\n parent process\n");

printf("parent process id is %d \n", getppid());

exit(1);

else

perror("\n ACCEPT_ERR\n");

exit(1);
}

else

perror("\nBIND_ERR\n");

close(sockfd);

ServerOutput:

[staff@cc5 cnlab]$ cc coserverco.c

[staff@cc5 cnlab]$ ./a.out

parent process

parent process id is 1 child process id is 4397

received message is computer

message sent successfully

(b)Concurrent Client Connection Oreniented

#include <stdio.h>

#include <sys/types.h>
#include <sys/socket.h>

#include<netinet/in.h>

#include <arpa/inet.h>

main(int argc, char *argv[])

int sockfd, rval;

char buff1[20],buff2[20];

struct sockaddr_in server, client;

int len;

sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

if(sockfd==-1)

perror("\n SOCK_ERR\n");

exit(1);

server.sin_family=AF_INET;

server.sin_addr.s_addr=inet_addr("192.168.0.5");

server.sin_port=htons(3339);

rval=connect(sockfd,(struct sockaddr *)&server, sizeof(server));

if(rval!=-1)
{

printf("\n enter a message \n");

scanf("%s", buff1);

rval=send(sockfd,buff1,sizeof(buff1),0);

if(rval==-1)

perror("\n SEND_ERR\n");

exit(1);

rval=recv(sockfd,buff2,sizeof(buff2),0);

if(rval!=-1)

printf("\n Received message is %s \n", buff2);

else

perror("\nRECV_ERR\n");

exit(1);

}
else

printf("\n CONNECT_ERR\n");

exit(1);

Client Output:

[staff@cc5 cnlab]$ cc clientco.c

[staff@cc5 cnlab]$ ./a.out

enter a message

computer

Received message is computer

You might also like