1 import java.io.BufferedReader; 2 import java.io.InputStreamReader; 3 import java.io.PrintWriter; 4 import java.net.InetAddress; 5 import java.net.Socket; 6 public class SimpleClient { 7 public static void main(String[] args) throws Exception { 8 Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),8888);//连接某台机器(127.0.0.1)上的某个端口(8888) 9 PrintWriter pw=new PrintWriter(socket.getOutputStream());//得到Socket连接的输出流 10 pw.println("你好服务器!!");//向服务器发送数据(有可能在缓存里面不会立刻发送,所以要下面的语句) 11 pw.flush();//立刻发送数据 12 BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//取得Socket连接的输入流 13 System.out.println(in.readLine());//打印取得的数据 14 Thread.sleep(1000); 15 } 16 }