黑马程序员_java基础客户-服务端模式

-----
/*
客户端向服务器端上传文本
*/
import java.io.*;
import java.net.*;

class   TextClient
{
	public static void main(String[] args) throws Exception
	{
		Socket s = new Socket("192.168.1.254",10006);

		BufferedReader bufr = 
			new BufferedReader(new FileReader("IPDemo.java"));
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);

		String line = null;

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

		s.shutdownOutput();//关闭客户端的输出流。

		BufferedReader bufIn = new BufferedReader (new InputStreamReader(s.getInputStream()));//读一次完事
		
		String str = bufIn.readLine();
		System.out.println(str);

		bufr.close();
		s.close();
	}
}

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

		Socket s = ss.accept();

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

		BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

		PrintWriter out = new PrintWriter(new FileWriter("server.txt"),true);

		String line = null;

		while((line=bufr.readLine())!=null)
		{
			//if("over".equals(line))
			//break;
			out.println(line);
		}

		PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
		pw.pritnln("上传成功");

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


/*
客户端:
1 创建服务端点
2 读取客户端已有的图片数据。
3 通过socket输入流将数据发给服务端。
4 读取服务端反馈信息。
5 关闭。
*/

import java.io.*;
import java.net.*;
class  PicClient
{
	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("图片格式错误,请重新选择");
				return
		 
		 }

		 if (fiel.length()>5*1024*1024)
		 {
			 System.out.println("文件过大,没安好心");
				 return;
		 }
		
		
		Socket s = new Socket("192.168.1.254",10007);

		FileInputStream fis = new FileInputStream(file);

		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 num = in.read(bufIn);
		System.out.println(new String(bufIn.0,num));

		fis.close();
	}
}


/**/

class PicThread implements Runnable

{
	private Socket s;//给线程分配socket流
	PicThread(Socket s)
	{
		this.s = s;
	}
public void run()
	{
		try
		{
		
		int count = 0
		String ip = s.getIneAddress().getHostAddress();
		InputStream in = s.getInputStream();

		File file = new File(ip+"("+(count++)+")"+".jpg");
		
		 while(file.exists())
			 File file = new File(ip+"("+(count++)+")"+".jpg");

		PrintOutputStream fos = new FileOutputSTream("server.bmp"),true);

		byte[] buf = new byte[1024];

		int len =0;
		while((line=bufr.readLine())!=-1)
		{
			fos.write(buf,0,len
		}

		OutputStream out = s.getOutputStream;
		pw.pritnln("上传成功");

		out.close();
		s.close();
		ss.close();
		}
		catch () (Exception e)
		{
			throw new RuntimeException(ip+"上传失败");
		}
	}
}

class PicServer
{
	ServeSocket ss = new ServerSocket(10007);

	while(true)//主线程等待
	{
		Socket s = ss.accept();//创建了新客户端线程
		new Thread(new PicThread(s)),start;
	}
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值