Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSCI 4300/5300 Project1 Fall 2018 Inter-Process Socket Communication under Linux

ID: 3756736 • Letter: C

Question

CSCI 4300/5300 Project1 Fall 2018

Inter-Process Socket Communication under Linux OS

Objectives:
1. Study Inter-Process Communication on Linux OS
2. Gain experience with an industry standard OS
3. Use client server architecture for inter-process communication
4. Work with multiple client connections
5. Gain experience with socket programming on Linux OS


Due: Oct. 19, 11:00 PM, Fri.


You are going to investigate the socket communication and programming under Linux OS. Based on the general framework of the Client - Server architecture, you are going to implement a simplified application of inter-process socket communication under Linux OS.

The implementation should consist of the server side and client side. The server side should provide the following major functions:
1) Create a socket for communication
2) Bind the socket to an address (“localhost” and “IP address + port #” or ). If this server will be accessed by other computers on the Internet, an address consists of a port number and IP address of this server.
3) Listen for connections
4) Accept a connection from a client.
5) Send and receive data
6) Create processes to hand multiple connections concurrently
7) Close the connection when a client stops its communication.
The major function of the client side includes:
1. Create a socket for communication
2. Connect the server socket
3. Send and receive data repeatedly
4. Close the connection when communication stops.
It is fine that your implementation is based on the other existing work/source codes. But you have to acknowledge them by providing references and URLs that used in your work.

Figure 1 (a) and (b) are sample screenshots for communication between 1 server, three local clients and one remote client. However, Figure 1 only show one message sent by the client. Your client should let a user send messages as many times as he/she wants. Your results don’t have to be the same as this but your project is supposed to work similar to this.


Figure 1(a): The server and three local clients


Figure 1(b): The remote client
Figure 1: Screenshots of communication among one server (upper-left), three local clients and one remote client.


Submission:
You will submit one ZIP file to Blackboard, which includes the followings:
(1) Your source codes and *.exe files for both server and client
(2) A simple report in MS Word includes:
a) A simple flowchart of the server (you can scan it and then insert it to your word report)
b) A simple flowchart of the client (you can scan it and then insert it to your word report)
c) The procedure and commands of how to compile, link and run the codes
d) The screenshots of your results including the server, multiple clients, or remote clients and their communications repeatedly.
e) List URLs and references you have used in this project

You should ZIP all these documents into a single file and submit it. The name of your submitted zip file should be your Blackboard login.

Grading Rubrics:

Requirements
Points Possible

Investigate socket communication on Linux OS
5

Report
25

Enough Comments in source codes
5

Server starts successfully
5

Server listens for connections
5

Sever accepts a connection from a client
5

Server sends and receives data to/from a client
5

Server creates processes to handle multiple client connections concurrently
5

Server closes the socket when communication stops
5

Client starts successfully
5

Client can connect to the server
5

Client sends and receive data repeatedly to/from a server
10

Client closes the socket when communication stops
5

The server has a different IP address from the clients
10

Total Points
100

How to Run a Program on Linux

1. Restart the computer and choose Linux OS (Ubuntu).

Username: student

Password:Fall2018

2. To run a simple C program

(1) Click on “Apps Button” at the left bottom on the screen. The 1st page of Apps appear, you need to click on the round button(on the right) to switch to the next page of Apps. Look for “Text Editor” icon and open the Text Editor.

(2) To run a hello world C sample program ‘hello.c’, type the following codes in to the text editor

// 'Hello World!' program

#include <stdio.h>

int main()

{

printf("Hello World!");

printf(" ");

return 0;

}

(3) Click “Save”. When it asks where to save, select “Home” (Student) folder (the default folder for a student). Do not change the default folder for a student. Choose to save this file as “hello.c”.

(4) Click on “Apps Button” again to find “Terminal” icon (next to “Text Editor” icon). Open it.

You can use “ls” command to list all files and folders under the student home folder.

student@ad-Hp-EliteDesk-705-GI-MT: ~ $ ls

(5) Compile: use this command to compile the source code and generate an output file called hello.o

gcc -c hello.c -o hello.o // -c: compile file; -o: output

(6) Link:

gcc hello.o -o hello// for linking; -o: output; hello is an executable file

(7) Run: now type in the command to run the program

./hello// run it

3. To run a C program to create a process

(1) To run process.c

Create process.c file, make sure that you put this file under the home folder. In the terminal, type in the following commands:

  gcc -c process.c -o process.o //compile the source code and output it to process.o

  gcc process.o -o process// for linking

  ./process // run it

(2) In process.c, create different processes by changing the parameters in:

execlp("/bin/ls","ls",NULL)

Paths for different commands and applications:

ls: /bin

vdir: /bin

firefox: /usr/bin

xcalc: /usr/bin

You can use the command “cd” to go to the desired folder, e.g.: cd usrin

You can use the command “ls” to list the files and directories under the current folder.

Note: you can not use “ls, vdir, firefox, xcalc” again in your lab

4. To run a C program to create a thread

Make sure that you put the thread.c file under the home folder. In the terminal, type in the   

following commands:

gcc -c thread.c -o thread.o //compile the source code and output it to thread.o

gcc -pthread thread.o -o thread// link it with the pthread libraries

./thread X//to run it, where x is an argument that needs to be passed in,  

like: ./thread 5

Note: you can not use the same runner function in your lab.

5. Reference

“CJkR9.png” is a Unix command line "cheat sheet" suitable for a student to use as a wall paper. It covers several of the important commands.

5:36 Notifications CSCI 4300/5300 Fall 2018 Inter-Process Socket Communication under Linux OS .Study Inter-Process Communication on Linux 2 Gain experience with an industry standard OS Use client server architecture for inter-process 4. Work with multiple client connections 5. Gain experience with socket programming on Linux OS Due: Oct. 19, 11:00 PM, Fri. You are going to communication and programming under Linux OS. Based on the general framework of the Client- Server architecture, you are going to implement a simplified application of inter-process socket communication under Linux OS. investigate the socket The implementation should consist of the server side and client side. The server side should provide the following major functions: Create a socket for communication 2) Bind the socket to an address ("localhost' and P address + port or ). If this server will be accessed by other computers on the Internet, an address consists of a port number and IP address of this server Listen for connections 4 Accept a connection from a client. s Send and receive data Create processes to hand multiple connections Close the connection when a client stops its The major function of the client side includes: Create a socket for communication

Explanation / Answer

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

    char recvBuff[1024];

    struct sockaddr_in serv_addr;

    if(argc != 2)

    {

        printf(" Usage: %s <ip of server> ",argv[0]);

        return 1;

    }

    memset(recvBuff, '0',sizeof(recvBuff));

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)

    {

        printf(" Error : Could not create socket ");

        return 1;

    }

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

    serv_addr.sin_family = AF_INET;

    serv_addr.sin_port = htons(5000);

    if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)

    {

        printf(" inet_pton error occured ");

        return 1;

    }

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

    {

       printf(" Error : Connect Failed ");

       return 1;

    }

    while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)

    {

        recvBuff[n] = 0;

      if(fputs(recvBuff, stdout) == EOF)

        {

            printf(" Error : Fputs error ");

        }

    }

    if(n < 0)

    {

        printf(" Read error ");

    }

    return 0;

}