Question

1. Write a client program and a server program with multiple clients: Write a server application...

1. Write a client program and a server program with multiple clients:

Write a server application with multiple clients. This means your server program must use threads or fork methods to support multiple clients. You will use socket() and connect() system calls in Unix/Linux/Windows to write an application based on client/server socket programming. You can work on this project using either Java or C/C++. Through this assignment you will learn how to design a simple multithreading client-server programming using Java or C/C++.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

One Server And Multiple Clients

Using The C Programming For Implementation.........

First Write Server code then Client Code Both of the code are given below...

************************************************************************************************************************************

Server Program In C

************************************************************************************************************************************

tcpserver.c

#include"stdio.h"
#include"stdlib.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"netinet/in.h"

#define PORT 4444
#define BUF_SIZE 2000
#define CLADDR_LEN 100

void main() {
 struct sockaddr_in addr, cl_addr;
 int sockfd, len, ret, newsockfd;
 char buffer[BUF_SIZE];
 pid_t childpid;
 char clientAddr[CLADDR_LEN];
 
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 if (sockfd < 0) {
  printf("Error creating socket!\n");
  exit(1);
 }
 printf("Socket created...\n");
 
 memset(&addr, 0, sizeof(addr));
 addr.sin_family = AF_INET;
 addr.sin_addr.s_addr = INADDR_ANY;
 addr.sin_port = PORT;
 
 ret = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
 if (ret < 0) {
  printf("Error binding!\n");
  exit(1);
 }
 printf("Binding done...\n");

 printf("Waiting for a connection...\n");
 listen(sockfd, 5);

 for (;;) { //infinite loop
  len = sizeof(cl_addr);
  newsockfd = accept(sockfd, (struct sockaddr *) &cl_addr, &len);
  if (newsockfd < 0) {
   printf("Error accepting connection!\n");
   exit(1);
  }
  printf("Connection accepted...\n");

  inet_ntop(AF_INET, &(cl_addr.sin_addr), clientAddr, CLADDR_LEN);
  if ((childpid = fork()) == 0) { //creating a child process

   close(sockfd); 
//stop listening for new connections by the main process. 
//the child will continue to listen. 
//the main process now handles the connected client.

   for (;;) {
    memset(buffer, 0, BUF_SIZE);
    ret = recvfrom(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, &len);
    if(ret < 0) {
     printf("Error receiving data!\n");  
     exit(1);
    }
    printf("Received data from %s: %s\n", clientAddr, buffer); 

    ret = sendto(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, len);   
    if (ret < 0) {  
     printf("Error sending data!\n");  
     exit(1);  
    }  
    printf("Sent data to %s: %s\n", clientAddr, buffer);
   }
  }
  close(newsockfd);
 }
}

************************************************************************************************************************************

Client Program In C

************************************************************************************************************************************

tcpclient.c

#include"stdio.h"  
#include"stdlib.h"  
#include"sys/types.h"  
#include"sys/socket.h"  
#include"string.h"  
#include"netinet/in.h"  
#include"netdb.h"
  
#define PORT 4444 
#define BUF_SIZE 2000 
  
int main(int argc, char**argv) {  
 struct sockaddr_in addr, cl_addr;  
 int sockfd, ret;  
 char buffer[BUF_SIZE];  
 struct hostent * server;
 char * serverAddr;

 if (argc < 2) {
  printf("usage: client < ip address >\n");
  exit(1);  
 }

 serverAddr = argv[1]; 
 
 sockfd = socket(AF_INET, SOCK_STREAM, 0);  
 if (sockfd < 0) {  
  printf("Error creating socket!\n");  
  exit(1);  
 }  
 printf("Socket created...\n");   

 memset(&addr, 0, sizeof(addr));  
 addr.sin_family = AF_INET;  
 addr.sin_addr.s_addr = inet_addr(serverAddr);
 addr.sin_port = PORT;     

 ret = connect(sockfd, (struct sockaddr *) &addr, sizeof(addr));  
 if (ret < 0) {  
  printf("Error connecting to the server!\n");  
  exit(1);  
 }  
 printf("Connected to the server...\n");  

 memset(buffer, 0, BUF_SIZE);
 printf("Enter your message(s): ");

 while (fgets(buffer, BUF_SIZE, stdin) != NULL) {
  ret = sendto(sockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &addr, sizeof(addr));  
  if (ret < 0) {  
   printf("Error sending data!\n\t-%s", buffer);  
  }
  ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL);  
  if (ret < 0) {  
   printf("Error receiving data!\n");    
  } else {
   printf("Received: ");
   fputs(buffer, stdout);
   printf("\n");
  }  
 }
 
 return 0;    
}  

************************************************************************************************************************************

How To Execute This Programs

--terminal 1--
gcc tcpserver.c -o server
./server

--terminal 2--
gcc tcpclient.c -o client
./client 192.168.0.4

--terminal 3--
./client 192.168.0.4

  • Note that 192.168.0.4 is the IP address of the machine in which the server is running.

***********************************************************************************************************************************

Add a comment
Know the answer?
Add Answer to:
1. Write a client program and a server program with multiple clients: Write a server application...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • want code in C: and please answer the question! Goal: To improve the client-server model introduced...

    want code in C: and please answer the question! Goal: To improve the client-server model introduced in the class Requirements: The server should be able to accept requests from multiple clients and communicate with them. .Each client should be able to communicate multiple rounds of messages with the server. .Whenever the server receives a message from a client, it replies with "I got your message To implement the server with multithreading. .A client program should terminate its communication with the...

  • Practice socket programming with threads: Write an 'echo' server using UDP. (This server does not need...

    Practice socket programming with threads: Write an 'echo' server using UDP. (This server does not need to be multi-threaded, but make sure that you do know how to implement a multi-threaded server when asked.) Each request is handled by replying to the client with the unmodified string the client sent. Also, write an 'echo' client to test your server. Each client will send 20 sequentially numbered messages to the server & receive the replies. This code needs to be in...

  • To develop a client/server application using TCP sockets and the C programming language that is capable...

    To develop a client/server application using TCP sockets and the C programming language that is capable of supporting multiple concurrent service requests from different clients. PROBLEM You are to use the Ubuntu operating system as well as both the client and the server programs. You are to modify your server program to process requests from more than one client concurrently. This means different clients may request either the same service or a total different one. The services supported by your...

  • implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of...

    implement the follwing code using command promp or Eclipse or any other program you are familiar with. keep the name of the classes exatcly the same as requested for testing purpose. please follow each instruction provided below Objective of this assignment o get you famililar with developing and implementing TCP or UDP sockets. What you need to do: I. Implement a simple TCP Client-Server application 2. and analyze round trip time measurements for each of the above applications 3. The...

  • In this lab you will write a simple chat application in Python using TCP sockets. The...

    In this lab you will write a simple chat application in Python using TCP sockets. The chat application consists of a chat server and a chat client. At any one time, the chat server is communicating with just one chat client. When the client and the server are done chatting, they exchange end messages which ends the session. The client and server read and write Unicode strings using the readUTF() and writeUTF() methods of DataInputStream and DataOutputStream respectively. After compilation,...

  • Write two C programs that run a server program and a client program concurrently. Server program:...

    Write two C programs that run a server program and a client program concurrently. Server program: The server program provides a simple search for a specific value in an array sent to it from a client. If the value appears in the array, the server indicates the index of the first occurrence of that value in the array. The server sends the client search value and its array position. If the value does not occur in the array, only the...

  • Using the programming language of your choice, write a client application which: Connects to a network...

    Using the programming language of your choice, write a client application which: Connects to a network service, then Asks for version information, then Receives the response, then Disconnects and Prints out the response. Detail: The program will take no input from the user. When your program is executed, it should connect to IP address 64.183.98.170 on port 3800. Upon successful connection, send the string: version\n where \n is an end of line marker. The server will respond with a string...

  • In this assignment, you design a simple chat room in the form of a network application which uses the services of a TCP/IP computer network. Your design should have a clientserver architecture in whic...

    In this assignment, you design a simple chat room in the form of a network application which uses the services of a TCP/IP computer network. Your design should have a clientserver architecture in which the server is multi-threaded. Then, you need to implement the server-side of the chat-room application in Java (implementing the client-side is optional). The server maintains a list (an ArrayList will work well) of all the active connections. It will listen on a port for a new...

  • Write a program that shows the operation of TCP/IP socket. You are allowed to use any...

    Write a program that shows the operation of TCP/IP socket. You are allowed to use any programming language. You have to submit your code with a screenshot of the result after running the code. Useful links: https://www.javaworld.com/article/2077322/core-java-sockets-programming-in-java-atutorial.html https://www.c-sharpcorner.com/article/socket-programming-in-cpp-using-boost-asio-tcpserver-and-client/

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT