- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Here I am going to connect the server and client, Next, the client enables to share files to the server if the client is connected with the server.
If you working on the same, set the IP address at the client-side 127.0.0.1 otherwise check the servers side machine IP address and make sure one has to connect with the server machine Hotspot.
First, you have to know the client-server programming in java. I have already published client-server code in the previous blog. Click below for check the code - > >> >>>>>
Java client-server programming
STEPS :
The first one has to compile both server and client classes. Next, open a command prompt and run the server file, and after that open the command prompt in another machine that is connected with the machine where the server code is running, and run the client file. if a connection successfully establishes with the server, at the client side ask for a file name after that file-sharing process start, then once the file-sharing process complete, one acknowledgment comes from the server-side. For now, I was written code for one way connection but very soon upload a two-way connection code.
Please share this blog with your friends.
One thing to remember: Client-side changes the IP address by checking the server-side machine and make sure the port number of both the side same.
Client Code :
import java.io.*;
import java.net.*;
import java.util.*;
class client {
public static void main(String[] args) {
try{
Socket s=new Socket("192.168.0.102",12000);
DataOutputStream bout = new DataOutputStream(s.getOutputStream());
DataInputStream bin = new DataInputStream(s.getInputStream());
s.setKeepAlive(true);
Scanner scan = new Scanner(System.in);
System.out.println("Enter file name ");
String filename = scan.next();
File file = new File(filename);
byte[] bArray = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bArray);
fis.close();
bout.write(bArray,0,bArray.length);
System.out.println(bArray.length);
System.out.println(bin.readUTF());
s.close();
}catch(Exception e){System.out.println(e);}
}
}
Server code :
import java.io.*;
import java.net.*;
import java.util.*;
public class server {
public static void main(String[] args){
try{
ServerSocket ss = new ServerSocket(12000);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
//FileOutPutStream fout = new FileOutputStream();
s.setKeepAlive(true);
byte[] bytearray = new byte[1024 * 16];
dis.read(bytearray) ;
FileOutputStream fos = new FileOutputStream("client.java");
fos.write(bytearray);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeUTF("Thank you for sending file");
s.close();
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
More Questions :
- How to make To-Do List in React
- How to Use Bootstrap in React
- How to run python command in java
- Get link
- X
- Other Apps
Comments
Post a Comment
Any Query Regarding Article Ask me in comment