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
Asked
Active
Viewed 2,789 times
0
-
1What have you tried so far? WHat errors did you get? Can you show some code? – Fjodr Jun 23 '15 at 14:06
-
So you can access the data with a simple HTTP request? – Kuurde Jun 23 '15 at 14:09
-
2possible duplicate of : http://stackoverflow.com/questions/5371943/reading-from-a-url-connection-java – nafas Jun 23 '15 at 14:12
-
I use the code that I found in this page and it did not work : http://stackoverflow.com/questions/25665423/use-java-to-connect-ethernet-device – Masoud Jun 23 '15 at 16:16
-
yes I can send and receive data by using a web browser like chrome . – Masoud Jun 23 '15 at 16:18
1 Answers
0
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);

Iman
- 1,017
- 10
- 26
-
I used this code but didn't work . actually it has some problem in : String oneLineOfInput = br.readLine(); because the code stop at this line ( no error ) . – Masoud Jun 24 '15 at 07:23