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();
		
	}

}