[Java] 网络-01 TCPClient / TCPServer

import java.net.*;
import java.io.*;

public class TCPServer {
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(6666);
        while(true) {
            Socket s = ss.accept(); // accept 阻塞式的
System.out.println("a client connect!");
            DataInputStream dis = new DataInputStream(s.getInputStream());
            System.out.println(dis.readUTF()); // readUTF 阻塞式的
            dis.close();
            s.close();
        }
        
    }
}
import java.net.*;
import java.io.*;

public class TCPClient {
    public static void main(String[] args) throws Exception {
        Socket s = new Socket("127.0.0.1", 6666);
        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        Thread.sleep(30000);
        dos.writeUTF("hello server!");
        dos.flush();
        dos.close();
        s.close();
    }
}


posted @ 2013-12-09 14:42  小尼人00  阅读(877)  评论(0编辑  收藏  举报