JAVA程式碼 通過Socket讀取返回的資料
阿新 • • 發佈:2018-12-19
- package net.java2000.net;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.InetAddress;
- import java.net.Socket;
- /**
- * 通過Socket讀取返回的資料。
- *
- * @author 趙學慶,Java世紀網(java2000.net)
- *
- */
- publicclass SocketGet {
- publicstaticvoid main(String[] args) {
- try {
- // 建立連線
- InetAddress addr = InetAddress.getByName(
- int port = 25;
- Socket socket = new Socket(addr, port);
- // 如果超過這個時間沒有新的資料,則不再繼續等待資料
- socket.setSoTimeout(1000);
- // 建立讀取資料的Reader,裡面指定了需要的編碼型別。
- BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream(),
- "UTF-8"));
- // 讀取每一行的資料.注意大部分埠操作都需要互動資料。
- String str;
- while ((str = rd.readLine()) != null) {
- System.out.println(str);
- }
- rd.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- 220163.com Anti-spam GT for Coremail System (163com[071018])
- java.net.SocketTimeoutException: Read timed out
- at java.net.SocketInputStream.socketRead0(Native Method)
- at java.net.SocketInputStream.read(Unknown Source)
- at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
- at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
- at sun.nio.cs.StreamDecoder.read(Unknown Source)
- at java.io.InputStreamReader.read(Unknown Source)
- at java.io.BufferedReader.fill(Unknown Source)
- at java.io.BufferedReader.readLine(Unknown Source)
- at java.io.BufferedReader.readLine(Unknown Source)
- at net.java2000.net.SocketGet.main(SocketGet.java:31)