JAVA:一个简单的TPC传输消息

1服务器

import java.io.*;
import java.net.*;

public class IP {
	public static void main(String[] args) {
		try {
			ServerSocket server=new ServerSocket(9006);
			Socket socket=server.accept();
			InputStream in=socket.getInputStream();
			OutputStream out=socket.getOutputStream();
			out.write("你好".getBytes());
			
			byte bt[]=new byte[1024];
			int len=in.read(bt);
			String pass=new String(bt,0,len);
			System.out.println(pass);
			
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}
}

2客户端

import java.io.*;
import java.net.*;

public class IPs {
	public static void main(String[] args) {
		try {
			Socket socket=new Socket("127.0.0.1",9006);
			InputStream in=socket.getInputStream();
			OutputStream out=socket.getOutputStream();
			out.write("你好,我是客户端".getBytes());
			
			byte bt[]=new byte[1240];
			int len=in.read(bt);
			String pass=new String(bt,0,len);
			System.out.println(pass);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42192693/article/details/80957340