标题:java.net.UnknownHostException
只看楼主
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
结帖率:100%
 问题点数:0 回复次数:3 
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 
2007-10-04 12:11
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
得分:0 

源程序:
/////////////////////////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


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-10-04 12:18
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
得分:0 

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


可惜不是你,陪我到最后
2007-10-04 16:35
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
得分:0 
谢谢千里冰封,解决了

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-10-04 19:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-174978-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.325585 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved