TCP即时小通信

package 第十二章;
import java.io.*;
import java.net.*;
public class TcpServer {
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		ServerSocket server=new ServerSocket(55555);
		Socket s=server.accept();
		InputStream is =s.getInputStream();
		byte []b=new byte[1024];
		int length =is.read(b);
		
		String msg=new String (b,0,length);
		System.out.println(msg);
		
		is.close();
		s.close();
		server.close();
	}

}


//客户端
package 第十二章;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class TcpClient {
	public static void main(String[] args) throws UnknownHostException, IOException{
		// TODO Auto-generated method stub
		while(true){
			Scanner scan=new Scanner(System.in);
			System.out.println("请输入你要输入的内容:");
			String str=scan.next();
			
			Socket s=new Socket("192.168.1.179",55555);
			OutputStream os=s.getOutputStream();
			
			os.write(str.getBytes());
			os.close();
			s.close();
		}
	}

}
原文地址:https://www.cnblogs.com/xiaoqisfzh/p/4708961.html