Socket Programming with threads
In this project , I have work over sockets in which I create an project. My project is over how multiple clients send requests to the server and server send the solution to the respective clients. For this project I use tcp/ip protocol and POSIX Threads. In This project I transfer file from server to client. The purpose of using thread is so that many client can access the server simultaneously. So that no client is wait for server request. Because Threads can executes parallel so many clients can access the server same time continuosly.
This done by both using INET and UNIX. In UNIX it is within the network and in INET it is over the network.
Firstly, I created server. The system calls use for creating the server are:
- Socket: This is to create socket for communication. This returns the socket descriptor.
- Bind: This is to basically give name to socket & assign port to socket.
- Listen: This creates queue in which clients wait for there connection. In this we can select the size of our server,s wait queue. It can defines how many client can wait in the queue.
- Accept: This provide another socket descriptor through which server can chat with its clients.
Then, I created several clients. The system call for client are:
- Socket:To create socket.
- Connect:This to provide socket descriptor through which it can chat with server.
In this project, the server accept the connections from different clients and do work as per there request. This project is basically for transfer of file from server to client. client ask for a file from server . Server first check whether the client is legal or not. Because first server ask for the password from the client . If it type the write right password, then server follow the further proceedings .After that clients demand for file . Then server check for that file by opening in read only mode. If file exits,then server send the confirmation to the client. Then client creates that file and open write only mode.
Here server reads data from that file and write into client socket descriptor .On the other hand, clients reads the data through the socket descriptor and write it into the file descriptor of the file.
In this way, I can able to transfer the file from one system to another using the INET. This is the file transfer over the network.
I also use thread for connecting multiple clients to the server. When the server receive accept call from any client it creates thread. This Thread runs the functions which reads the data from the file and write the data in the socket descriptor.
For this project, I use TCP protocol for the transmission of data between client and server. Because TCP is more secure than UDP. In TCP, before transferring the data over the network there is secure connection is created between client and server. After the connection is created then data is start transferring between client and server.