Loading

Java网络编程-TCP

InetAddress和SocketAddress

//InetAddress
InetAddress add1=InetAddress.getByName("127.0.0.1");
			
InetAddress add2=InetAddress.getByName("localhost");

InetAddress add3=InetAddress.getLocalHost();

//InetSocketAddress
InetSocketAddress socadd1=new InetSocketAddress("127.0.0.1", 8080);

InetSocketAddress socadd2=new InetSocketAddress("localhost", 8080);
	

TCP协议信息传输

//客户端发送一条消息
public class TcpClient_01 {
	public static void main(String[] args) {
		Socket socket=null;
		OutputStream os=null;
		
		try {
			InetAddress addr=InetAddress.getByName("127.0.0.1");
		
			int port=9999;
			
			socket =new Socket(addr, port);
			os=socket.getOutputStream();
			
			os.write("i am client".getBytes());
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				os.close();
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
	}
}
//服务端接收消息
public class TcpServer_01 {
	
	public static void main(String[] args) {
		ServerSocket servsocket=null;
		Socket accept=null;
		InputStream is=null;
		ByteArrayOutputStream bos=null;
		try {
			servsocket=new ServerSocket(9999);
			
			while(true) {
				accept=servsocket.accept();
				
				is=accept.getInputStream();
				
				/*
				byte[] buffer=new byte[1024];
				int len;
				while((len=is.read(buffer))!=-1) {
					String msg=new String(buffer,0,len);
					System.out.println(msg);
				}
				*/
				
				bos=new ByteArrayOutputStream();
				byte[] buffer=new byte[1024];
				int len;
				while((len=is.read(buffer))!=-1) {
					bos.write(buffer,0,len);//使用管道流
				}
				System.out.println("server accept:"+bos.toString());
			}
			
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				bos.close();
				is.close();
				accept.close();
				servsocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}

TCP收发图片

//客户端发送图片
public class TcpClient_02 {
	public static void main(String[] args) throws UnknownHostException, IOException {
		
		Socket socket=new Socket(InetAddress.getByName("127.0.0.1"), 9000);
		
		OutputStream os=socket.getOutputStream();
		
		FileInputStream fis=new FileInputStream(new File("aaa.png"));
		
		byte [] buffer=new byte[512];
		int len;
		while((len=fis.read(buffer))!=-1) {
			os.write(buffer,0,len);
		}
		
		socket.shutdownOutput();//发送完毕,关闭输出
		
		InputStream is=socket.getInputStream();
		ByteArrayOutputStream bos=new ByteArrayOutputStream();
		byte[] buf=new byte[1024];
		int len2=0;
		while((len2=is.read(buf))!=-1) {
			bos.write(buf,0,len2);
		}
		System.out.println("client:"+bos.toString());
		
		
		bos.close();
		is.close();
		fis.close();
		os.close();
		socket.close();
		
	}
	
}
//服务端接收图片
public class TcpServer_02 {
	public static void main(String[] args) throws IOException {
		ServerSocket servsocket=new ServerSocket(9000);
		Socket accept=servsocket.accept();
		InputStream is=accept.getInputStream();
//		OutputStream os=accept.getOutputStream();
		/*
		Scanner scanner=new Scanner(System.in);
		scanner.next();
		*/
		FileOutputStream fos=new FileOutputStream(new File("receive1.png"));
		byte[] buffer=new byte[1024];
		int len;
		while((len=is.read(buffer))!=-1) {
			fos.write(buffer,0,len);
		}
		
		OutputStream os=accept.getOutputStream();
		os.write("服务器接收完毕".getBytes());
		
		os.close();
		fos.close();
		is.close();
		accept.close();
		servsocket.close();
		
	}
}
posted @ 2020-12-11 22:55  沿途有余弦  阅读(94)  评论(0)    收藏  举报