TCP



TCP只分客户端和服务端,而且必须先有服务端

示例1:

class TcpClient {

	public static void main(String[] args) throws Exception, IOException {
		
		//1.创建客户端的socket服务,指定主机和端口
		Socket s = new Socket("192.168.0.100",10004);
		
		//2.获取socket中的输出流
		OutputStream os = s.getOutputStream();
		
		//3.往输出流中传数据
		os.write("服务端你好!".getBytes());
		
		//4.接收服务端的反馈信息
		InputStream is = s.getInputStream();
		byte[] buf = new byte[1024];
		int len = is.read(buf);
		System.out.println(new String(buf,0,len));
		//5.关闭socket服务
		s.close();
	}


}

class TcpServer {
	public static void main(String[] args) throws Exception {
		
		//1.建立ServerSocket服务,并监听一个端口
		ServerSocket ss = new ServerSocket(10004);
		
		//2.通过accept方法获取连接过来的客户端对象
		Socket s = ss.accept();
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"......已连接");
		
		//3.通过使用客户端的输入流获取客户端发送过来的数据
		InputStream is = s.getInputStream();
			//创建一个字节数组用于存放客户端传递过来的数据
		byte[] buf = new byte[1024];
			//使用客户端对象的输入流,将流数据存放到字符数组中
		int len = is.read(buf);
		System.out.println(new String(buf,0,len));
		
		//4.反馈信息给客户端
		OutputStream os = s.getOutputStream();
		Thread.sleep(5000);
		os.write("服务端已收到你的信息,谢谢".getBytes());
				
		//5.关闭客户端
		s.close();
		//ss.close();		
	}
}



示例2:

class TcpClient {

	public static void main(String[] args) throws Exception, IOException {
		
		//1.创建客户端的socket服务,指定主机和端口
		Socket s = new Socket("192.168.0.100",10004);
		
		//2.获取socket中的输出流
		OutputStream os = s.getOutputStream();
		
		//3.往输出流中传数据
		os.write("服务端你好!".getBytes());
		
		//4.接收服务端的反馈信息
		InputStream is = s.getInputStream();
		byte[] buf = new byte[1024];
		int len = is.read(buf);
		System.out.println(new String(buf,0,len));
		//5.关闭socket服务
		s.close();
	}


}

class TcpServer {
	public static void main(String[] args) throws Exception {
		
		//1.建立ServerSocket服务,并监听一个端口
		ServerSocket ss = new ServerSocket(10004);
		
		//2.通过accept方法获取连接过来的客户端对象
		Socket s = ss.accept();
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"......已连接");
		
		//3.通过使用客户端的输入流获取客户端发送过来的数据
		InputStream is = s.getInputStream();
			//创建一个字节数组用于存放客户端传递过来的数据
		byte[] buf = new byte[1024];
			//使用客户端对象的输入流,将流数据存放到字符数组中
		int len = is.read(buf);
		System.out.println(new String(buf,0,len));
		
		//4.反馈信息给客户端
		OutputStream os = s.getOutputStream();
		Thread.sleep(5000);
		os.write("服务端已收到你的信息,谢谢".getBytes());
				
		//5.关闭客户端
		s.close();
		//ss.close();		
	}
}


示例3:

class TcpClient {

	public static void main(String[] args) throws Exception, IOException {
		
		//1.创建客户端的socket服务,指定主机和端口
		Socket s = new Socket("192.168.0.100",10004);
		
		//2.获取socket中的输出流
		OutputStream os = s.getOutputStream();
		
		//3.往输出流中传数据
		os.write("服务端你好!".getBytes());
		
		//4.接收服务端的反馈信息
		InputStream is = s.getInputStream();
		byte[] buf = new byte[1024];
		int len = is.read(buf);
		System.out.println(new String(buf,0,len));
		//5.关闭socket服务
		s.close();
	}


}

class TcpServer {
	public static void main(String[] args) throws Exception {
		
		//1.建立ServerSocket服务,并监听一个端口
		ServerSocket ss = new ServerSocket(10004);
		
		//2.通过accept方法获取连接过来的客户端对象
		Socket s = ss.accept();
		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+"......已连接");
		
		//3.通过使用客户端的输入流获取客户端发送过来的数据
		InputStream is = s.getInputStream();
			//创建一个字节数组用于存放客户端传递过来的数据
		byte[] buf = new byte[1024];
			//使用客户端对象的输入流,将流数据存放到字符数组中
		int len = is.read(buf);
		System.out.println(new String(buf,0,len));
		
		//4.反馈信息给客户端
		OutputStream os = s.getOutputStream();
		Thread.sleep(5000);
		os.write("服务端已收到你的信息,谢谢".getBytes());
				
		//5.关闭客户端
		s.close();
		//ss.close();		
	}
}


示例4:

class ClientThread {

	public static void main(String[] args) throws Exception, IOException {
                
		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(file.length()>1024*1024*5)
		{
			System.out.println("文件过大");
			return;
		}
		
		Socket s = new Socket("192.168.0.100",10007);
		FileInputStream fis = new FileInputStream("1.jpg");
		OutputStream os = s.getOutputStream();
		byte[] buf = new byte[1024];
		int line = 0;
		while((line = fis.read(buf))!=-1)
		{
			os.write(buf,0,line);
			os.flush();
		}
		s.shutdownOutput();
		
		InputStream is = s.getInputStream();
		byte[] b = new byte[1024];
		int num = is.read(b);
		System.out.println(new String(b,0,num));
		fis.close();
		s.close();
		
		
	}

}

class ServerThread {

	public static void main(String[] args) throws Exception {
		ServerSocket ss = new ServerSocket(10007);
		while(true)
		{
			Socket s = ss.accept();
			new Thread(new UploadThread(s)).start();

		}
		
	
	}

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

	@Override
	public void run() {
		try {
			int count = 1;
			String ip = s.getInetAddress().getHostAddress();
			System.out.println(ip+"--已连接");
			
			File file = new File(ip+"("+(count++)+").jpg");
			while(file.exists())
			{
				file = new File(ip+"("+(count++)+").jpg");
			}
			
			FileOutputStream fos = new FileOutputStream(file);
			InputStream is = s.getInputStream();
			byte[] buf = new byte[1024];
			int line = 0;
			while((line = is.read(buf))!=-1)
			{
				fos.write(buf,0,line);
				fos.flush();
			}
			OutputStream os = s.getOutputStream();
			os.write((ip+"上传成功").getBytes());
			
			fos.close();
			s.close();
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值