java网络编程的理解
tcp
server端
import java.io.DataInputStream; import java.net.ServerSocket; import java.net.Socket; public class testtcpserver { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub ServerSocket ss = new ServerSocket(6666);// while(true) { Socket s = ss.accept();//接受 System.out.println("a client connect"); DataInputStream dis = new DataInputStream(s.getInputStream()); System.out.println(dis.readUTF()); dis.close(); s.close(); } } }
client端
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.OutputStream; import java.net.Socket; public class tcpclient { public static void main(String[] args) throws Exception{ Socket s = new Socket("192.168.1.90",6666);//申请 OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); Thread.sleep(5000); dos.writeUTF("helle server"); dos.flush(); dos.close(); s.close(); } }