170320网络编程 udpclient udpGroupClient
1.udp是无连接的协议
2.udpClient.java
import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import javax.swing.*; public class udp_client extends JFrame{ JPanel jpbottom,jpfirst,jpsecond,jpthird; JList jlistContent; JScrollPane jspContent; JLabel jlbAddress,jlbPort,jlbSend; JTextField jtxtAddress,jtxtPort,jtxtSend; JButton jbConnect,jbSend; DatagramSocket socket=null; boolean isExit=false; DefaultListModel<String> dlm=null; public udp_client(){ this.setLocation(200,200); this.setSize(500,600); this.setLayout(new BorderLayout()); this.setTitle("udp客户端程序"); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); jlistContent=new JList(); jspContent=new JScrollPane(jlistContent); this.add(jspContent,BorderLayout.CENTER); jpbottom=new JPanel(); jpbottom.setLayout(new GridLayout(3,1)); jpfirst=new JPanel(); jlbAddress=new JLabel("服务器地址:"); jlbPort=new JLabel("服务器端口:"); //jbConnect=new JButton("连接"); jtxtAddress=new JTextField(15); jtxtPort=new JTextField(5); jpfirst.add(jlbAddress); jpfirst.add(jtxtAddress); jpfirst.add(jlbPort); jpfirst.add(jtxtPort); //jpfirst.add(jbConnect); jpbottom.add(jpfirst); jpsecond=new JPanel(); jlbSend=new JLabel("发送信息:"); jtxtSend=new JTextField(30); jbSend=new JButton("发送"); jbSend.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //先获取发送地址和发送端口号 //创建一个临时的套接字进行发送,不需要指定端口,任何一个都行 DatagramSocket sendSocket=null; try { sendSocket=new DatagramSocket(); String sendAddress=jtxtAddress.getText().trim(); int sendPort=Integer.parseInt(jtxtPort.getText().trim()); InetAddress address=InetAddress.getByName(sendAddress); byte[] sendbuffer=jtxtSend.getText().trim().getBytes(); DatagramPacket sendPacket=new DatagramPacket(sendbuffer,0,sendbuffer.length,address,sendPort); sendSocket.send(sendPacket); } catch (Exception e1) { // TODO Auto-generated catch block setJlist("发送信息失败!!!!"+e1.getMessage()); }finally{ sendSocket.close(); } } }); jpsecond.add(jlbSend); jpsecond.add(jtxtSend); jpsecond.add(jbSend); jpbottom.add(jpsecond); this.add(jpbottom,BorderLayout.SOUTH); this.addWindowListener(new WindowListener(){ @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub isExit=true; if(socket!=null){ try{ socket.close(); }catch(Exception e2){ e2.printStackTrace(); } } } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } }); dlm=new DefaultListModel<String>(); try { socket=new DatagramSocket(8001); setJlist("初始化socket开启成功"); //接受信息 new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub while(!isExit){ byte[] buffer=new byte[1024]; //字节数组+整数值 DatagramPacket udpPacket=new DatagramPacket(buffer,buffer.length); try { socket.receive(udpPacket); //收到的数据 //将收到的字节转化为字符串,添加偏移量 String receiveData=new String(udpPacket.getData(),0,udpPacket.getLength()); setJlist(receiveData); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); setJlist("接受数据失败"); socket.close(); break; } } } }).start(); } catch (SocketException e1) { // TODO Auto-generated catch block socket=null; setJlist("绑定8000端口失败!!!!!"+e1.getMessage()); } this.setVisible(true); } private void setJlist(String info){ dlm.addElement(info); jlistContent.setModel(dlm); } public static void main(String[]args){ new udp_client(); } }
3.UdpGroupClient.java
package zhbit.udp.group; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.MulticastSocket; import java.net.SocketException; import java.net.UnknownHostException; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class UdpGroupClient extends JFrame { String groupAddress="230.0.0.1"; int groupPort=8000; JList<String> jlist; JScrollPane jsp; JPanel jp1,jp2,jp3; JLabel jbl1,jbl2,jbl3,jbl4,jbl5; JTextField jtx,jtxIP,jtxPort; JButton jbSend,jbJoin,jbExit; DefaultListModel<String> dlm=null; MulticastSocket socket=null; boolean isExit=false; public UdpGroupClient() { this.setSize(500,600);//大小 this.setTitle("聊天窗体"); this.setLocation(100, 100);//显示位置 this.setLayout(new BorderLayout());//布局 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);//关闭操作 jlist=new JList<String>(); jsp=new JScrollPane(jlist); this.add(jsp,BorderLayout.CENTER); jp1=new JPanel(); jp1.setLayout(new GridLayout(3,1)); jp2=new JPanel(); jbl1=new JLabel("发送地址:"); jbl2=new JLabel("发送端口:"); jbJoin=new JButton("加入"); jbExit=new JButton("退出"); jbExit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { socket.leaveGroup(InetAddress.getByName(groupAddress)); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); jbJoin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try { InetAddress address=InetAddress.getByName(groupAddress); socket.joinGroup(address); socket.setLoopbackMode(false); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while(!isExit) { byte[] buffer=new byte[1024]; DatagramPacket udpPackage=new DatagramPacket(buffer,buffer.length); try { socket.receive(udpPackage); //把接收到的数据转化为字符串 String receiveInfo=new String(udpPackage.getData(),0,udpPackage.getLength()); setJList("收到组播信息:"+receiveInfo); } catch (IOException e) { // TODO Auto-generated catch block setJList("接收数据失败!!!!"); break; } } } }).start(); } catch (Exception e1) { // TODO Auto-generated catch block setJList("加入组播组的失败!!!!"); } } }); jbSend=new JButton("发送"); jbSend.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub DatagramSocket sendSocket=null; try { sendSocket=new DatagramSocket(); InetAddress address=InetAddress.getByName(groupAddress); byte[] sendBuffer=jtx.getText().trim().getBytes(); DatagramPacket pack=new DatagramPacket(sendBuffer,0,sendBuffer.length,address,groupPort); sendSocket.send(pack);//发送数据 } catch (Exception ex) { // TODO Auto-generated catch block setJList("发送数据失败!!!"+ex.getMessage()); } finally { sendSocket.close(); } } }); jp2.add(jbJoin); jp2.add(jbExit); jp1.add(jp2); jp3=new JPanel(); jbl3=new JLabel("发送信息:"); jtx=new JTextField(30); jp3.add(jbl3); jp3.add(jtx); jp3.add(jbSend); jp1.add(jp3); this.add(jp1,BorderLayout.SOUTH); this.setVisible(true); this.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub isExit=true; try { //退出组播组 socket.leaveGroup(InetAddress.getByName(groupAddress)); socket.close(); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } }); dlm=new DefaultListModel<String>(); try { socket=new MulticastSocket(groupPort); } catch (IOException e1) { // TODO Auto-generated catch block setJList("初始化组播socket失败!!!"+e1.getMessage()); } } private void setJList(String info) { dlm.addElement(info); jlist.setModel(dlm); } public static void main(String[] args) { // TODO Auto-generated method stub new UdpGroupClient(); } }
4.Unrecognized Windows Sockets error: 0: Cannot bind
端口被占用