我编写了一个发送数据报的客户端
这就是源码:
import java.io.*;
import java.net.*;
import java.util.*;
public class ClientTest
{
public static void main(String agrs[])
{
DatagramPacket packet;
DatagramSocket socket;
InetAddress address = InetAddress.getByName ("ServerTest");
int port=4700;
byte buf[]=new byte[32];
String s="Hello";
try
{
socket=new DatagramSocket();
s.getBytes(0,s.length(),buf,0);
packet=new DatagramPacket(buf,32,address,port);
socket.send(packet);
packet=new DatagramPacket(buf,32);
socket.receive(packet);
s=new String(packet.getData(),0);
System.out.println("Received from server:" + s);
socket.close();
}
catch(Exception e)
{
System.out.println("Exception" + e);
}
}
}
///////////////////////////////////////////////////
编译后报错:
异常“java.net.UnknownHostException“未被捕获,因而未出现在throws子句中
//////////////////////////////////////////////////////////
------解决方案--------------------------------------------------------
>>InetAddress.getByName ("ServerTest");
is this "ServerTest" a valid host??
------解决方案--------------------------------------------------------
请问该如何处理呢?
------解决方案--------------------------------------------------------
主机名正确吗?
------解决方案--------------------------------------------------------
搂主,请教客户端程序和服务器端程序要在不同的机器上运行吗?
------解决方案--------------------------------------------------------
楼上不一定,楼主再看看细节代码