Transfer the File “Client Socket to Server Socket” in Java
This article describes a one-way client and Server Setup where a client connects, and sends the file to the server and the server writes the file in another location with a different name. It means we send the file using the server socket.
Socket Programming in Java
Socket Programming is used for communicating different JRE in the different networks in java. In simple words, we can say socket programming is connecting two nodes in different networks and communicating two each other.
File Transfer Implementation in Java Socket
In this example, we will create client.java class in this class we make the Socket object and define the server socket port number for communication. In this class, we select which file send over the network.
- To connect to another machine we need two pieces of information first one is the IP address and the second one is the port number.
- In our case, we are using localhost and the port is 900
- We make a Socket object using the java.net package
Socket ob = new Socket(ip,port _ number)
Now we call the “sendFile” method with the parameter of a file path and we open the file and send the file to the server socket using DataOutputStream Class
Here, we define the ServerSocket object using the ServerSocket class.
ServerSocket server = new ServerSocket(port_number)
- when the client sent the request to the server socket. we will call the “receiveFile” method
- We receive the file from the client socket and read the file using the data input stream class
- In this method, we will change the file name and the location of the file. write the file using FileOutputStream Class.
Please Login to comment...