DatagramSocket编程

DatagramSocket编程importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.IOException;importjava.ne

大家好,又见面了,我是你们的朋友全栈君。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Talk extends JFrame{
	JLabel la=new JLabel("请输入");
	JTextField tfd=new JTextField(20);
	JButton but=new JButton("发送");
	JPanel p=new JPanel();
	JTextArea jta=new JTextArea(10,20);
	Container c;
	DatagramSocket ds;
	DatagramReceive dc;
	int localPost=3000;
	int remotePost=5000;
	boolean flag=true;
	public void init(){
		c=this.getContentPane();
		c.setLayout(new BorderLayout());
		p.setLayout(new FlowLayout());
		p.add(la);p.add(tfd);p.add(but);
		c.add(p,BorderLayout.NORTH);
		c.add(jta,BorderLayout.CENTER);
		but.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String temp=tfd.getText();
				jta.append("\n我说:"+temp);
				byte[] buf=temp.getBytes();
				try {
					DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("localhost"),remotePost);
					try {
						ds.send(dp);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					if(temp.trim().equals("bye")){
						flag=false;
						//dc.close();
					}
				} catch (UnknownHostException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
	this.setSize(500,500);
	this.setVisible(true);
	}
	public Talk(String name,int localPost,int remotePost){
		super(name);
		this.localPost=localPost;
		this.remotePost=remotePost;
		init();
		try {
			ds=new DatagramSocket(localPost);
			dc=new DatagramReceive(name,ds,jta);
			dc.start();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			System.out.println("DatagramSocket创建失败");
		}
		
	}
	public static void main(String[] args) {
		Talk t1=new Talk("A",3000,5000);
		Talk t2=new Talk("B",5000,3000);
	}

}


import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import javax.swing.JTextArea;

public class DatagramReceive extends Thread{
	String name;
	DatagramSocket ds;
	JTextArea jta;
	boolean flag=true;
	public DatagramReceive(String name,DatagramSocket ds,JTextArea jta){
		this.name=name;
		this.ds=ds;
		this.jta=jta;
	}
		public void run(){
			while(flag){
				byte[] buf=new byte[1024];
				DatagramPacket dp=new DatagramPacket(buf,buf.length);
				try {
					ds.receive(dp);
					String temp=new String(buf,0,dp.getLength());
					jta.append("\n对方说:"+temp);
					if(temp.trim().equals("bye")){
						flag=false;
						ds.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.out.println(name+"接受失败");
				}
				
			}
		}

}


DatagramSocket编程

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/138641.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号