TCP传输协议中的Server和Cilent之间的交互

import java.net.*;
import java.io.*;
public class Server {
	public static  void main(String args[])throws Exception{
		ServerSocket ss = new ServerSocket(6666);//端口被监听
		while(true){
			Socket s = ss.accept();//Server端口允许客户端的链接,返回一个客户端对象
			DataInputStream dis = new DataInputStream(s.getInputStream());
			System.out.println(dis.readUTF());
			dis.close(); 
			s.close(); 
			System.out.println("Accept!");
		}
	}
}

import java.net.*;
import java.io.*;
public class Client {
	public static void main(String[] args)throws Exception{
		Socket c = new Socket("127.0.0.1",6666);
		OutputStream os = c.getOutputStream();
		DataOutputStream dd  = new DataOutputStream(os);
		Thread.sleep(3000);
		dd.writeUTF("hello Server!");
		dd.flush();dd.close();
		c.close();
	}
}

你可能感兴趣的:(JAVA)