《Java就业培训教程》 作者:张孝祥 书中源码
《Java就业培训教程》P374源码
发送程序:UdpSend.java
import java.net.*;
public class UdpSend
{
public static void main(String [] args) throws Exception
{
DatagramSocket ds=new DatagramSocket();
String str="hello world";
DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),
InetAddress.getByName("192.168.0.213"),3000);
ds.send(dp);
ds.close();
}
}
接收程序:UdpRecv.java
import java.net.*;
public class UdpRecv
{
public static void main(String [] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(3000);
byte [] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
ds.receive(dp);
String strRecv=new String(dp.getData(),0,dp.getLength()) +
" from " + dp.getAddress().getHostAddress()+":"+dp.getPort();
System.out.println(strRecv);
ds.close();
}
}
《Java就业培训教程》P378源码
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Chat
{
Frame f=new Frame("我的聊天室");
TextField tfIP=new TextField(15);
List lst=new List(6);
DatagramSocket ds;
/*由于DatagramSocket的构造函数声明可能抛出异常,我们的程序需要用try…catch语句
进行异常捕获处理,所以不能直接在这里调用DatagramSocket的构造函数对ds进行初始化,
我们需要将ds的初始化放在Chat类的构造函数中去完成。*/
public Chat()
{
try
{
ds=new DatagramSocket(3000);
}catch(Exception ex){ex.printStackTrace();}
}
public static void main(String [] args)
{
Chat chat=new Chat();
chat.init();
}
public void init()
{
f.setSize(300,300);
f.add(lst);
Panel p=new Panel();
p.setLayout(new BorderLayout());
p.add("West",tfIP);
TextField tfData=new TextField(20);
p.add("East",tfData);
f.add("South",p);
f.setVisible(true);
f.setResizable(false);//限制用户改变窗口的大小
//增加关闭窗口的事件处理代码
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
ds.close();//程序退出时,关闭Socket,释放相关资源
f.setVisible(false);
f.dispose();
System.exit(0);
}
});
//增加在消息文本框中按下回车键的事件处理代码
tfData.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//取出文本框中的消息字符串,并将其转换成字节数组
byte[] buf;
buf = e.getActionCommand().getBytes();
try
{
DatagramPacket dp= new DatagramPacket(buf,buf.length,
InetAddress.getByName(tfIP.getText()),3000);
ds.send(dp);
}catch(Exception ex){ex.printStackTrace();}
/*上面的Exception的引用变量名不能为e,而是改写成了ex,因为e已经在
actionPerformed方法中作为形式参数变量名被定义过了。*/
((TextField)e.getSource()).setText("");
}
});
}
}
《Java就业培训教程》P380源码
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class Chat
{
Frame f=new Frame("我的聊天室");
TextField tfIP=new TextField(15);
List lst=new List(6);
DatagramSocket ds;。
public Chat()
{
try
{
ds=new DatagramSocket(3000);
}catch(Exception ex){ex.printStackTrace();}
new Thread(new Runnable()
{
public void run()
{
byte buf[]=new byte[1024];
DatagramPacket dp= new DatagramPacket(buf,1024);
while(true)
{
try
{
ds.receive(dp);
lst.add(new String(buf,0,dp.getLength())+
":from"+dp.getAddress().getHostAddress(),0);
}catch(Exception e){e.printStackTrace();}
}
}
}).start();
}
……
}
《Java就业培训教程》P385源码
import java.net.*;
import java.io.*;
public class TcpServer
{
public static void main(String [] args)
{
try
{
ServerSocket ss=new ServerSocket(8001);
Socket s=ss.accept();
InputStream ips=s.getInputStream();
OutputStream ops=s.getOutputStream();
ops.write("welcome to www.it315.org!".getBytes());
byte [] buf = new byte[1024];
int len = ips.read(buf);
System.out.println(new String(buf,0,len));
ips.close();
ops.close();
s.close();
ss.close();
}catch(Exception e){e.printStackTrace();}
}
}
《Java就业培训教程》P388源码
import java.net.*;
import java.io.*;
class Servicer implements Runnable
{
Socket s;
public Servicer(Socket s)
{
this.s = s;
}
public void run()
{
try
{
InputStream ips=s.getInputStream();
OutputStream ops=s.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ips));
DataOutputStream dos = new DataOutputStream(ops);
while(true)
{
String strWord = br.readLine();
//System.out.println(strWord +":" + strWord.length());
if(strWord. equalsIgnoreCase("quit"))
break;
String strEcho = (new StringBuffer(strWord).reverse()).toString();
//dos.writeBytes(strWord +"---->"+ strEcho +"/r/n");
dos.writeBytes(strWord + "---->"+ strEcho +
System.getProperty("line.separator")); }
br.close();
//关闭包装类,会自动关闭包装类中所包装的底层类。所以不用调用ips.close()
dos.close();
s.close();
}catch(Exception e){e.printStackTrace();}
}
}
class TcpServer
{
public static void main(String [] args)
{
try
{
ServerSocket ss=new ServerSocket(8001);
while(true)
{
Socket s=ss.accept();
new Thread(new Servicer(s)).start();
}
//ss.close();
}catch(Exception e){e.printStackTrace();}
}
}
《Java就业培训教程》P391源码
import java.net.*;
import java.io.*;
public class TcpClient
{
public static void main(String [] args)
{
try
{
//Socket s=new Socket(InetAddress.getByName("192.168.0.213"),8001);
if(args.length < 2)
{
System.out.println("Usage:java TcpClient ServerIP ServerPort");
return;
}
Socket s=new Socket(
InetAddress.getByName(args[0]),Integer.parseInt(args[1]));
InputStream ips=s.getInputStream();
OutputStream ops=s.getOutputStream();
BufferedReader brKey =
new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos = new DataOutputStream(ops);
BufferedReader brNet = new BufferedReader(new InputStreamReader(ips));
while(true)
{
String strWord = brKey.readLine();
dos.writeBytes(strWord + System.getProperty("line.separator"));
if(strWord.equalsIgnoreCase("quit"))
break;
else
System.out.println(brNet.readLine());
}
dos.close();
brNet.close();
brKey.close();
s.close();
}catch(Exception e){e.printStackTrace();}
}
}