黑马程序员-TCP上传图片-多线程并发上传图片-客户端并发登陆-自定义服务器

---------------------- android培训、java培训、期待与您交流! ---------------------- 
TCP练习(上传图片)
/**
 * 上传图片
 * 客户端
 * 1.服务端点
 * 2.读取客户端已有的图片数据。
 * 3.通过socket输出流将数据发给服务端。
 * 4.读取服务端反馈信息。
 * 5.关闭。
 * @author onlin_000
 *
 */
public class TcpUploadingPiceClient {

	public static void main(String[] args) throws Exception {
		Socket s = new Socket(InetAddress.getByName("192.168.1.111"),10005);
		FileInputStream fis = new FileInputStream(new File("test.bmp"));
		OutputStream ous = s.getOutputStream();
		byte[] buf =new byte[1024];
		int len = 0;
		while((len = fis.read(buf)) != -1){
			ous.write(buf, 0, len);
		}
		s.shutdownOutput();
		InputStream ins = s.getInputStream();
		int i =ins.read(buf);
		System.out.println(new String(buf,0,i));
		ous.close();
		fis.close();
		s.close();
		ins.close();
	}
}
服务端:
public class TcpUploadingPiceServer {

	public static void main(String[] args) throws Exception {
		ServerSocket ss = new ServerSocket(10005);
		Socket s = ss.accept();
		FileOutputStream fos = new FileOutputStream(new File("Server.bmp"));
		InputStream ins = s.getInputStream();
		byte[] data = new byte[1024];
		int len = 0;
		while((len = ins.read(data)) != -1){
			fos.write(data,  0, len);
		}
		OutputStream outs = s.getOutputStream();
		outs.write("上传成功!".getBytes());
		fos.close();
		ins.close();
		outs.close();
		s.close();
		ss.close();
		
	}
}



TCP练习(多线程并发上传图片)
服务端:
</pre></div><div style="font-family: 微软雅黑; font-size: 14px; line-height: 21px; background-color: inherit;"><pre name="code" class="java">/**
 * 让多个客户端用户能够同时并发上传文件
 * 那么服务端最好就将每个客户端封装到一个单独的线程中,这样就可以同时处理对个客户端的请求。
 * 
 *如何定义线程呢?
 *只要明确了每一个客户端要在服务端执行的代码。
 * @author onlin_000
 *
 */
public class TcpUploadingByThreadServer {

	public static void main(String[] args) {
		while(true){
			ServerSocket ss = null;
			try {
				ss = new ServerSocket(10006);
				Socket s = ss.accept();
				new Thread(new clientByThread(s)).start();
			} catch (IOException e) {
				throw new RuntimeException("服务器启动失败!");
			}finally{
				try {
					ss.close();
				} catch (IOException e) {
					throw new RuntimeException("服务器关闭失败!");
				}
			}
		}
		
	}
}
class clientByThread implements Runnable{

	Socket s;
	clientByThread(Socket s){
		this.s = s;
	}
	@Override
	public void run() {
		String ip = s.getInetAddress().getHostAddress();
		int count = 0;
		try {
			File file = new File("server\\"+ ip + "(" + count + ")"+".bmp");
			while(file.exists()){
				file = new File("server\\"+ ip + "(" + (++count) + ")"+".bmp");
			}
			FileOutputStream fos = new FileOutputStream(file);
			InputStream ins = s.getInputStream();
			byte[] data = new byte[1024];
			int len = 0;
			while((len = ins.read(data)) != -1){
				fos.write(data,  0, len);
			}
			OutputStream outs = s.getOutputStream();
			outs.write("上传成功!".getBytes());
			fos.close();
			ins.close();
			outs.close();
			s.close();
		} catch (Exception e) {
			throw new RuntimeException(ip +"上传失败!");
		}
	}
客户端;
public class TcpUploadingByThreadClient {

	public static void main(String[] args) {
		try {
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入图片路径,请用\\\\代替\\");
			String path = sc.nextLine();
			sc.close();
			File file = new File(path);
			if(!file.exists()){
				System.out.println("请输入正确的路径");
				return;
			}
			if(!file.getName().endsWith(".bmp")||file.getName().endsWith(".BMP")){
				System.out.println("请选择正确的文件");
				return;
			}
			if(file.length()>1024*1024*5){
				System.out.println("文件太大!");
				return;
			}
			Socket s = new Socket(InetAddress.getByName("192.168.1.111"),10006);
			FileInputStream fis = new FileInputStream(file);
			OutputStream ous = s.getOutputStream();
			byte[] buf =new byte[1024];
			int len = 0;
			while((len = fis.read(buf)) != -1){
				ous.write(buf, 0, len);
			}
			s.shutdownOutput();
			InputStream ins = s.getInputStream();
			int i =ins.read(buf);
			System.out.println(new String(buf,0,i));
			ous.close();
			fis.close();
			s.close();
			ins.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}





TCP练习(客户端并发登录)
<span style="font-weight: normal;">/**
 * 客户端通过键盘录入用户名。
 * 服务端对这个用户名进行校验。
 * 
 * 如果该用户名存在,在服务器端显示xxx已登录。
 * 并在客户端显示xxx欢迎光临。
 * 
 * 如果该用户不存在,正在服务器端显示xxx尝试登录。
 * 并在客户端显示xxx用户不存在
 * @author onlin_000
 *
 */
public class TcpLoadServer {

	public static void main(String[] args) {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(10006);
			while(true){
				Socket s = ss.accept();
				new Thread(new LoadServer(s)).start();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
class LoadServer implements Runnable{
	Socket s;
	LoadServer( Socket s){
		this.s = s;
	}
	@Override
	public void run() {
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip + "..............connected");
		BufferedReader bufin = null;
		try {
			bufin = new BufferedReader(new FileReader(new File("User.txt")));
			BufferedReader buf = 
					new BufferedReader(new InputStreamReader(s.getInputStream()));
			PrintWriter pw = 
					new PrintWriter(s.getOutputStream(),true);
			for(int i = 0; i < 3; i++){
				boolean flag = false;
				String name = buf.readLine();
				if(name == null)
					return;
				String data = null;
				while((data = bufin.readLine()) != null){
					if(name.equals(data))
						flag = true;
				}
				if(flag){
					System.out.println(name + "登录成功");
					pw.println("欢迎" + name + "登录成功");
				}else{
					System.out.println(name + "尝试登录");
				}
			}
			buf.close();
			pw.close();
			bufin.close();
		} catch (Exception e) {
			throw new RuntimeException(ip + "登录失败");
		}
	}
	
}</span>
客户端:
<span style="font-weight: normal;">public class TcpLoadClient {

	public static void main(String[] args) {
		BufferedReader bufin = null;
		try {
			bufin = new BufferedReader( new InputStreamReader(System.in));
			Socket s = new Socket(InetAddress.getByName("192.168.1.111"),10006);
			BufferedReader buf = 
					new BufferedReader(new InputStreamReader(s.getInputStream()));
			PrintWriter pw =
					new PrintWriter(s.getOutputStream(),true);
			String name = null;
			while((name = bufin.readLine())!= null){
				
				if(name.equals("over"))
					return;
				pw.println(name);
				String re = buf.readLine();
				if(re.contains("欢迎"))
					System.out.println(re);
				pw.close();
				buf.close();
				s.close();
			}
		} catch (Exception e) {
			throw new RuntimeException("客户端异常");
		}
	}
}</span>

自定义浏览器-Tomcat服务器
<span style="font-weight: normal;">public class ServerDemo {

	public static void main(String[] args) {
		ServerSocket ss;
		try {
			ss = new ServerSocket(10010);
			Socket s = ss.accept();
			System.out.println(s.getInetAddress().getHostAddress() + "...............connected");
			PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
			pw.println("<font color = blue size = 5>Hello celient!</font>");
			ss.close();
			s.close();
		} catch (IOException e) {
			throw new RuntimeException("服务端创建失败");
		}
	}
}</span>

域名解析
1.客户机提出域名解析请求,并将该请求发送给本地的域名服务器。
2.当本地的域名服务器收到请求后,就先查询本地的缓存,如果有该纪录项,则本地的域名服务器就直接把查询的结果返回。
3.如果本地的缓存中没有该纪录,则本地域名服务器就直接把请求发给根域名服务器,然后根域名服务器再返回给本地域名服务器一个所查询域(根的子域)的主域名服务器的地址。
4.本地服务器再向上一步返回的域名服务器发送请求,然后接受请求的服务器查询自己的缓存,如果没有该纪录,则返回相关的下级的域名服务器的地址。
5.重复第四步,直到找到正确的纪录。
6.本地域名服务器把返回的结果保存到缓存,以备下一次使用,同时还将结果返回给客户机。

---------------------- android培训、java培训、期待与您交流! ---------------------- 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值