JavaSocket学习---基础篇_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JavaSocket学习---基础篇

JavaSocket学习---基础篇

 2014/9/10 12:39:42  商人shang  程序员俱乐部  我要评论(0)
  • 摘要:这里首先介绍几个概念:BIO——同步阻塞IO、NIO——同步非阻塞IO、AIO——异步非阻塞IO今天的例子是最简单的一个例子,没有引入多线程,仅仅是一个单线程阻塞的Socket例子,多线程的下次再继续。Server端得代码如下:packagecom.henushang.socket;importjava.io.BufferedReader;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket
  • 标签:学习 Java socket

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

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

?

Server端得代码如下:

class="java" name="code">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.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.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网络编程精解》

?

?

?

  • BlockProgram.zip (1.8 KB)
  • 下载次数: 0
发表评论
用户名: 匿名