java网络编程技术有哪些_Java网络编程技术2

packageNet.UDPchat;importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.IOException;import java.net.*;import javax.swing.*;public class UDPchat implementsActionListener,Runnable {

JFrame jf;

JLabel jl1,jl2,jl3;

JTextField recPortText,sendPortText,IPText,msgText;

JButton startBtn,sendBtn;

JTextArea showArea;

JScrollPane jsp;

JPanel jp1,jp2;

Container con;

Thread thread= null;

DatagramSocket receiveSocket,sendSocket;

DatagramPacket receivePack,sendPack;privateInetAddress sendIP;private intsendPort,recPort;private byteinBuf[],outBuf[];private static final int BUFSIZE = 1024;publicUDPchat(){

jf= new JFrame("聊天————UDP协议");

jl1= new JLabel("接收端口号:");

jl2= new JLabel("发送端口号:");

jl3= new JLabel("对方的地址:");

recPortText= newJTextField();

recPortText.setColumns(5);

sendPortText= newJTextField();

sendPortText.setColumns(5);

IPText= newJTextField();

IPText.setColumns(8);

msgText= newJTextField();

msgText.setColumns(40);

msgText.setEditable(false);

msgText.addActionListener(this);

startBtn= new JButton("开始");

startBtn.addActionListener(this);

sendBtn= new JButton("发送");

sendBtn.setEnabled(false);

sendBtn.addActionListener(this);

showArea= newJTextArea();

showArea.setEditable(false);

showArea.setLineWrap(true); //自动换行

jsp = newJScrollPane(showArea);

jp1= newJPanel();

jp2= newJPanel();

jp1.setLayout(newFlowLayout());

jp2.setLayout(newFlowLayout());

jp1.add(jl1);

jp1.add(recPortText);

jp1.add(jl2);

jp1.add(sendPortText);

jp1.add(jl3);

jp1.add(IPText);

jp1.add(startBtn);

jp2.add(msgText);

jp2.add(sendBtn);

con=jf.getContentPane();

con.add(jp1, BorderLayout.NORTH);

con.add(jsp, BorderLayout.CENTER);

con.add(jp2, BorderLayout.SOUTH);

jf.setSize(600, 400);

jf.setLocation(300, 200);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}//在线程中接收数据

public voidrun() {

String str;while(true){try{

receiveSocket.receive(receivePack);

str= new String(receivePack.getData(),0,receivePack.getLength());

showArea.append("对方说:"+str+"\n");

}catch(IOException e) {

showArea.append("接收数据出错!\n");

}

}

}public voidactionPerformed(ActionEvent e) {try{if(e.getSource()==startBtn){

inBuf= new byte[BUFSIZE];

sendPort=Integer.parseInt(sendPortText.getText());

recPort=Integer.parseInt(recPortText.getText());

sendIP=InetAddress.getByName(IPText.getText());

sendSocket= newDatagramSocket();//创建接收数据包

receivePack = newDatagramPacket(inBuf,BUFSIZE);//指定接收数据的端口

receiveSocket = newDatagramSocket(recPort);//创建线程准备接收对方的信息

thread = new Thread(this);

thread.setPriority(Thread.MIN_PRIORITY);

thread.start();

msgText.setEditable(true);

sendBtn.setEnabled(true);

startBtn.setEnabled(false);

}else{ //按下了“发送”按钮或回车键

outBuf =msgText.getText().getBytes();//组装要发送的数据

sendPack = newDatagramPacket(outBuf,outBuf.length,sendIP,sendPort);//发送数据

sendSocket.send(sendPack);

showArea.append("我说:"+msgText.getText()+"\n");

msgText.setText(null);

}

}catch(NumberFormatException e1) {

e1.printStackTrace();

}catch(UnknownHostException e1) {

showArea.append("无法连接到指定地址\n");

}catch(SocketException e1) {

showArea.append("无法打开指定端口\n");

}catch(IOException e1) {

showArea.append("发送数据失败!\n");

}

}public static voidmain(String[] args) {newUDPchat();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值