黒馬プログラマー---学習ノート(24日目)

9393 ワード

—————————— ASP.Net+Android+IOS開発、.Netトレーニング、お客様とのコミュニケーションを楽しみにしています!——————
/*
 
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class Pic 
{
	public static void main(String[] args)throws Exception 
	{
		   ServerSocket ss=new ServerSocket(1314);

		   while(true){
		   		Socket s=ss.accept();
				new Thread(new Pics(s)).start();
		   }
	}
}
// 
class Picc 
{
	   public static void main(String []args)throws Exception{
		  final Frame f=new Frame();
			f.setTitle(" ");
			f.setBounds(300,200,500,100); 
			f.setLayout(new FlowLayout());
			Label l=new Label();
			l.setText(" :");
			final TextField t=new TextField(40);
			Button b=new Button(" ");
			f.add(l);
			f.add(t);
			f.add(b);
			f.addWindowListener(new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					System.exit(0);
				}
			});
			b.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					try
					{
							 FileDialog fd=new FileDialog(f," ",FileDialog.LOAD);
					fd.setVisible(true);
					String path=fd.getDirectory();
					String name=fd.getFile();
					t.setText(path+name);

				   Socket s=new Socket("192.168.100.3",1314);

				   if(t.getText()!=null || !("".equals(t.getText()))){
							FileInputStream fis=new FileInputStream(new File(t.getText()));
							BufferedReader	br=new BufferedReader(new InputStreamReader(s.getInputStream()));
						   OutputStream os=s.getOutputStream();

						   byte [] buf=new byte[1024];
						   
						   int len=0;
						   while((len=fis.read(buf))!=-1){
								  os.write(buf,0,len);
						   }

						   s.shutdownOutput();

						   System.out.println(br.readLine());

						   			s.close();
				   }else{
						throw new RuntimeException(" ");	
					}
					}
					catch (Exception ee)
					{
						System.out.println(" ");
					}
				
				}
			});

			 f.setVisible(true);
			
	   	   

		  


	   }	
}
// 
class Pics	implements Runnable
{
	private Socket s;
	public Pics(Socket s){
		this.s=s;
	}
	public void run(){
			try
			{
				int count=0;
				 InputStream in=s.getInputStream();
				 OutputStream out=s.getOutputStream();
				 String ip=s.getInetAddress().getHostAddress();
				 File file=new File(ip+"("+(++count)+").gif");;
					if(file.exists())
						file=new File(ip+"("+(++count)+").gif");
				 FileOutputStream fw=new FileOutputStream(file);
				 byte []buf=new byte[1024];
				 int len=0;
				 while((len=in.read(buf))!=-1){
				 	  fw.write(buf,0,len);
				 }


				 out.write(" ".getBytes());

				 fw.close();
				 s.close();
			}
			catch (Exception e)
			{
				System.out.println(" ");
			}
	}
}
/*
 

*/
import java.io.*;
import java.net.*;
class Login 
{
	public static void main(String[] args)throws Exception 
	{
		ServerSocket ss=new ServerSocket(1314);
		while(true){
				 Socket s=ss.accept();
				 new Thread(new LoginServer(s)).start();
		}
	}
}
class LoginClient
{
	public static void main(String[] args)throws Exception 
	{
		  Socket s =new Socket("192.168.100.3",1314);

		  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		  PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
		  BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));
		  String line=null;
		  for (int i=0;i<3 ;i++ )
		  {
			   line=br.readLine();
			   pw.println(line);

			   String info=brin.readLine();
			   if(info.contains(" ")){
				   System.out.println(" ");
				   break;
			   }else{
			   		System.out.println(" .. ");
			   }
		  }

		  br.close();
		  s.close();
	}
}
class LoginServer  implements Runnable
{
	private Socket s;
	public LoginServer(Socket s){
		this.s=s;
	}
	public void run(){
		try
		{
			 BufferedReader br=null;
			 BufferedReader brin=new BufferedReader(new InputStreamReader(s.getInputStream()));
			 PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
			 String line=null;	

			 for (int i=0;i<3;i++)
			 {
				br= new BufferedReader(new FileReader("user.txt"));
				 String user=brin.readLine();
				 if(user==null)
					 break;

				 boolean is=false;

				 while((line=br.readLine())!=null){
				 	 if(user.equals(line)){
						 is=true;
							break;
					 }
				 }
				   if(is){
						pw.println(user+" !!");
						System.out.println(user+" ");
						break;
					 }else{
						 pw.println(user+" ");
						 System.out.println(" ");
					 }
			 }		 
			 br.close();
			 s.close();
		}
		catch (Exception e)
		{
			System.out.println(" ......");
		}
	}
}
/*
 



*/
import java.io.*;
import java.net.*;
class Demo1 
{
	public static void main(String[] args)throws Exception 
	{
		
		ServerSocket ss = new ServerSocket(12121);

		Socket s = ss.accept();
		// 
		InputStream in=s.getInputStream();
		byte[] buf=new byte[1024];
		int len=in.read(buf);
		System.out.println(new String(buf,0,len));
		 // s
		PrintWriter out=new PrintWriter(s.getOutputStream(),true);

		out.println(" ");


		s.close();
		ss.close();

	}
}
/*

  


 :tomcat 
*/


import java.net.*;
import java.io.*;
class Demo2 
{
	public static void main(String[] args)throws Exception 
	{
		Socket s=new Socket("127.0.0.1",8888);

		PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
		// 
		pw.println("GET /news/ HTTP/1.1");
		pw.println("Connection: Closed");
		pw.println("Cache-Control: max-age=0");
		pw.println("Host: localhost:"+s.getLocalPort());
		pw.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		pw.println("User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko)");
		pw.println("Chrome/33.0.1750.149 Safari/537.36");
		pw.println("Accept-Encoding: gzip,deflate,sdch");
		pw.println("Accept-Language: zh-CN,zh;q=0.8,en;q=0.6");
		pw.println();
		pw.println();


	  // System.out.println(s.getLocalPort());
	  // 
		InputStream in=s.getInputStream();
		byte[] buf=new byte[1024];

		int len=0;
		while((len=in.read(buf))!=-1){
		   System.out.println(new String(buf,0,len));
		}

		

		s.close();

	}
}

URLクラス
一般的な方法
url.getProtocol()/取得プロトコルgetHost();//ホストアドレスgetFile();//ファイル名getPath()/パスgetQuery()/クエリー内容getPort()/ポートtoString()/クラスを返す文字列表現openConnection()/接続を開き、Connection接続オブジェクトURLConnectionクラスに戻る
/*
 ie  

 

*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class  IE extends Frame
{
	private TextField t;
	private Button b;
	private TextArea ta;
	private Label l;
	private Button b1;
	private FileDialog save;
	public IE(){
		super(" IE");
		init();
	}
	public void init(){
		setBounds(300,200,600,500);
		setLayout(new FlowLayout());
		
		l=new Label(" URL:");
		t=new TextField(50);
		b=new Button(" ");
		b1=new Button(" ");
		ta=new TextArea(26,75);

		add(l);
		add(t);
		add(b);
		add(b1);
		add(ta);

		keyEvent();
		buttonEvent();
		windowEvent();

		setVisible(true);
	}
	private void keyEvent(){
		
	}
	private void buttonEvent(){
	   b.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
					 telnet();
			}
	   });
	   b1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				   saveFile();
			}
	   });
	}
	private void windowEvent(){
		 addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		 
		 });
	}
	private void saveFile(){
		try
		{
			save=new FileDialog(this," ",FileDialog.SAVE);
			save.setVisible(true);
			String path=save.getDirectory();
			String file=save.getFile();
			if(path!=null || file!=null){
				File f=new File(path,file);

				String taT=ta.getText();

				BufferedWriter br=new BufferedWriter(new FileWriter(f));
				br.write(taT);

				br.close();
			}
		}
		catch (Exception e)
		{
		}
	}
	private void telnet(){
		try
		{
			ta.setText("");
			/*
			//URL 
			sop(url.getProtocol());
			sop(url.getHost());
			sop(url.getFile());
			sop(url.getPath());
			sop(url.getQuery());
			sop(url.getPort());
			sop(url.toString());
			*/
			URL url=new URL(t.getText());
			URLConnection uc=url.openConnection();
		   /*
		   // 
			LineNumberReader lr=new LineNumberReader(new InputStreamReader(uc.getInputStream()));
			String line=null;

			while((line=lr.readLine())!=null){
				ta.append(lr.getLineNumber()+"  "+line+"\r
"); } lr.close(); */ InputStream in=uc.getInputStream(); byte[] buf=new byte[1024]; int len=0; while((len=in.read(buf))!=-1){ ta.append(new String(buf,0,len,"utf-8")); } in.close(); } catch (Exception e) { } } private void sop(Object o){ System.out.println(o); } } class IETest { public static void main(String[] args) { new IE(); } } /* http://192.168.1.254:80/myweb/demo.html?name=haha&age=30 http://192.168.100.3:8888/news */

ドメイン名の解析:
実は127とlocalHostのマッピング関係は本機上のc:window/systems/driver/etc/hostです
ホストファイルの設定
 —————————— ASP.Net+Android+IOS開発、.Netトレーニング、お客様とのコミュニケーションを楽しみにしています!——————