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

Java Socket 示例01

时间:2014-11-20 18:19:34      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   java   on   

Server 端代码:

package ServerSide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {

    private int port = 8088;
    private ServerSocket serverSocket;
    
    public EchoServer() {
        
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public String echo(String msg) {
        
        return "echo: " + msg;
    }
    
    private PrintWriter getWriter(Socket socket) {
        
        OutputStream socketOut = null;
        try {
            socketOut = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new PrintWriter(socketOut, true);
    }
    
    private BufferedReader getReader(Socket socket) {
        
        InputStream socketIn = null;
        try {
            socketIn = socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BufferedReader(new InputStreamReader(socketIn));
    }
    
    public void service() {
        
        while(true) {
            Socket socket = null;
            try {
                socket = serverSocket.accept();
                System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
                
                BufferedReader br = getReader(socket);
                PrintWriter pw = getWriter(socket);
                
                String msg = null;
                while((msg = br.readLine()) != null) {
                    System.out.println(msg);
                    pw.println(echo(msg));
                    if(msg.equals("bye")) 
                        break;
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    if(socket != null) {
                        socket.close();
                    }
                }
                catch (IOException e) {
package ClientSide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class EchoClient {

    private static String host = "10.11.121.239";
    private static int port = 8088;
    private Socket socket;
    
    public EchoClient(String hostname) {
        
        host = hostname;
        try {
            socket = new Socket(host, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public EchoClient() {
        
        this(host);
    }
    
    
    private PrintWriter getWriter(Socket socket) {
        
        OutputStream socketOut = null;
        try {
            socketOut = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new PrintWriter(socketOut, true);
    }
    
    
    private BufferedReader getReader(Socket socket) {
        
        InputStream socketIn = null;
        try {
            socketIn = socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BufferedReader(new InputStreamReader(socketIn));
    }
    
    
    public void talk() {
        
        try {
            BufferedReader br = getReader(socket);
            PrintWriter pw = getWriter(socket);
            
            BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
            String msg = null;
            while((msg = localReader.readLine()) != null) {
                pw.println(msg);
                System.out.println(br.readLine());
                
                if(msg.equals("bye"))
                    break;
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                socket.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    public static void main(String[] args) {
        
        new EchoClient().talk();
    }
}

 


                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void main(String[] args) throws IOException {
        
        new EchoServer().service();
    }
}

 

 

Client 代码:

 

Java Socket 示例01

标签:style   blog   io   ar   color   os   sp   java   on   

原文地址:http://www.cnblogs.com/starzou/p/4111082.html

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