Network programming - TCP transmission

1. The client sends text to the server, and the server will convert the text to uppercase and return it to the client

public class Testtwo {
     @Test 
     public void bTest(){//客户端
    	 Socket socket=null;
    	 OutputStream os=null;
    	 FileInputStream fis=null;
    	 //发送服务端数据
    	 try {
			socket=new Socket(InetAddress.getByName("127.0.0.1"), 8989);//创建连接地址为127.0.0.1,端口为8989
		    os=socket.getOutputStream();//获取到输出对象
		     fis=new FileInputStream(new File("2.txt"));//取到文件
		     int len;
		     byte [] b=new byte[1024];
		     while((len=fis.read(b))!=-1){//对文件进行读取
		    	 os.write(b, 0, len);//将数据发送到服务器
		    	 String ssString=new String(b, 0, len);
		    	 System.out.print(ssString);
		     }
		     socket.shutdownOutput();//向系统说明数据发送完毕
		     //接收服务端数据
		     InputStream is=socket.getInputStream();//获取到服务器发的数据对象
		     int len1;
		     byte [] b1=new byte[1024];
		     while((len1=is.read(b1))!=-1){
		    	 String ssString=new String(b1, 0, len1);//类型转化
		    	 System.out.print(ssString);
		     }
    	 } catch (Exception e) {
			// TODO: handle exception
		}
    	 finally {
    		 try {
 				os.close();
 				fis.close();
 	    		 socket.close();
 			} catch (IOException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
     		 
		}
    	 
     }
     @Test 
     public void sTest(){
    	 ServerSocket ss=null;
    	 Socket socket=null;
    	 InputStream is=null;
    	 OutputStream os=null;
    	 try {
			 ss=new ServerSocket(8989);//绑定服务器端口
			 socket=ss.accept();//阻塞,直到接收到客户端的Socket连接  
			  is=socket.getInputStream();//获取到输入流对象
			  os=socket.getOutputStream();//获取到输出流对象
			 int len;
			 byte [] bs=new byte[1024];
			 while((len=is.read(bs))!=-1){//对输入流进行数据读取和处理
				 String ssString=new String(bs,0,len);
				 System.out.println(ssString);
				 char []cstr=ssString.toCharArray();
				 for(int i=0;i<cstr.length;i++){
					 if(cstr[i]>='a'&&cstr[i]<='z'){
						 cstr[i]=(char) ((int)cstr[i]-32);
					 }
					 
				 }
				 System.out.println(String.valueOf(cstr));
				 os.write(String.valueOf(cstr).getBytes());//发送到客户端
			 }
			 //socket.shutdownInput();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 finally{
    		 try {
				os.close();
				is.close();
	    		 socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		 
    	 }
    	 
     }
}

2. The server reads the picture and sends it to the client, and the client saves the picture locally

public class Testone {
     @Test 
     public void cTest(){
    	 Socket socket=null;
    	 InputStream is=null;
    	 FileOutputStream fos=null;
    	 try {
			 socket=new Socket(InetAddress.getByName("127.0.0.1"), 8989);
		      is=socket.getInputStream();
		      fos=new FileOutputStream(new File("1.jpg"));
		     int len;
		     byte [] b=new byte[1024];
		     while((len=is.read(b))!=-1){
		    	fos.write(b, 0, len);
		     }
		     System.out.println("成功保存文件");
		     
    	 } catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 finally {
 			try {
 				fos.close();
 				is.close();
 				socket.close();
 				
 			} catch (IOException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
 			
 			
 		}
     }
     @Test 
     public void serverTest() {
    	 ServerSocket ss=null;
    	 Socket socket=null;
    	 OutputStream os=null;
    	 FileInputStream fis=null;
		try {
			ss = new ServerSocket(8989);
			 socket=ss.accept();
	    	  os=socket.getOutputStream();
	    	  fis=new FileInputStream(new File("2.jpg"));
		     
	    	 int len;
		     byte [] b=new byte[1024];
		     while((len=fis.read(b))!=-1){
		    	 os.write(b,0,len);
		     }
		     socket.shutdownInput();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				fis.close();
				os.close();
				socket.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
    	 
    	 
     }
}

Guess you like

Origin blog.csdn.net/zs520ct/article/details/79384962