• java基础—网络编程———建立聊天的形式




    接收器和发射器的简单演示




    import java.io.*;
    
    import java.net.*;
    
    
    public class SocketDemo {
    
    	public static void main(String[] args) throws Exception {
    		// TODO Auto-generated method stub
    
    		System.out.println("发送端启动");
    		System.out.println(InetAddress.getLocalHost().getHostAddress());
    		DatagramSocket ds = new DatagramSocket();
    		
    		String str = "发送端来了了了";
    		byte[] buf = str.getBytes();
    		DatagramPacket dp = new 	DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.3.4"),10101);
    		ds.send(dp);
    		ds.close();
    	}
    
    }
    

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.SocketException;
    
    
    public class SocketDemo2 {
    
    	public static void main(String[] args) throws Exception {
    		// TODO Auto-generated method stub
           System.out.println("接收端启动");
           DatagramSocket ds = new DatagramSocket(10101);
           byte[] buf = new byte[1024];
           DatagramPacket dp = new DatagramPacket(buf,buf.length);
           ds.receive(dp);
           String ip = dp.getAddress().getHostAddress();
           int port = dp.getPort();
           String text  = new String(dp.getData(),0,dp.getLength());
           System.out.println(ip+"   "+port);
           System.out.println(text);
           ds.close();
           /*
            * 因为UDP协议数据传输,仅仅管发送数据。而无论接收是否可以接收到数据,因此应该先启动接收端程序,再启动发送端程序*/
    	}
    
    }
    

    双窗体聊天模式:



    建立发送端:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    
    
    public class SocketChatDemoInput 
    {
    
    	public static void main(String[] args) throws Exception 
    	{
    		// TODO Auto-generated method stub
    		System.out.println("发送端启动");
    		DatagramSocket ds = new DatagramSocket();
    		BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
    		String line = null;
    		while((line = bufr.readLine())!=null)
    		{
    			byte[] buf = line.getBytes();
    			DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.3.4"),10102);
    			ds.send(dp);
    			if("over".equals(line))
    				break;
    		}
    		ds.close();
    
    	}
    
    }
    

    建立接收端

    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.SocketException;
    
    
    public class SocketChatOutput {
    
    	public static void main(String[] args) throws Exception {
    		// TODO Auto-generated method stub
    
    		System.out.println("接收端启动");
    		DatagramSocket ds = new DatagramSocket(10102);
    		while(true)
    		{
    			byte[] buf = new byte[1024];
    			DatagramPacket dp = new DatagramPacket(buf,buf.length);
    			ds.receive(dp);
    			String ip =dp.getAddress().getHostAddress();
    			int inpor = dp.getPort();
    			String text = new String(dp.getData(),0,dp.getLength());
    			
    			System.out.println(ip+"  "+ inpor+"    
    "+text);
    		}
    	
    		
    	}
    
    }
    


    单窗体聊天模式

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    
    
    class Send implements Runnable
    {
    	private DatagramSocket ds ;
    	public Send(DatagramSocket ds)
    	{
    		this.ds= ds;
    	}
    	public void run()
    	{
    		try
    		{
    			System.out.println("发送端启动");
    			BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
    			String line = null;
    			while((line = bufr.readLine())!=null)
    			{
    				byte[] buf = line.getBytes();
    				DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.3.4"),10103);
    				ds.send(dp);
    				if("over".equals(line))
    					{
    					System.out.println("聊天程序退出");
    					   break;
    					}
    			}
    			ds.close();
    		}catch(Exception e)
    		{
    			System.out.println(e.toString());
    		}
    	}
    }
    class Rece implements Runnable
    {
    	DatagramSocket  ds;
    	Rece(DatagramSocket ds)
    	{
    		this.ds = ds;
    	}
    	public void run()
    	{
    		
    		while(true)
    		{
    			System.out.println("接收端启动");
    			byte[] buf = new byte[1024];
    			DatagramPacket dp = new DatagramPacket(buf,buf.length);
    			try 
    			{
    				 ds.receive(dp);
    			} catch (IOException e) 
    			{
    		
    				e.printStackTrace();
    			}
    			String ip = dp.getAddress().getHostAddress();
    			int port = dp.getPort();
    			String text = new String(dp.getData(),0,dp.getLength());
    			System.out.println(ip+"   "+ port+"   
    "+text);
    		}
    	}
    	
    	}
    
    public class SocketGroupChat 
    {
    
    	public static void main(String[] args) throws Exception 
    	{
    		// 单窗体聊天模式
    
    		DatagramSocket send = new DatagramSocket();
    		DatagramSocket rece = new DatagramSocket(10103);
    		
    		Send s = new Send(send);
    		Rece r = new Rece(rece);
    		
    		new Thread(s).start();
    		new Thread(r).start();
    		
    	}
    
    }
    














  • 相关阅读:
    复旦大学2016--2017学年第二学期(16级)高等代数II期末考试第八大题解答
    复旦大学2016--2017学年第二学期高等代数II期末考试情况分析
    16 级高代 II 思考题十的多种证明
    16 级高代 II 思考题九的七种解法
    Jordan 块的几何
    实对称阵可对角化的几种证明及其推广
    复旦大学高等代数在线课程2017--2018学年记录
    复旦高等代数II(16级)每周一题
    复旦大学2016--2017学年第一学期(16级)高等代数I期末考试第六大题解答
    复旦大学2016--2017学年第一学期(16级)高等代数I期末考试第七大题解答
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5044232.html
Copyright © 2020-2023  润新知