Page 202 - 捷運技術 第28期
P. 202
196 28 92 2
class Udp1 extends Thread
{
int cpsPort;
DatagramSocket cpsSocket;
DatagramPacket iinput;
DatagramPacket ooutput;
InetAddress spsaddr;
int spsport;
byte[] bf;
byte[] bbf;
public Udp1 (int spot)
{cpsPort=spot;
bf =new byte[64];
try { iinput=new DatagramPacket(bf,bf.length);
cpsSocket=new DatagramSocket(cpsPort) ; }
catch (IOException e)
{ System.err.println(e);} }
public void run()
{while (true)
{ try{ cpsSocket.receive(iinput);
spsaddr=iinput.getAddress();
spsport= iinput.getPort();
String s=new String(iinput.getData(),0,iinput.getLength());
System.out.println(s);
String ss= Ack ;
bbf=ss.getBytes();
ooutput=new DatagramPacket(bbf,bbf.length,spsaddr,spsport);
cpsSocket.send(ooutput); }
catch (IOException e)
{ System.err.println(e);}
} //end while
} //end run
} // Udp1 end
cps sps