1. Write a TCP client and TCP server program in C language on UNIX operating system. The
client program begins by sending a request, after accepting the client request; server program
sends back a confirmation and its clock time to the client. Client program displays the server
clock time on its screen. The maximum concurrent clients this server can handle are four.
Display necessary messages, wherever necessary.
(20 marks)
Ans:
Header file Explanation
#include <stdio.h> /* printf() and fprintf() */
#include <sys/types.h /* Socket data types */
#include <sys/socket.h> /* socket(), connect(), send(), recv() */
#include <netinet/in.h> /* IP Socket data types */
#include <arpa/inet.h> /* sockaddr_in, inet_addr() */
#include <stdlib.h> /* atoi() */
#include <string.h> /* memset() */
#include <unistd.h> /* close() */
TCP Client Program
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
printf("This is the client program\n");
int sockfd;
int len, rc ;
struct sockaddr_in address;
int result;
char ch = 'A';
//Create socket for client.
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("Socket create failed.\n") ;
return -1 ;
}
//Name the socket as agreed with server.
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(7734);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1)
{
perror("Error has occurred");
exit(-1);
}
while ( ch < 'Y') {
//Read and write via sockfd
rc = write(sockfd, &ch, 1);
printf("write rc = %d\n", rc ) ;
if (rc == -1) break ;
read(sockfd, &ch, 1);
printf("Char from server = %c\n", ch);
//if (ch == 'A') sleep(5) ; // pause 5 seconds
}
close(sockfd);
exit(0);
}
TCP Server Program
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
/// SERVER
int main(int argc, char *argv[])
{ //Declaring process variables.
int server_sockfd, client_sockfd;
int server_len ;
int rc ;
unsigned client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
//Remove any old socket and create an unnamed socket for the server.
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htons(INADDR_ANY);
server_address.sin_port = htons(7734) ;
client program begins by sending a request, after accepting the client request; server program
sends back a confirmation and its clock time to the client. Client program displays the server
clock time on its screen. The maximum concurrent clients this server can handle are four.
Display necessary messages, wherever necessary.
(20 marks)
Ans:
Header file Explanation
#include <stdio.h> /* printf() and fprintf() */
#include <sys/types.h /* Socket data types */
#include <sys/socket.h> /* socket(), connect(), send(), recv() */
#include <netinet/in.h> /* IP Socket data types */
#include <arpa/inet.h> /* sockaddr_in, inet_addr() */
#include <stdlib.h> /* atoi() */
#include <string.h> /* memset() */
#include <unistd.h> /* close() */
TCP Client Program
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
printf("This is the client program\n");
int sockfd;
int len, rc ;
struct sockaddr_in address;
int result;
char ch = 'A';
//Create socket for client.
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("Socket create failed.\n") ;
return -1 ;
}
//Name the socket as agreed with server.
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(7734);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1)
{
perror("Error has occurred");
exit(-1);
}
while ( ch < 'Y') {
//Read and write via sockfd
rc = write(sockfd, &ch, 1);
printf("write rc = %d\n", rc ) ;
if (rc == -1) break ;
read(sockfd, &ch, 1);
printf("Char from server = %c\n", ch);
//if (ch == 'A') sleep(5) ; // pause 5 seconds
}
close(sockfd);
exit(0);
}
TCP Server Program
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
/// SERVER
int main(int argc, char *argv[])
{ //Declaring process variables.
int server_sockfd, client_sockfd;
int server_len ;
int rc ;
unsigned client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
//Remove any old socket and create an unnamed socket for the server.
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htons(INADDR_ANY);
server_address.sin_port = htons(7734) ;
server_len = sizeof(server_address);
rc = bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
printf("RC from bind = %d\n", rc ) ;
//Create a connection queue and wait for clients
rc = listen(server_sockfd, 5);
printf("RC from listen = %d\n", rc ) ;
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
printf("after accept()... client_sockfd = %d\n", client_sockfd) ;
while(1)
{
char ch;
printf("server waiting\n");
//Accept a connection
//client_len = sizeof(client_address);
//client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
//printf("after accept()... client_sockfd = %d\n", client_sockfd) ;
//Read write to client on client_sockfd
rc = read(client_sockfd, &ch, 1);
printf("RC from read = %d\n", rc ) ;
if (ch=='X') break ;
ch++;
write(client_sockfd, &ch, 1);
}
printf("server exiting\n");
//close(client_sockfd);
close(client_sockfd);
return 0;
}
2.
(a) Write the step by step procedure to configure a remote server and transfer a Directory to
Remote Server in Linux.
(10 marks)
Ans:
A remote server is a server on another computer (remote host).
To configure access to the server in this set-up, you need to specify the following:
- Connection settings: server host, port, and user credentials.
- The server configuration root folder and the URL address to access it.
- Correspondence between the project root folder, the folder on the server to copy the data from the project root folder to, and the URL address to access the copied data on the server. This correspondence is called mapping.
Creating a Server Configuration: Specifying Its Name and Type
A. Open the Deployment dialog box. Do one of the following:
- Open the Settings dialog box by choosing File | Settings or pressing Ctrl+Alt+S and click Deployment under Build, Execution, Deployment.
- Choose Tools | Deployment | Configuration on the main menu.
B. In the left-hand pane, that shows a list of all the existing server configurations, click the Add
toolbar button . The Add Server dialog box opens.
C. Specify the server configuration name in the Name text box. From the Type drop-down list,
choose the server configuration type depending on the protocol you are going to use to
exchange the data with the server.
- FTP: choose this option to have PhpStorm access the server via the FTP file transfer protocol.
- SFTP: choose this option to have PhpStorm access the server via the SFTP file transfer protocol.
- FTPS: choose this option to have PhpStorm access the server via the FTP file transfer protocol over SSL (the FTPS extension).
- When editing the server configuration name in the Name text box, use the Up and Down keys on your keyboard to change the preselected server access to type in the Protocol drop-down list.
D. Click OK. The Add Server dialog box closes and you return to the Connection tab of the
Deployment dialog box.
- Click the Use as Default toolbar button.
Ftp server is used to transfer files between server and clients. All major operating system supports
ftp. ftp is the most used protocol over internet to transfer files. Like most Internet operations, FTP
works on a client/ server model. FTP client programs can enable users to transfer files to and from a
remote system running an FTP server program.
Configuring the ftp Server
The vsftpd RPM package is required to configure a Red Hat Enterprise Linux system as an ftp server.
If it is not already installed, install it with rpm commands as described in our pervious article. After it
is installed, start the service as root with the command service vsftpd start . The system is now an ftp
server and can accept connections. To configure the server to automatically start the service at boot
time, execute the command chkconfig vsftpd on as root. To stop the server, execute the command
service vsftpd stop. To verify that the server is running, use the command service vsftpd status.
(b) Write a step by step procedure to create and configure samba Server in Linux. Also,
transfer files from client side.
Ans:
Installing Samba
1. Use yum to install the Samba package:
- yum -y install samba
Creating Samba Test Directory and Files
For this part of the procedure, you'll use the su - (switch user) command to work as root. Although it’s
not best practice to do this regularly, there are times where it's much more practical to work directly as
root instead of trying to use sudo to do everything. This is one of those times.
You're going to create a new directory containing three empty files which you'll share using Samba.
2. While logged on as root, create the new directory /smbdemo with the following command:
mkdir /smbdemo
3. Change the permissions on the new directory to 770 with the following command:
chmod 770 /smbdemo
4. Navigate to the new directory with the following command:
cd /smbdemo
5. Add three empty files to the directory with the following command:
touch file1 file2 file3
Adding the Samba User
You must add users to the Samba database in order for them to have access to their home directory
and other Samba shares.
6. Use the following command to add a new Samba user (the new Samba user must be an existing
Linux user or the command will fail):
smbpasswd -a <username>
For example, to add the user don, use the command smbpasswd -a don.
Creating the Samba Group
7. Perform the following steps to create a smbusers group, change ownership of the /smbdemo
directory, and add a user to the smbusers group:
groupadd smbusers
chown :smbusers /smbdemo
usermod -G smbusers don
Configuring Samba
Samba configuration is done in the file /etc/samba/smb.conf. There are two parts to
/etc/samba/smb.conf:
- Global Settings: This is where you configure the server. You’ll find things like authentication method, listening ports, interfaces, workgroup names, server names, log file settings, and similar parameters.
- Share Definitions: This is where you configure each of the shares for the users. By default, there’s a printer share already configured.
Configuring smb.conf
8. In the Global Settings section, at line 74, change the workgroup name to your workgroup name.