如何通过TCP通信协议由客户端向服务端发送信息?如何相互发送通信信息?

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import org.junit.Test;


/*TCP编程: 从客户端给服务器发送信息,服务器端把信息输出到控制台上
 * */
public class TestTcp1 {
     //客户端  
	@Test
	public void client(){
		//1.建立一个Socket的对象,通过其构造器指明服务器端的ip地址,以及接受程序的端口号
		Socket scoket=null;
		//2.通过  getOutputStream(): 发送数据 ,方法的返回值 OutputStream对象   就是输出
		OutputStream os=null;
		try {
			scoket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
			os = scoket.getOutputStream();
			
			//重点write(字节数组)
			os.write("我爱JAVA".getBytes());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(scoket!=null){
				try {
					scoket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
	}
	
	//服务器端
	@Test
	public void server(){
		//1.建立一个ServerSocket对象,通过构造器指明自身的端口号:
		ServerSocket ss=null;
		//2.调用其accept(),返回一个Socket对象
		Socket s=null;
		//3.调用Socket对象的getInputStream(): 获取从一个客户端发送过来的输入流
		InputStream is=null;
		try {
			ss = new ServerSocket(9090);
			s = ss.accept();
			is = s.getInputStream();
			//4.对流进行操作
			byte[] b=new byte[20];
			int len;
			while((len=is.read(b))!=-1){
				//将字节转换成字符串
				String str=new String(b, 0, len);
				System.out.print(str);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(s!=null){
				try {
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(ss!=null){
				try {
					ss.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
		
	}
}

相互发送信息

public class TestTCP2 {
	  //客户端  
		@Test
		public void client(){
			//1.建立一个Socket的对象,通过其构造器指明服务器端的ip地址,以及接受程序的端口号
			Socket scoket=null;
			//2.通过  getOutputStream(): 发送数据 ,方法的返回值 OutputStream对象   就是输出
			OutputStream os=null;
			InputStream is=null;
			try {
				scoket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
				os = scoket.getOutputStream();
				os.write("来自客户端:今天上班吗?".getBytes());
				
				//重点:shutdownOutput();执行此方法,显式的告诉服务器消息发送完毕
				scoket.shutdownOutput();
				
				
				is = scoket.getInputStream();
				byte[] b=new byte[1024];
				int len;
				while((len=is.read(b))!=-1){
					String str=new String(b,0,len);
					System.out.println(str);
				}
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(is!=null){
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				if(os!=null){
					try {
						os.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				if(scoket!=null){
					try {
						scoket.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
			
			
			
			
			
		}
		
		//服务器端
		@Test
		public void server(){
			//1.建立一个ServerSocket对象,通过构造器指明自身的端口号:
			ServerSocket ss=null;
			//2.调用其accept(),返回一个Socket对象
			Socket s=null;
			//3.调用Socket对象的getInputStream(): 获取从一个客户端发送过来的输入流
			InputStream is=null;
			OutputStream os=null;
			try {
				ss = new ServerSocket(9090);
				s = ss.accept();
				is = s.getInputStream();
				//4.对流进行操作
				byte[] b=new byte[20];
				int len;
				while((len=is.read(b))!=-1){
					String str=new String(b, 0, len);
					System.out.print(str);
				}
				
				
				os = s.getOutputStream();
				os.write("来自服务端:上班啊".getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(os!=null){
					try {
						os.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if(is!=null){
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				if(s!=null){
					try {
						s.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				if(ss!=null){
					try {
						ss.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
			
		}
}

猜你喜欢

转载自blog.csdn.net/Java_stud/article/details/82347239