码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA_Socket

时间:2015-05-15 22:46:35      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 1 import java.net.*;
 2 import java.io.*;
 3 
 4 public class TCPServer {
 5     public static void main(String[] args) throws Exception {
 6         ServerSocket ss = new ServerSocket(6666);
 7         while(true) {
 8             Socket s = ss.accept();
 9 System.out.println("a client connect!");
10             DataInputStream dis = new DataInputStream(s.getInputStream());
11             System.out.println(dis.readUTF());
12             Thread.sleep(3000);
13             System.out.println("hello client!");
14             dis.close();
15             s.close();
16         }
17         
18     }
19 }

 

 1 import java.net.*;
 2 import java.io.*;
 3 
 4 public class TCPClient {
 5     public static void main(String[] args) throws Exception {
 6         Socket s = new Socket("127.0.0.1", 6666);
 7         OutputStream os = s.getOutputStream();
 8         DataOutputStream dos = new DataOutputStream(os);
 9         Thread.sleep(3000);
10         dos.writeUTF("hello server!");
11         dos.flush();
12         dos.close();
13         s.close();
14     }
15 }

 

技术分享

 

JAVA_Socket

标签:

原文地址:http://www.cnblogs.com/roger-h/p/4506908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!