/* *先存在服务器 */ =================服务器============== package com.test.tcp; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class TCP_Server { public static void main(String[] args) throws IOException { ServerSocket ss = new ServerSocket(10086); Socket s =ss.accept(); InputStream is = s.getInputStream(); byte[] b = new byte[1024]; int len = is.read(b); String str =new String(b, 0, len); System.out.println(str); s.close(); } } =================客户端============== package com.test.tcp; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; public class TCP_Clinet { public static void main(String[] args) throws IOException { Socket s= new Socket("localhost", 10086); OutputStream os = s.getOutputStream(); os.write("TCP,你好!".getBytes()); os.close(); } } ======结果==== TCP,你好!
|
|