Java中封裝了大量的socket API
import java
import java
import
public class uclient
{
private DatagramSocket cli;
private DatagramPacket pac;
private byte sb[];
private String sen;
public uclient()
{
Init();
}
public void Init()
{
try
{
//指定端口號
cli=new DatagramSocket(
sb=new byte[
sen=
sb=sen
pac=new DatagramPacket(sb
InetAddress
cli
}
catch(SocketException se)
{
se
}
catch(IOException ie)
{
ie
}
}
public static void main(String args[])
{
new uclient();
}
}
接收端數據:
import java
import java
import
public class userve
{
private DatagramSocket ser;
private DatagramPacket pac;
private byte rb[];
private String rev;
public userve()
{
Init();
}
public void Init()
{
try
{
ser=new DatagramSocket(
rb=new byte[
pac=new DatagramPacket(rb
rev=
int i=
while(i==
//無數據
{
ser
i=pac
//接收數據
if(i>
{
//指定接收到數據的長度
可使接收數據正常顯示
rev=new String(rb
System
i=
}
}
}
catch(Exception e)
{
e
}
}
public static void main(String args[])
{
new userve();
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26023.html