TCP(Socket基础编程)

1、TCP特点:

  面向连接、可靠安全、传输量大、速度较慢

2、socket编程主要依靠两个类:socket 、serversocket

  example1:客户端可以不停输入字符串,服务端返回字符串的大写

客户端代码:

 1 public static void main(String[] args) throws Exception{
 2             //建立socket连接 连接一旦建立成功 就可以获得 输入流 和 写出流
 3             Socket s = new Socket(InetAddress.getLocalHost(),10001);
 4             //客户端从键盘输入字符
 5             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 6             //从服务端获取的输入流,可以获取从服务端返回的信息
 7             BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
 8             //写出流  方式一 把信息发送到服务端
 9             PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
10             //写出流 方式二
11             //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
12             
13             String line = null;
14             while((line=br.readLine())!=null){
15                 //写到服务端 方式一
16                 pw.println(line);
17                 //写到服务端 方式二
18                 /*
19                     bw.write(line);
20                     bw.newLine();
21                     bw.flush();
22                 */
23                 //读取服务端返回信息(这是一个阻塞方法)
24                 String result = in.readLine();
25                 
26                 System.out.println(result);
27             }
28             s.close();
29             br.close();
30    }

服务端代码:

  

1 public static void main(String[] args) throws Exception {
2         ServerSocket ss = new ServerSocket(10001);
3         while(true){
4             //此方法是阻塞式方法
5             Socket s = ss.accept();
6             //服务端这儿需要使用线程 每次接受一个客户端就为其开启一个线程,防止多个客户端访问出现阻塞情况
7             new Thread(new ServerThread(s)).start();
8         }
9   }

 

 1 class ServerThread implements Runnable{
 2     
 3     private Socket socket;
 4     
 5     public ServerThread(Socket socket) {
 6         this.socket = socket;
 7     }
 8     
 9     @Override
10     public void run() {
11         try {
12             System.out.println(socket.getInetAddress().getHostAddress()+"is connecting");
13             BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
14             PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
15             String line =null;
16             while((line = br.readLine())!=null){
17                 //如果接收到结束标志 则结束
18                 if("over".equals(line)){
19                     break;
20                 }
21                 System.out.println(line);
22                 pw.println(line.toUpperCase());
23             }
24         } catch (IOException e) {
25             // TODO Auto-generated catch block
26             e.printStackTrace();
27         }
28         
29     }
30 }
View Code

 

Example2:上传文件——客户端将文件内容写到服务端,服务端 将内容保存到其他位置,实现从服务端上传文件的功能

  客户端代码:

 1 public static void main(String[] args) throws Exception {
 2             Socket s = new Socket(InetAddress.getLocalHost(),10001);
 3             BufferedReader br = new BufferedReader(new FileReader(new File("D:/baichuanyu/HelloWorld/src/com/action/Hello.java")));
 4             BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
 5             PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
 6             String line = null;
 7             while((line=br.readLine())!=null){
 8                 pw.println(line);
 9             }
10             //文件内容写到服务器完成后,服务端没有接受到结束标志,会继续readline()下去
11             //通过shutdownOutput()方法 可以向服务器写-1结束标志 从而结束服务端的readline()方法
12             s.shutdownOutput();
13             String result = in.readLine();
14             System.out.println(result);
15             s.close();
16             br.close();
17         }

  服务端代码:

  

 1 class UploadThread implements Runnable{
 2 
 3     private Socket socket;
 4     
 5     public UploadThread(Socket socket) {
 6         this.socket = socket;
 7     }
 8     
 9     @Override
10     public void run() {
11         try {
12             System.out.println(socket.getInetAddress().getHostAddress()+" is connecting....");
13             BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
14             PrintWriter pw = new PrintWriter(new FileWriter(new File("E:\\Hello.txt")),true);
15             PrintWriter os = new PrintWriter(socket.getOutputStream(),true);
16             String line =null;
17             while((line = br.readLine())!=null){
18                 pw.println(line);
19             }
20             os.println("over");
21             pw.close();
22         } catch (IOException e) {
23             // TODO Auto-generated catch block
24             e.printStackTrace();
25         }
26         
27     }
28 }
View Code
1 public static void main(String[] args) throws Exception {
2         ServerSocket ss = new ServerSocket(10001);
3         while(true){
4             //此方法是阻塞式方法
5             Socket s = ss.accept();
6             //服务端这儿需要使用线程 每次接受一个客户端就为其开启一个线程,防止多个客户端访问出现阻塞情况
7             new Thread(new UploadThread(s)).start();
8         }
9      }

 

posted on 2014-11-06 09:50  欲上云端  阅读(289)  评论(0编辑  收藏  举报