当前位置: 代码迷 >> Java相关 >> java.net.UnknownHostException
  详细解决方案

java.net.UnknownHostException

热度:950   发布时间:2007-10-04 12:11:50.0
java.net.UnknownHostException
java.net.UnknownHostException
Thrown to indicate that the IP address of a host could not be determined.
主机的IP地址无法确定,请问要如何解决这个问题。
搜索更多相关的解决方案: java  主机  host  address  Thrown  

----------------解决方案--------------------------------------------------------

源程序:
/////////////////////////ChatServer.java//////////////////////////////////////
import java.io.*;
import java.net.*;
public class ChatServer {
public static void main(String[] str)
{
ServerSocket soc;
Socket svrSoc;
ObjectOutputStream objOut=null;
ObjectInputStream objIn=null;
try
{
System.out.println("正在等待客户机连接。。。");
soc=new ServerSocket(8080);
svrSoc=soc.accept();
System.out.println("客户机已经连接成功,你想退出请敲击‘QUIT’");
objOut=new ObjectOutputStream(svrSoc.getOutputStream());
objIn=new ObjectInputStream(svrSoc.getInputStream());
}
catch (Exception e)
{
System.exit(0);

}
SendMsgOut send =new SendMsgOut(objOut);
send.start();
GetMsgFromClient get=new GetMsgFromClient(objIn);
get.start();
}

}
class SendMsgOut extends Thread
{
ObjectOutputStream objOut=null;
public SendMsgOut(ObjectOutputStream out)
{
objOut=out;
}
public void run()
{
String strMsg="";
while (true)
{
try
{
strMsg=(new BufferedReader(new InputStreamReader(System.in))).readLine();

objOut.writeObject(strMsg);
if(strMsg.equals("QUIT"));
System.exit(0);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

}
class GetMsgFromClient extends Thread
{
ObjectInputStream objIn;
public GetMsgFromClient(ObjectInputStream in)
{
objIn=in;
}
public void run()
{
String strMsg="";
while (true)
{
try
{
strMsg=(String)objIn.readObject();
System.out.println("客户"+strMsg);


if(strMsg.equals("QUIT"));
System.exit(0);
}
catch (Exception e)
{

}
}
}
}


////////////////////////////ChatClient.java////////////////////////////////
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatClient {
public static void main(String[] str)
{
String serverName=new Scanner(System.in).nextLine();
if(serverName.trim()=="")
{
System.out.println("你输入的服务器名称或地址不能为空!");
System.exit(0);

}
Socket cltSoc;
ObjectOutputStream objOut=null;
ObjectInputStream objIn=null;
try
{
InetAddress address =InetAddress.getByName(serverName);
cltSoc=new Socket(address,8080);
objOut=new ObjectOutputStream(cltSoc.getOutputStream());
objIn=new ObjectInputStream(cltSoc.getInputStream());
System.out.println("成功连接服务器");
System.out.println("如想退出,请敲‘QUIT’");

}
catch(Exception e)
{
System.out.println(""+e);
System.exit(0);

}
GetMsg get=new GetMsg(objIn);
get.start();
SendMsg send=new SendMsg(objOut);
send.start();
}

}
class GetMsg extends Thread
{
ObjectInputStream objIn;
public GetMsg(ObjectInputStream in)
{
objIn=in;

}
public void run()
{
String strMsg="";
while(true)
{
try
{
strMsg=(String)objIn.readObject();
System.out.println("服务器:"+strMsg);
if(strMsg.equals("QUIT"))
System.exit(0);
}
catch(Exception e)
{

}
}
}
}
class SendMsg extends Thread
{
ObjectOutputStream objOut;
public SendMsg(ObjectOutputStream out)
{
objOut=out;
}
public void run()
{
String strMsg="";
while (true)
{
try
{
strMsg=(new BufferedReader(new InputStreamReader(System.in))).readLine();

objOut.writeObject(strMsg);
if(strMsg.equals("QUIT"));
System.exit(0);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}


都可以编译,但执行ChatClient.java的时候,输入内容就会出现java.net.UnknownHostException


----------------解决方案--------------------------------------------------------

主机名无法识别,是不是输了非法的IP地址


----------------解决方案--------------------------------------------------------
谢谢千里冰封,解决了
----------------解决方案--------------------------------------------------------
  相关解决方案