2 m0 37198806 m0_37198806 于 2017.01.06 13:39 提问

java.net.UnknownHostException

java程序偶尔出现java.net.UnknownHostException: www.**.com
网络不稳定么?

2个回答

gogo_land
gogo_land   2017.01.06 15:45

dns问题,解析不了域名,局域网内遇到过

sun1021873926
sun1021873926   Ds   Rxr 2017.01.06 17:38

可以参考这篇文章 http://omyyal.iteye.com/blog/1741213

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
linux系统下部署TOMCAT错误java.net.UnknownHostException bogon bogon
linux系统下部署TOMCAT异常错误java.net.UnknownHostException :bogon: bogon 主要原因是主机映射原因 修改hosts文件即可
hadoop2.7.2HA集群安装
hadoop2.7.2HA集群安装
Android调用WebService
最近学习的是在android端如何去调用远程WebService,都说WebService是一种基于SOAP协议的远程调用标准,对于这个协议理解不深,知道webservice可以将不同操作系统平台、不同语言、不同技术整合到一块,android SDK没有直接调用webservice的库,最常用的是借助ksoap2-android这个第三方SDK。该Demo参考《疯狂Android讲义》并做改动,希望可以帮助到初次接触Androi调用WebService的朋友
JAVA 实现 ping
一段JAVA代码 实现ping功能 import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.nio.channels.SocketChannel;
Tools.java
工具类,转义时间和html等!import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.UnknownHostException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import net.sf.json.JSONArray; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.digest.DigestUtils;
TCP并发上传——java源码
import java.io.*; import java.net.InetAddress; import java.net.UnknownHostException; public class ClientDemo { public static void main(String[] args) { try { InputStream in = new FileInputStream("D:\\Client"); byte[] buf = new byte[1024*1024]; int len; ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); while (true) { len = in.read(buf); if (len == -1) { new TCPClient(bufOut.toByteArray(), InetAddress.getLocalHost(), 1211); break; } else { bufOut.write(buf, 0, len); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
javaQQ聊天室+SocketTCP
package ChatRoomTCPQQ聊天室; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JOptionPane; public class ChatRoomClent { private Socket s; public ChatRoomClent(String host,int port){ try { s=new Socket(host,port); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void sendMessage(String str){ PrintWriter pw=null; try { pw=new PrintWriter(s.getOutputStream()); pw.println(str); pw.flush(); } catch (IOException e) { e.printStackTrace(); } } public String reciveMessage(){ BufferedReader br=null; String str=""; try { br=new BufferedReader(new InputStreamReader(s.getInputStream())); str = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } }
类qq聊天工具
1. import java.awt.BorderLayout; 2. import java.awt.Component; 3. import java.awt.Dialog; 4. import java.awt.Dimension; 5. import java.awt.FlowLayout; 6. import java.awt.GridBagConstraints; 7. import java.awt.GridBagLayout; 8. import java.awt.GridLayout; 9. import java.awt.Image; 10. import java.awt.Insets; 11. import java.awt.MenuItem; 12. import java.awt.PopupMenu; 13. import java.awt.Rectangle; 14. import java.awt.SystemTray; 15. import java.awt.Toolkit; 16. import java.awt.TrayIcon; 17. import java.awt.TrayIcon.MessageType; 18. import java.awt.event.ActionEvent; 19. import java.awt.event.ActionListener; 20. import java.awt.event.ComponentAdapter; 21. import java.awt.event.ComponentEvent; 22. import java.awt.event.MouseAdapter; 23. import java.awt.event.MouseEvent; 24. import java.awt.event.WindowAdapter; 25. import java.awt.event.WindowEvent; 26. import java.io.File; 27. import java.io.FileInputStream; 28. import java.io.FileNotFoundException; 29. import java.io.FileOutputStream; 30. import java.io.IOException; 31. import java.net.DatagramPacket; 32. import java.net.DatagramSocket; 33. import java.net.InetAddress; 34. import java.net.SocketException; 35. import java.net.UnknownHostException; 36. import java.util.Date; 37. import java.util.Scanner; 38. import java.util.Stack; 39. import java.util.prefs.Preferences; 40. 41. import javax.swing.BoxLayout; 42. import javax.swing.ImageIcon; 43. import javax.swing.JButton; 44. import javax.swing.JDialog;
java网络编程示例
Java 网络编程 说明: 网络编程,分为两种一种是TCP、另一种是UDP方式。我们分别写一下TCP、UDP端的Server和Client简单演示代码。 TCP: Sever import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class TCPServer { public static void main(String[] args) { try { ServerSocket socket = new ServerSocket(4444); while (true) { Socket sc =socket.accept(); InputStream instr = sc.getInputStream(); DataInputStream dataInputStream = new DataInputStream(instr); System.out.println(dataInputStream.readUTF()); } } catch (IOException e) { e.printStackTrace(); } } } Client import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; public class TCPClient { public static void main(String[] args) { try { Socket socket = new Socket("127.0.0.1",4444); OutputStream oStream = socket.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(oStream); dataOutputStream.writeUTF("Hello"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } UDP: Server: import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; public class UDPServer { public static void main(String[] args) { try { byte [] buffer = new byte[1024]; DatagramSocket socket = new DatagramSocket(4444); DatagramPacket packet = new DatagramPacket(buffer, buffer.length); while(true){ socket.receive(packet); System.out.println(new String(buffer,0,packet.getLength())); } } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } Client import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException; public class UDPClient { public static void main(String[] args) { byte [] buffer = null; buffer = new String("hello").getBytes(); DatagramPacket packet=null; try { packet = new DatagramPacket(buffer, buffer.length,new InetSocketAddress("127.0.0.1", 4444)); DatagramSocket socket = new DatagramSocket(1234); socket.send(packet); socket.close(); } catch (SocketException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
计算机网络实验报告 获取MAC socket通信
服务器端: package com.softeem.socket.server; import java.io.FileNotFoundException; import java.io.IOException; public class FileServer { /** * @param args */ public static void main(String[] args) { // 读取本地文件 try { java.io.FileInputStream fis = new java.io.FileInputStream( new java.io.File("d:\\数据库.zip")); // 转换成网络输出流 java.net.ServerSocket ss = new java.net.ServerSocket(9000); java.net.Socket sk = ss.accept(); //DataOutputStream 处理数据 数据的输出流 java.io.OutputStream os = new java.io.DataOutputStream( sk.getOutputStream()); // 利用网络输出流将文件传到客户端 边读边发送 int len=0; byte[] buff = new byte[1024]; while((len=fis.read(buff))!=-1){ os.write(buff, 0, len); os.flush(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 客户端: package com.softeem.socket.client; import java.io.FileNotFoundException; import java.io.IOException; import java.net.UnknownHostException; public class FileClient { /** * @param args */ public static void main(String[] args) { // 文件的输出流 try { java.io.FileOutputStream fos = new java.io.FileOutputStream( new java.io.File("e:\\database.zip ")); // 网络的输入流 java.net.Socket sk = new java.net.Socket("192.168.1.101",9000); java.io.InputStream is = new java.io.DataInputStream( sk.getInputStream()); // 接受网络输入流 写入文件 int len = 0; byte[] buff = new byte[1024]; while((len=is.read(buff))!=-1){ fos.write(buff, 0, len); fos.flush(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 有源码和体会,写的很详细和完整。