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

java socket编程基础

时间:2015-04-19 17:50:15      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:


1. [代码]读操作Runable 


1
package com.hrd.test.socket; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 7 /** 8 * 作者: ehomeud 创建于: 2015/4/15 13:15 9 */ 10 public class ReadThread implements Runnable { 11 12 private BufferedReader br; 13 private String sc; 14 15 public ReadThread(BufferedReader br,String sc){ 16 this.br=br; 17 this.sc =sc; 18 } 19 @Override 20 public void run() { 21 while (true){ 22 try { 23 String str = br.readLine(); 24 System.out.println(sc +":" +str); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 } 30 }

2. [代码]写操作Runable

package com.hrd.test.socket;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
 
/**
 * 作者: ehomeud 创建于: 2015/4/15 13:20
 */
public class WriteThread implements Runnable {
 
    private PrintWriter pw;
    private BufferedReader br;
 
    public WriteThread(PrintWriter pw, BufferedReader br) {
        this.pw = pw;
        this.br = br;
    }
 
    @Override
    public void run() {
        try {
            while (true) {
                pw.println(br.readLine());
                pw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. [代码]服务端ServerSocket

package com.hrd.test.socket;
 
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
/**
 * 作者: ehomeud 创建于: 2015/4/15 10:00
 */
public class Server {
 
    public static int PORT = 9999;
    public static void main(String[] args) {
        ServerSocket s = null;
        Socket socket = null;
        BufferedReader br = null;
        PrintWriter pw = null;
        BufferedReader console=null;
 
        ExecutorService pool = Executors.newFixedThreadPool(2);
        try {
            //设置服务端通信端口
            s = new ServerSocket(PORT);
            System.out.println("ServerSocket Start:"+s);
            //等待请求,该方法会阻塞,直到获得请求
            socket = s.accept();
            System.out.println("Connection accept socket:"+socket);
            //用于接收客户端发来的请求
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //用于发送返回信息,发送数据要调用.flush()方法
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            console = new BufferedReader(new InputStreamReader(System.in));
 
            pool.submit(new ReadThread(br,"server"));
            pool.submit(new WriteThread(pw,console));
            while(true){
                Thread.sleep(10000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            System.out.println("关闭中.....");
            try {
                console.close();
                br.close();
                pw.close();
                socket.close();
                s.close();
            } catch (Exception e2) {
            }
        }
    }
}

4. [代码]客户端Socket  

package com.hrd.test.socket;
 
import java.io.*;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
/**
 * 作者: ehomeud 创建于: 2015/4/15 10:10
 */
public class Client {
 
    public static void main(String[] args) {
        Socket socket = null;
        BufferedReader br = null;
        PrintWriter pw = null;
 
        ExecutorService pool = Executors.newFixedThreadPool(2);
        try {
            //客户端socket指定服务器的地址和端口号
            socket = new Socket("127.0.0.1", Server.PORT);
            System.out.println("Socket:" + socket+"客户端启动中...");
            //同服务器原理一样
            br = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                    socket.getOutputStream())));
 
            BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
            //提交读
            pool.submit(new WriteThread(pw,console));
            pool.submit(new ReadThread(br,"client"));
            while (true){
                Thread.sleep(100000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                System.out.println("关闭中......");
                br.close();
                pw.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
 
}

 

 

java socket编程基础

标签:

原文地址:http://www.cnblogs.com/tian830937/p/4439407.html

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