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+"    \n"+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+"   \n"+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();
		
	}

}














早起的年轻人 CSDN认证博客专家 移动开发 项目管理 Java
只要用心去做,每一件事情还是有可能成功的,当然成功是没有界限的,只不过是达到自己心里的那个目标,公众号:我的大前端生涯,一个爱喝茶的程序员,通常会搞搞SpringBoot 、Herbinate、Mybatiys、Android、iOS、Flutter、Vue、小程序等.
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页