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
- 1What have you tried so far? WHat errors did you get? Can you show some code?Fjodr– Fjodr2015-06-23 14:06:37 +00:00Commented Jun 23, 2015 at 14:06
- So you can access the data with a simple HTTP request?Kuurde– Kuurde2015-06-23 14:09:48 +00:00Commented Jun 23, 2015 at 14:09
- 2possible duplicate of : stackoverflow.com/questions/5371943/…nafas– nafas2015-06-23 14:12:54 +00:00Commented Jun 23, 2015 at 14:12
- I use the code that I found in this page and it did not work : stackoverflow.com/questions/25665423/…Masoud– Masoud2015-06-23 16:16:29 +00:00Commented Jun 23, 2015 at 16:16
- yes I can send and receive data by using a web browser like chrome .Masoud– Masoud2015-06-23 16:18:03 +00:00Commented Jun 23, 2015 at 16:18
Add a comment |
1 Answer
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); 1 Comment
Masoud
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 ) .