网络编程的实例

/*
2、创建客户/服务器应用,客户端发送"Time Require"到服务器,
服务器返回当前服务器的日期和时间,服务器能同时处理多个客户请求。  
*/
import java.net.*;
import java.io.*;
import java.util.*;
public class TCP
{
		public static void main(String[] args) throws Exception
		{
			new Thread(new TServer()).start();
		}
}
class TClient 
{
		public static void main(String[] args) throws Exception
		{
			//发送数据过程
			Socket s = null;
			for (int i = 0; i < 5; i++)
			{
				s = new Socket("172.18.177.222", 52088);
				String c_data = "Time Require........." + (i+1);
				OutputStream out = s.getOutputStream();
				out.write(c_data.getBytes());//把数据写入通道
				//接收服务器返回的数据
				InputStream in = s.getInputStream();
				byte[] buf = new byte[1024];
				int len = in.read(buf);//读取通道里面返回的数据
				String s_data = new String(buf, 0, len);
				System.out.print(s_data);
			}
			//关闭资源
			s.close();
		} 
}
class TServer implements Runnable
{
	private ServerSocket ss;
	public TServer() throws Exception
	{
		this.ss = new ServerSocket(52088);
	}
	public void run()
	{
		Socket s = null;
		try
		{
			while(true)
			{
				//接收客户端发来的数据
				s = ss.accept();
				System.out.println(s.getInetAddress().getHostAddress() + "..........Connect");
				InputStream in = s.getInputStream();
				byte[] buf = new byte[1024];
				int len = in.read(buf);
				String c_data = new String(buf, 0, len);//客户端发过来的数据转换成字符串
				System.out.println(c_data);
				//返回数据给客户端
				OutputStream out = s.getOutputStream();
				Calendar data = Calendar.getInstance();//获取时间
				Date d = data.getTime();
				String s_data = "服务端 : " + d;
				out.write(s_data.getBytes());//返回数据
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		//关闭资源
		//s.close();
		//ss.close();
	}
}



/*
1、	采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。
客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。(20分)
(服务端程序和客户端程序分别命名为Server.java和Client.java)
*/
import java.net.*;
import java.io.*;
class Client
{
    public static void main(String[] args) throws Exception
    {
		//发送数据 给定文件名
         DatagramSocket ds = new DatagramSocket(); 
		 byte[] buf = "TCP.java".getBytes();
		 DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("172.18.177.222"), 52099);
		 ds.send(dp);
		//接收数据
		String data = null;
		 ds = new DatagramSocket(52010);
		 BufferedWriter bufw = new BufferedWriter(new FileWriter("Copy.txt"));
		while(true)
		{
			  int i = 0;
			 byte[] buf1 = new byte[1024];
			 dp = new DatagramPacket(buf1, buf1.length);
			 ds.receive(dp);
			 data = new String(dp.getData(), 0, dp.getLength());
			 bufw.write(data);
			 bufw.newLine();
			 bufw.flush();
			 System.out.println(data + "--------->" + dp.getLength());
			 i++;
			 if (i == 100)
				break;
		}
		 ds.close();
    } 
}
class Server
{
	public static void main(String[] args) throws Exception
	{
		//接收数据
		DatagramSocket ds = new DatagramSocket(52099);
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf, buf.length);
		ds.receive(dp);
		//返回数据
		String data = new String(dp.getData(), 0, dp.getLength());
		System.out.println(data);
		BufferedReader burd = new BufferedReader(new FileReader(data));
		String s_data = null;
		ds = new DatagramSocket();
		while ((s_data = burd.readLine()) != null)
		{
			byte[] buf1 =s_data.getBytes();
			dp = new DatagramPacket(buf1, buf1.length, InetAddress.getByName("172.18.177.222"), 52010);
			ds.send(dp);
		}
		ds.close();
	}
}


/*
2、	编写客户/服务器程序,客户端Client.java使用DatagramSocket对象将数据包发送到服务器,
请求获取服务器端的图像(考生可自选图像文件)。服务器端Server.java将图像文件包装成数据包,
并使用DatagramSocket对象将该数据包发送到客户端。首先将服务器端的程序编译通过,并运行起来,等待客户的请求。(本题30分)
程序的运行效果如下图所示:
*/
import javax.imageio.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.image.*;
public class UdpPicture extends JFrame implements ActionListener
{
	JPanel p1, p2;
	JLabel p = new JLabel("");
	JButton b1;
	public UdpPicture()
	{
		super("图片获取【天华】");
		p1 = new JPanel();
		p2 = new JPanel();
		p2.setLayout(new BorderLayout());
		p1.setLayout(new BorderLayout());
		b1 = new JButton("获取图片");
		p1.add(b1);
		p2.add(p);
		add(p1, BorderLayout.NORTH);
		add(p2, BorderLayout.CENTER);
		setSize(432,355);
		setVisible(true);
		setDefaultCloseOperation(2);
		setLocationRelativeTo(null);
		b1.addActionListener(this);    
	}
    public static void main(String[] args) 
    {
		new UdpPicture();    
    } 
	public void actionPerformed(ActionEvent e)
	{	
		String str = null;
		if ((JButton)e.getSource() == b1)
		{
			ImageIcon picture = null;
			if(Static.num == 0)
			{
				try
				{
					PClient c = new PClient();
					c.c_start();				
					Static.str = c.getName();
					Static.num = 1;
					//显示图片
					picture = new ImageIcon(Static.str);
					File file = new File(Static.str);//获取图片路径
					FileInputStream fis = new FileInputStream(file);
					BufferedImage bufferedImg = ImageIO.read(fis);
					int imgWidth = bufferedImg.getWidth();
					int imgHeight = bufferedImg.getHeight();
					setSize(imgWidth,imgHeight);
					p.setIcon(picture);
				}
				catch (Exception e1)
				{
					System.out.println("出错");
				}
			}
			else if(Static.num == 1)
			{
					picture = new ImageIcon(Static.str);

					p.setIcon(picture);
			}
		}
	}
}
class PClient
{
	private String name;
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
	public static void sopn(Object obj)
	{
		System.out.print(obj);
	}
	public String getName()
	{
		return this.name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public void c_start() throws Exception
	{
	   //发送请求
		DatagramSocket ds = new DatagramSocket();
		byte[] c_buf = "1.jpg".getBytes();
		DatagramPacket dp = new DatagramPacket(c_buf, c_buf.length, InetAddress.getByName("127.0.0.1"), 55665);
		ds.send(dp);
		//接收请求
		ds = new DatagramSocket(55666);
		byte[] s_buf = new byte[1024];
		String str = "4.jpg";
		this.setName(str);
		FileOutputStream fos = new FileOutputStream(str);
		while(true)
		{
			dp = new DatagramPacket(s_buf, s_buf.length);
			ds.receive(dp);
			fos.write(dp.getData(), 0, dp.getLength());
			if(dp.getLength() < 1024) break;
		}
		ds.close();
	}
}
class PServer //implements Runnable
{
	public static void main(String[] args) throws Exception
	{
		    //接收客户端的请求
			DatagramSocket  ds = new DatagramSocket(55665);
			byte[] buf = new byte[1024];
			DatagramPacket dp = new DatagramPacket(buf, buf.length);
			ds.receive(dp);
			String str = new String(dp.getData(), 0, dp.getLength());
			sop(str);
			//发送数据包
			ds = new DatagramSocket();
			buf = new byte[1024];
			FileInputStream fis = new FileInputStream(str);//读取文件名
			int len;
			while((len = fis.read(buf)) != -1)
			{
				dp = new DatagramPacket(buf, len, InetAddress.getByName("127.0.0.1"), 55666);
				ds.send(dp);
			}
			buf = "886".getBytes();
			dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 55666);
			ds.send(dp);
			ds.disconnect();
			ds.close();
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
	public static void sopn(Object obj)
	{
		System.out.print(obj);
	}
}
class Static
{
	public static int num = 0; 
	public static String str = null;
}


第一个弄了我很久啊 唉, 醉了。 记录一下。。。

你可能感兴趣的:(网络编程的实例)