java socket单线程阻塞

这里首先介绍几个概念:BIO——同步阻塞IO、NIO——同步非阻塞IO、AIO——异步非阻塞IO

今天的例子是最简单的一个例子,没有引入多线程,仅仅是一个单线程阻塞的Socket例子,多线程的下次再继续。

这样子的例子仅仅供学习使用,基本上是没有实用意义的,因为在实际的应用中基本上是不止于一个Client的。

Server端得代码如下:

Java代码  
package com.henushang.socket;   
  
import java.io.BufferedReader;   
import java.io.PrintWriter;   
import java.net.ServerSocket;   
import java.net.Socket;   
  
import com.henushang.socket.util.SocketUtils;   
  
public class EchoServer {   
    private int port = 8000;   
    private ServerSocket serverSocket;   
       
    public EchoServer() throws Exception {   
        serverSocket = new ServerSocket(port);   
        System.out.println("waitting connet...");   
    }   
       
    public String echo(String msg) {   
        return "echo:" + msg;   
    }   
       
    public void service() {   
        Socket socket = null;   
        String msg = null;   
        while (true) {   
            try {   
                socket = serverSocket.accept();// 准备接受连接   
                System.out.println("new connection: " + socket.getInetAddress() + ":" + socket.getPort());   
                BufferedReader reader = SocketUtils.getReader(socket);// 输入流   
                PrintWriter writer = SocketUtils.getWriter(socket);// 输出流   
                while ((msg = reader.readLine()) != null) {// 接收消息   
                    System.out.println(msg);   
                    writer.println(echo(msg));// 发送消息   
                    writer.flush();// 注意,在使用缓冲流在发送消息的时候最好进行强制刷新,否则,可能会由于缓冲区不满而暂时不发送消息   
                    if ("bye".equals(msg)) {   
                        break;   
                    }   
                }   
            } catch (Exception e) {   
                e.printStackTrace();   
            }finally {   
                SocketUtils.close(socket);   
            }   
        }   
    }   
       
    public static void main(String[] args) {   
        try {   
            new EchoServer().service();   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
       
}  


package com.henushang.socket;

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import com.henushang.socket.util.SocketUtils;
public class EchoServer {
private int port = 8000;
private ServerSocket serverSocket;

public EchoServer() throws Exception {
serverSocket = new ServerSocket(port);
System.out.println("waitting connet...");
}

public String echo(String msg) {
return "echo:" + msg;
}

public void service() {
Socket socket = null;
String msg = null;
  while (true) {
try {
socket = serverSocket.accept();// 准备接受连接
System.out.println("new connection: " + socket.getInetAddress() + ":" + socket.getPort());
BufferedReader reader = SocketUtils.getReader(socket);// 输入流
PrintWriter writer = SocketUtils.getWriter(socket);// 输出流
while ((msg = reader.readLine()) != null) {// 接收消息
System.out.println(msg);
writer.println(echo(msg));// 发送消息
writer.flush();// 注意,在使用缓冲流在发送消息的时候最好进行强制刷新,否则,可能会由于缓冲区不满而暂时不发送消息
if ("bye".equals(msg)) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
SocketUtils.close(socket);
}
}
}

public static void main(String[] args) {
try {
new EchoServer().service();
} catch (Exception e) {
e.printStackTrace();
}
}

}


客户端代码:

Java代码  
package com.henushang.socket;   
  
import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStreamReader;   
import java.io.PrintWriter;   
import java.net.Socket;   
  
import com.henushang.socket.util.SocketUtils;   
  
public class EchoClient {   
    private String host = "127.0.0.1";   
    private int port = 8000;   
    private Socket socket ;   
       
    public EchoClient() throws Exception {   
        socket = new Socket(host, port);   
    }   
       
    public void talk() throws IOException {   
        try {   
            BufferedReader reader = SocketUtils.getReader(socket);   
            PrintWriter writer = SocketUtils.getWriter(socket);   
            // 读取本地控制台的消息   
            BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));   
            String msg = null;   
            while ((msg = localReader.readLine()) != null) {   
                writer.println(msg);   
                writer.flush();   
                System.out.println(reader.readLine());   
                if ("bye".equals(msg)) {   
                    break;   
                }   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
        }finally {   
            SocketUtils.close(socket);   
        }   
    }   
       
    public static void main(String[] args) throws Exception{   
        new EchoClient().talk();   
    }   
}  


package com.henushang.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import com.henushang.socket.util.SocketUtils;
public class EchoClient {
private String host = "127.0.0.1";
private int port = 8000;
private Socket socket ;

public EchoClient() throws Exception {
socket = new Socket(host, port);
}

public void talk() throws IOException {
try {
BufferedReader reader = SocketUtils.getReader(socket);
PrintWriter writer = SocketUtils.getWriter(socket);
// 读取本地控制台的消息
BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
while ((msg = localReader.readLine()) != null) {
writer.println(msg);
writer.flush();
System.out.println(reader.readLine());
if ("bye".equals(msg)) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
SocketUtils.close(socket);
}
}

public static void main(String[] args) throws Exception{
new EchoClient().talk();
}
}
 
下面是我写的一个工具类,做了一些重复的操作的汇集,如下:


Java代码  
package com.henushang.socket.util;   
  
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;   
import java.nio.channels.SocketChannel;   
  
public class SocketUtils {   
       
    public static PrintWriter getWriter(Socket socket) throws IOException {   
        OutputStream os = socket.getOutputStream();   
        return new PrintWriter(os);   
    }   
       
    public static PrintWriter getWriter(SocketChannel socketChannel) throws IOException {   
        return getWriter(socketChannel.socket());   
    }   
       
    public static BufferedReader getReader(Socket socket) throws IOException{   
        InputStream is = socket.getInputStream();   
        return new BufferedReader(new InputStreamReader(is, "UTF-8"));   
    }   
       
    public static BufferedReader getReader(SocketChannel socketChannel) throws IOException{   
        return getReader(socketChannel.socket());   
    }   
       
    public static void close(Socket socket) {   
        try {   
            if (socket != null) {   
                socket.close();   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
  
    public static void close(SocketChannel socketChannel) {   
        try {   
            if (socketChannel != null) {   
                socketChannel.close();   
            }   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
}  


package com.henushang.socket.util;


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;
import java.nio.channels.SocketChannel;

public class SocketUtils {

public static PrintWriter getWriter(Socket socket) throws IOException {
OutputStream os = socket.getOutputStream();
return new PrintWriter(os);
}

public static PrintWriter getWriter(SocketChannel socketChannel) throws IOException {
return getWriter(socketChannel.socket());
}

public static BufferedReader getReader(Socket socket) throws IOException{
InputStream is = socket.getInputStream();
return new BufferedReader(new InputStreamReader(is, "UTF-8"));
}

public static BufferedReader getReader(SocketChannel socketChannel) throws IOException{
return getReader(socketChannel.socket());
}

public static void close(Socket socket) {
try {
if (socket != null) {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}


public static void close(SocketChannel socketChannel) {
try {
if (socketChannel != null) {
socketChannel.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
java socket多线程阻塞请点击,http://www.shaccp.cn/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值