Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Endalkachew Admassu

ID=11,509/17
Comp.Eng(Extention)
Date 28/11/2020
Distributed system Assignment 2
__________________________________________________________________
Question
1. Compare connectionless (UDP) and connection-oriented (TCP) communication for the
implementation of each of the following application-level or presentation-level protocols:
a. virtual terminal access (for example, Telnet);
b. file transfer (for example, FTP);
c. user location (for example, rwho, finger);
d. information browsing (for example, HTTP);
e. remote procedure call.
2. Explain deadlocks and timeout in Interposes communication.
3. Using TCP/IP socket programming develop a client-server chat application
4. Using Java RMI develop a group chat application

Answer
1.) a. The long duration of sessions, the need for reliability and the unstructured sequences of
characters transmitted make connection-oriented communication most suitable for this
application. Performance is not critical in this application, so the overheads are of little
consequence.

b. File calls for the transmission of large volumes of data. Connectionless would be ok if error
rates are low and the messages can be large, but in the Internet, these requirements aren’t
met, so TCP is used.
c. Connectionless is preferable, since messages are short, and a single message is sufficient for
each transaction.
d. Either mode could be used. The volume of data transferred on each transaction can be quite
large, so TCP is used in practice.
e. RPC achieves reliability by means of timeouts and re-trys. so connectionless (UDP)
communication is often preferred.

2.)  Blocking operations issued in the wrong sequence can cause deadlock. Timeout can be used to
detect deadlocks.Connect and receive operations can result in indefinite blocking.For example, a
blocking connect request can result in the requesting process to be suspended indefinitely if the
connection is unfulfilled or cannot be fulfilled,  perhaps as a result of a breakdown in the network .It
is generally unacceptable for a requesting process to “hang” indefinitely.  Indefinite blocking can be
avoided by using timeout.

Page 1 of 8
3.)

// TCP/IP socket programming develop a client-server chat application

// TCP Server Programming

1. #include <stdio.h>
2. #include <netdb.h>
3. #include <netinet/in.h>
4. #include <stdlib.h>
5. #include <string.h>
6. #include <sys/socket.h>
7. #include <sys/types.h>
8. #define MAX 80
9. #define PORT 8080
10. #define SA struct sockaddr
11.
12. // Function designed for chat between client and server.
13. void func(int sockfd)
14. {
15. char buff[MAX];
16. int n;
17. // infinite loop for chat
18. for (;;) {
19. bzero(buff, MAX);
20. // read the message from client and copy it in buffer
21. read(sockfd, buff, sizeof(buff));
22. // print buffer which contains the client contents
23. printf("From client: %s\t To client : ", buff);
24. bzero(buff, MAX);
25. n = 0;
26. // copy server message in the buffer
27. while ((buff[n++] = getchar()) != '\n')
28. ;
29. // and send that buffer to client
30. write(sockfd, buff, sizeof(buff));
31.
32. // if msg contains "Exit" then server exit and chat ended.
33. if (strncmp("exit", buff, 4) == 0) {
34. printf("Server Exit...\n");
35. break;
36. }
37. }
38. }
39.
40. // Driver function
41. int main()
42. {
43. int sockfd, connfd, len;
44. struct sockaddr_in servaddr, cli;
45.
46. // socket create and verification
47. sockfd = socket(AF_INET, SOCK_STREAM, 0);
48. if (sockfd == -1) {
49. printf("socket creation failed...\n");
50. exit(0);

Page 2 of 8
51. }
52. else
53. printf("Socket successfully created..\n");
54. bzero(&servaddr, sizeof(servaddr));
55.
56. // assign IP, PORT
57. servaddr.sin_family = AF_INET;
58. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
59. servaddr.sin_port = htons(PORT);
60. // Binding newly created socket to given IP and verification
61. if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
62. printf("socket bind failed...\n");
63. exit(0);
64. }
65. else
66. printf("Socket successfully binded..\n");
67.
68. // Now server is ready to listen and verification
69. if ((listen(sockfd, 5)) != 0) {
70. printf("Listen failed...\n");
71. exit(0);
72. }
73. else
74. printf("Server listening..\n");
75. len = sizeof(cli);
76.
77. // Accept the data packet from client and verification
78. connfd = accept(sockfd, (SA*)&cli, &len);
79. if (connfd < 0) {
80. printf("server acccept failed...\n");
81. exit(0);
82. }
83. else
84. printf("server acccept the client...\n");
85.
86. // Function for chatting between client and server
87. func(connfd);
88. // After chatting close the socket
89. close(sockfd);
90. }

// TCP client programming


1. #include <netdb.h>
2. #include <stdio.h>
3. #include <stdlib.h>
4. #include <string.h>
5. #include <sys/socket.h>
6. #define MAX 80
7. #define PORT 8080
8. #define SA struct sockaddr
9. void func(int sockfd)
10. {
11. char buff[MAX];
12. int n;
13. for (;;) {
14. bzero(buff, sizeof(buff));

Page 3 of 8
15. printf("Enter the string : ");
16. n = 0;
17. while ((buff[n++] = getchar()) != '\n')
18. ;
19. write(sockfd, buff, sizeof(buff));
20. bzero(buff, sizeof(buff));
21. read(sockfd, buff, sizeof(buff));
22. printf("From Server : %s", buff);
23. if ((strncmp(buff, "exit", 4)) == 0) {
24. printf("Client Exit...\n");
25. break;
26. }
27. }
28. }
29.
30.
31. int main()
32. {
33. int sockfd, connfd;
34. struct sockaddr_in servaddr, cli;
35. // socket create and varification
36. sockfd = socket(AF_INET, SOCK_STREAM, 0);
37. if (sockfd == -1) {
38. printf("socket creation failed...\n");
39. exit(0);
40. }
41. else
42. printf("Socket successfully created..\n");
43. bzero(&servaddr, sizeof(servaddr));
44. // assign IP, PORT
45. servaddr.sin_family = AF_INET;
46. servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
47. servaddr.sin_port = htons(PORT);
48. // connect the client socket to server socket
49. if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
50. printf("connection with the server failed...\n");
51. exit(0);
52. }
53. else
54. printf("connected to the server..\n");
55.
56. // function for chat
57. func(sockfd);
58.
59. // close the socket
60. close(sockfd);
61. }

Page 4 of 8
Output –
Server side:
Socket successfully created..
Socket successfully binded..
Server listening..
server acccept the client...
From client: hi
To client : hello
From client: exit
To client : exit
Server Exit...

Client side:
Socket successfully created..
connected to the server..
Enter the string : hi
From Server : hello
Enter the string : exit
From Server : exit
Client Exit...

Page 5 of 8
4. Using Java RMI develop a group chat application

Server side

// GroupChatInterface
1. import java.rmi.*;
2.  
3. public interface GroupChatInterface extends Remote {
4.  
5. public boolean login (MessengerInterface m) throws RemoteException;
6.  
7. public void sendToAll(String s, MessengerInterface from) throws RemoteException;
8.  
9. public MessengerInterface getMessenger(String username) throws RemoteException;
10.  
11. }

// MessengerInterface.
62. import java.rmi.*;
63.  
64. public interface MessengerInterface extends Remote{
65.  
66. public String getUsername() throws RemoteException;
67.  
68. public void tell(String s) throws RemoteException;
69.  
70. }

// GroupChat Class
1. import java.rmi.*;
2. import java.rmi.server.*;
3. import java.util.*;
4.  
5. public class GroupChat extends UnicastRemoteObject implements GroupChatInterface{
6.
7. private Hashtable l=new Hashtable();
8.
9. public GroupChat() throws RemoteException{ }
10.
11. public boolean login(MessengerInterface m) throws RemoteException{
12. l.put(m.getUsername(), m);
13. m.tell("[Server] Welcome Client "+m.getUsername());
14. return true;
15. }
16. public void sendToAll(String s,MessengerInterface from) throws RemoteException{
17. System.out.println("\n["+from.getUsername()+"] "+s);
18. Enumeration usernames = l.keys();
19. while(usernames.hasMoreElements()){
20. String user=(String) usernames.nextElement();
21. MessengerInterface m=(MessengerInterface)l.get(user);

Page 6 of 8
22. if (user.equals(from.getUsername())){continue;}
23.
24. try{
25. m.tell("\n["+from.getUsername()+"] "+s);
26. }catch(Exception e){e.printStackTrace();}
27. }
28. }
29. public MessengerInterface getMessenger(String username) throws RemoteException{
30. MessengerInterface m=(MessengerInterface)l.get(username);
31. return m;
32. }
33. }

// StartServer Class

1. import java.rmi.*;
2. import java.rmi.server.*;
3.  
4. public class StartServer {
5. public static void main(String[] args) {
6. try {
7. System.setSecurityManager(new RMISecurityManager());
8. GroupChat obj=new GroupChat();
9. Naming.rebind("rmi://localhost/ABCD", obj);
10.  
11. System.out.println("[System] Chat Server is ready.");
12. }catch (Exception e) {
13. System.out.println("Chat Server failed: " + e);
14. }
15. }
16. }

// Create the policy file for the security


1. grant {
2. permission java.security.AllPermission;
3. };

Client Side
//Messenger

1. import java.rmi.*;
2. import java.rmi.server.*;
3.  
4. public class Messenger extends UnicastRemoteObject implements MessengerInterface{
5.  
6. private String username;
7. private GroupChatInterface server;
8. public Messenger(String u, GroupChatInterface s) throws RemoteException {
9. username=u;
10. server=s;
11. }

Page 7 of 8
12. public String getUsername() throws RemoteException{
13. return username;
14. }
15. public void tell(String s) throws RemoteException{
16. System.out.println(s);
17. }
18. }

//StartClient Class

1. import java.rmi.*;
2. import java.rmi.registry.*;
3. import java.rmi.server.*;
4. import java.io.*;
5. import java.util.*;
6.  
7. public class StartClient {
8.  
9. public static void main(String[] args) {
10. try {
11. System.setSecurityManager(new RMISecurityManager());
12. GroupChatInterface server =
(GroupChatInterface)Naming.lookup("rmi://192.1680.4/ABCD");
13. // 192.168.0.4 is server address
14. Scanner scanner=new Scanner(System.in);
15. System.out.println("[System] Client Messenger is running");
16. System.out.println("Enter a username to login and press Enter:");
17. String username = scanner.nextLine();
18. MessengerInterface m=new Messenger(username,server);
19. server.login(m);
20. server.sendToAll("Just Connected",m);
21. for(;;){
22. String aa = scanner.nextLine();
23. server.sendToAll(aa,m);

24. }
25. }catch (Exception e) {
26. System.out.println("Hello Client exception: " + e);
27. e.printStackTrace();
28. }
29. }
30. }

Page 8 of 8

You might also like