黑马程序员-网络编程 tcp传输 URL

------- android培训java培训、期待与您交流! ----------
ServerSocket构造函数中
ServerSocket(int port, int backlog)
backlog指连接的客户端的队列的最大长度,可以自定义指定。
Tcp传输协议通信:
/*
客户端:
1.创建Socket服务,并指定要连接的服务主机
2.获取socket流中的输出流
3.获取socket输入流,输出从服务端返回的数据
*/
import java.net.*;
import java.io.*;
class  TcpClient
{
	public static void main(String[] args) throws Exception
	{
		Socket s = new Socket("192.168.1.100",10003);


		OutputStream out = s.getOutputStream();


		out.write("tcp is coming".getBytes());


		InputStream in = s.getInputStream();


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


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


		s.close();


	}
}
/*
服务端:
1.建立服务端的socket服务。ServerSocket。并监听一个端口
2.获取连接过来的客户端对象。通过ServerSocket的accept方法。
3.客户端如果发过来数据,服务端会使用对应的客户端对象,并获取该客户端的流。
4.关闭服务端。
*/
class TcpServer
{
	public static void main(String[] args)throws Exception
	{
		ServerSocket ss = new ServerSocket(10003);


		Socket s = ss.accept();


		String ip = s.getInetAddress().getHostAddress();


		System.out.println(ip+"....is connecting");


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


			OutputStream out = s.getOutputStream();


			out.write("client  i receive".getBytes());
		}




		s.close();
	}
}

建立文本转换服务器:
客户端:
源:键盘录入
目的:网络设备,网络输出流


步骤:
1.建立服务
2.获取键盘录入
3.将数据发给服务端
4.获取服务端返回数据
5.关闭资源


服务端:
源:socket读取流
目的:socket输出流
1.建立服务
2.获取客户端socket
3.获取socket输入流中的数据
4.通过socket输出流返回数据
5.关闭客户端资源


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


		BufferedReader bufr = 
			new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter out = 
			new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		BufferedReader in = 
			new BufferedReader(new InputStreamReader(s.getInputStream()));


		String line = null;


		while((line=bufr.readLine())!=null)
		{
			out.write(line);
			out.newLine();//服务端有readLine方法,需要回车符标记
			out.flush();//缓冲区刷新后数据才会发送出去


			String str = in.readLine();
			
			System.out.println("sever:"+str);


			if("over".equals(line))
				break;
		}
		bufr.close();
		s.close();
	}
}


class TransServer
{
	public static void main(String[] args)throws Exception
	{
		ServerSocket ss = new ServerSocket(10005);


		Socket s = ss.accept();


		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"...is connecting");


		BufferedReader in = 
			new BufferedReader(new InputStreamReader(s.getInputStream()));
		//BufferedWriter out = 
			//new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);//可以使用PrintWriter输出流代替
		
		String line = null;
		while((line=in.readLine())!=null)
		{
			System.out.println(line);
			out.println(line.toUpperCase());
			//out.write(line.toUpperCase());
			//out.newLine();//客户端也是有readLine方法,需要回车符标记。
			//out.flush();
		}
		s.close();


	}
}

客户端并发上传图片:
import java.net.*;
import java.io.*;
class PicClient 
{
	public static void main(String[] args) throws Exception
	{
		Socket s = new Socket("192.168.1.100",10007);


		FileInputStream fis = new FileInputStream("1.jpg");


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


		InputStream in = s.getInputStream();


		byte[] bufIn = new byte[1024];


		int lenIn = in.read(bufIn);


		System.out.println(new String(bufIn,0,lenIn));


		fis.close();
		s.close();
	}
}
class PicThread implements Runnable//让客户端访问后的执行程序封装成对象并实现Runnable
{
	private Socket s;
	PicThread(Socket s)
	{
		this.s = s;
	}
	
	public void run()
	{
		int count = 1;
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"....is connecting");
		try
		{
			InputStream in = s.getInputStream();


			File file = new File(ip+".jpg");
			while(file.exists())//判断服务端所属文件是否重名
				file = new File(ip+"("+(count++)+")"+".jpg");


			FileOutputStream fos = new FileOutputStream(file);


			byte[] buf = new byte[1024];


			int len = 0;


			while((len=in.read(buf))!=-1)
			{
				fos.write(buf,0,len);
			}


			OutputStream out = s.getOutputStream();
		
			out.write("上传成功".getBytes());


			fos.close();


			s.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(ip+"图片上传失败");
		}
	}
}
class  PicServer
{
	public static void main(String[] args) throws Exception
	{
		ServerSocket ss = new ServerSocket(10007);
		
		while(true)//服务端不断接收不同客户端
		{
			Socket s = ss.accept();


			new Thread(new PicThread(s)).start();
		}
	}
}

客户端并发登录:
import java.net.*;
import java.io.*;
class  LoginClient
{
	public static void main(String[] args) throws Exception
	{
		Socket s = new Socket("192.168.1.100",10009);


		BufferedReader bufr = 
			new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		BufferedReader in = 
				new BufferedReader(new InputStreamReader(s.getInputStream()));
		for(int x=0;x<3;x++)
		{
			String line = bufr.readLine();
			if(line==null)
				break;
			out.println(line);
			
			String str = in.readLine();
			System.out.println(str);
			if(str.contains("欢迎"))
				break;
		}
		bufr.close();
		s.close();
	}
}
class LoginThread implements Runnable
{
	private Socket s;
	LoginThread(Socket s)
	{
		this.s = s;
	}
	public void run()
	{
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"...is connecting");
		try
		{
			for(int x=0;x<3;x++)
			{
				BufferedReader bufIn = 
					new BufferedReader(new InputStreamReader(s.getInputStream()));
				BufferedReader bufr =
					new BufferedReader(new FileReader("user.txt"));
				PrintWriter out = new PrintWriter(s.getOutputStream(),true);


				String name = bufIn.readLine();
				if(name==null)
					break;
				String line = null;


				boolean flag = false;
				while((line=bufr.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+",用户名不存在 ");
				}
				bufr.close();
			}
			s.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException("校验失败");
		}
		
	}
}
class  LoginServer
{
	public static void main(String[] args)  throws Exception
	{
		ServerSocket ss = new ServerSocket(10009);


		while(true)
		{
			Socket s = ss.accept();
			new Thread(new LoginThread(s)).start();
		}
	}
}
自定义浏览器和服务器:
import java.net.*;
import java.io.*;
/*
GET / HTTP/1.1
Host: localhost:11000
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,
User-Agent: Mozilla/5.0 (Windows NT 5.1)
Chrome/25.0.1349.2 Safari/537.21
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
*/
class  MyIE
{
	public static void main(String[] args) throws Exception
	{
		Socket s = new Socket("192.168.1.100",8080);


		PrintWriter out = new PrintWriter(s.getOutputStream(),true);


		out.println("GET /myweb/demo.html HTTP/1.1");
		out.println("Accept: */*");
		out.println("Accept-Language: zh-CN,zh;q=0.8");
		out.println("Host: 192.168.1.100:8080");
		out.println("Connection: closed");
		


		out.println();


		BufferedReader bufr = 
			new BufferedReader(new InputStreamReader(s.getInputStream()));
		String line = null;
		while((line=bufr.readLine())!=null)
		{
			System.out.println(line);
		}
		bufr.close();
		s.close();
	}
}
class IEServer
{
	public static void main(String[] args)throws Exception
	{
		ServerSocket ss = new ServerSocket(11000);


		Socket s = ss.accept();


		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"...is connecting");


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


		byte[] buf = new byte[1024];
		int len = in.read(buf);
		System.out.print(new String(buf,0,len));
		


		out.println("<font size=5 color=red>客户端 hello</font>");
		s.close();
	}
}

URL:统一资源定位符
URLConnection ucon = url.openConnection();//打开与远程主机的连接
将传输层上升到应用层


连接网络简易浏览器:
URL url = new URL(urlPath);


URLConnection ucon = url.openConnection();


			
InputStream in = ucon.getInputStream();


BufferedReader bufr = 
new BufferedReader(new InputStreamReader(in));


String line = null;
while ((line=bufr.readLine())!=null)
{
	System.out.println(line);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值