Send and receive data to/from a local IP address in Java

2.9k views Asked by At

I'm new to java and now I have a problem in using Ethernet port in Java . I have a device which can send and receive data by Ethernet . I connect it to a switch and when I enter its IP address in a browser I can see the data that the device sent ( the data is some HTML code ). But now I want to do it by a java application . If anyone can help me or send some sample code I would be grateful . thanks

1

There are 1 answers

1
Iman On

I think tutorials on socket programming will help you.
https://docs.oracle.com/javase/tutorial/networking/sockets/

A very brief code may look like this:

Socket socket = new Socket(ipAddressOfDevice);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOuputStream();
PrintWriter pw = new PrintWriter(out);
pw.write(yourHttpRequest);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String oneLineOfInput = br.readLine();
System.out.println(oneLineOfInput);