黑马程序员--第二十四天:网络编程第二天

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

 

 

//24-1

import java.io.*;
import java.net.*;

class PicClient
{
	public static void main(String[] args) throws Exception
	{
		Socket s = new Socket("First-PC",10007);
		FileInputStream fis = new FileInputStream("cover.png");
		OutputStream out = s.getOutputStream();
		byte[] buf = new byte[1024];
		
		int len = 0;
		while ((len=fis.read(buf))!=-1)
		{
			out.write(buf,0,len);
		}

		s.shutdownOutput();
		//out.write(1);

		InputStream in = s.getInputStream();
		byte[] bufIn = new byte[1024];

		int num = in.read(bufIn);
		System.out.println(new String(bufIn,0,num));

		fis.close();
		s.close();
	}
}

class PicServer
{
	public static void main(String[] args) throws Exception
	{
		ServerSocket ss = new ServerSocket(10007);
		Socket s = ss.accept();

		InputStream in = s.getInputStream();

		FileOutputStream fos = new FileOutputStream("server.bmp");

		byte[] buf = new byte[1024];
		int len = 0;

		while ((len=in.read(buf))!=-1)
		{
			//String str = new String(buf,0,buf.length);
			//if(len == 1)
			//	break;
			fos.write(buf,0,len);
		}
		OutputStream out = s.getOutputStream();

		out.write("上传成功".getBytes());

		fos.close();
		s.close();
		ss.close();
	}
}


//程序始终有个莫名其妙的问题,就是不知道怎么解决
//该原因是由于联网造成的,可能是连上互联网后客户端和服务器不应直接的在同一机子上运行。



//24-2
import java.io.*;
import java.net.*;

class PicClient2
{
	public static void main(String[] args) throws Exception
	{
		if(args.length!=1){
			System.out.println("请选择一张jpg图片");
			return;
		}

		File file = new File(args[0]);
		if(!(file.exists()&&file.isFile())){
			System.out.println("该文件不存在");
			return;
		}

		if (!file.getName().endsWith(".jpg"))
		{
			System.out.println("请选择JPG图片");
			return;
		}
		
		if (file.length()>1024*1024)
		{
			System.out.println("请选择不大于1M的图片");
			return;
		}

		Socket s = new Socket("First-PC",10008);
		FileInputStream fis = new FileInputStream("cover.png");
		OutputStream out = s.getOutputStream();
		byte[] buf = new byte[1024];
		
		int len = 0;
		while ((len=fis.read(buf))!=-1)
		{
			out.write(buf,0,len);
		}

		s.shutdownOutput();
		//out.write(1);

		InputStream in = s.getInputStream();
		byte[] bufIn = new byte[1024];

		int num = in.read(bufIn);
		System.out.println(new String(bufIn,0,num));

		fis.close();
		s.close();
	}
}



class PicServer2
{
	public static void main(String[] args) throws Exception
	{
			ServerSocket ss = new ServerSocket(10008);
			Socket s;
		while(true){
			s = ss.accept();
			new Thread(new PicThread(s)).start();
		}
	}
}

class PicThread implements Runnable
{
	private Socket s;
	public PicThread(Socket s){
		this.s = s;
	}

	public void run (){
		InputStream in = null;

		FileOutputStream fos = null;
		
		int count = 0;
		try
		{
			String ip = s.getInetAddress().getHostAddress();
			//System.out.println(s);
			System.out.println(ip);

			in = s.getInputStream();

			File file = new File("server("+(count)+").bmp");

			while(file.exists())
			file = new File("server("+(count++)+").bmp");

			fos = new FileOutputStream(file);
			
			byte[] buf = new byte[1024];
			int len = 0;

			while ((len=in.read(buf))!=-1)
			{
				//String str = new String(buf,0,buf.length);
				//if(len == 1)
				//	break;
				fos.write(buf,0,len);
			}

			OutputStream out = s.getOutputStream();

			out.write("上传成功".getBytes());

		}
		catch (Exception e)
		{
			throw new RuntimeException("上传失败");
		}finally{
			try
			{
				if(fos != null)
				fos.close();
			}
			catch (Exception e)
			{
				throw new RuntimeException("fos 关闭失败");
			}
						try
			{
				if(s != null)
				s.close();
			}
			catch (Exception e)
			{
				throw new RuntimeException("s 关闭失败");
			}
		}

	//ss.close();

	}
}


//24-3

import java.net.*;
import java.io.*;

class LoginClient 
{
	public static void main(String[] args) throws Exception
	{
		Socket s = new Socket("First-PC", 10009);
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);

		for (int i=0;i<3 ;i++ )
		{
			String line = br.readLine();
			if(line == null)
				break;
			out.println(line);
			
			String info = in.readLine();
			System.out.println("info:"+info);
			if(info.contains("欢迎"))
				break;
		}

		br.close();
		s.close();
		//System.out.println("Hello World!");
	}
}

class UserThread implements Runnable
{
	private Socket s;
	UserThread (Socket s){
		this.s = s;
	}

	public void run(){
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip);

		BufferedReader br = null;
		try
		{
			for (int i =0;i<3 ;i++ )
			{
				BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
				String name = in.readLine();
				if(name==null)
					break;
				br = new BufferedReader(new FileReader("user.txt"));
				PrintWriter out = new PrintWriter(s.getOutputStream(),true);
				
				String line = null;

				boolean flag = false;

				while ((line = br.readLine())!=null)
				{
					if(line.equals(name)){
						flag = true;
						break;
					}
				}

				if(flag){
					System.out.println(name+" 已登入");
					out.println(name+ " 欢迎光临");
					break;
				}else{
					System.out.println(name+" 尝试登入");
					out.println(name+" 用户不存在");
				}
			}

			s.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(ip+" 校验失败");
		}finally{
			try
			{
				if(br!=null)
					br.close();
			}
			catch (Exception e)
			{
				throw new RuntimeException("br 关闭失败");
			}
		}
	}
}

class LoginServer
{
	public static void main(String[] args) throws Exception
	{
		ServerSocket ss = new ServerSocket(10009);
		while(true){
			Socket s = ss.accept();
			new Thread(new UserThread(s)).start();
		}
	}
}


//24-4

import java.net.*;
import java.io.*;

class ServerDemo 
{
	public static void main(String[] args) throws Exception
	{
		ServerSocket ss = new ServerSocket(10010);
		Socket s = ss.accept();
		System.out.print(s.getInetAddress().getHostAddress());
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		out.println(")--(");
		s.close();
		ss.close();
		System.out.println("Hello World!");
	}
}



//24-6_7

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

class  MyIE
{
	private Frame f;
	private TextField tf;
	private Button b;
	private TextArea ta;
	private Dialog d; 
	private Button okb;
	private Label l;

	MyIE(){
		init();
	}

	public void init(){
		f = new Frame("my window");
		f.setBounds(300,300,600,500);
		f.setLayout(new FlowLayout());

		d = new Dialog(f,"show",true);
		l = new Label();
		okb = new Button("OK");
		d.setBounds(350,350,200,100);
		d.setLayout(new FlowLayout());

		tf = new TextField(180);

		b = new Button("button");

		ta = new TextArea(100,180);

		f.add(tf);
		f.add(b);
		f.add(ta);

		myEvent();
		f.setVisible(true);
	}

	private void myEvent(){
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});

		b.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				showDialog();
			}
		});

		okb.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				d.setVisible(false);
			}
		});

		tf.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e){
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					showDialog();
			}
		});
	}

	public void showDialog(){
		try
		{
			ta.setText("");//
			String urlPath = tf.getText();//
				
			URL url = new URL(urlPath);//
			URLConnection conn = url.openConnection();
			
			System.out.println(conn);

			InputStream in = conn.getInputStream();

			byte[] buf = new byte[1024];
			int len = in.read(buf);

			ta.setText(new String(buf,0,len));//
		}
		catch (Exception e)
		{
		}
	}

	public static void main(String[] args) 
	{
		new MyIE();
		//System.out.println("Hello World!");
	}
}



//24-8

import java.net.*;
import java.io.*;

class URLConnectionDemo 
{
	public static void main(String[] args) throws Exception
	{
		URL url = new URL("http://localhost/");
		URLConnection conn = url.openConnection();//
		
		System.out.println(conn);

		InputStream in = conn.getInputStream();//

		byte[] buf = new byte[1024];
		int len = in.read(buf);

		System.out.println(new String(buf,0,len));
	}
}


//24-8
import java.net.*;

class URLDemo 
{
	public static void main(String[] args) throws MalformedURLException
	{
		URL url = new URL("http://localhost/");
		System.out.println("getProtocol():"+url.getProtocol());	
		System.out.println("getHost():"+url.getHost());
		System.out.println("getPort():"+url.getPort());
		System.out.println("getPath():"+url.getPath());
		System.out.println("getFile():"+url.getFile());
		System.out.println("getQuery():"+url.getQuery());

		int port = url.getPort();
		if(port == -1)
			port = 80;
	
	}
}

域名解析:
C:\Windows\System32\drivers\etc\hosts


---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ---------------------- 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值