Socket通信,服务端和客户端互发消息

对于socket的通信原理这里就不做介绍,再我的另外一篇博客里有相关介绍,这里直接上代码
服务端

package com.modou.workday.word;

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;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.modou.workday.domain.Book;

public class socketServerDemo {
	public static void main(String[] args) throws IOException {
		// 创建一个服务器端socket,指定绑定的端口号,并监听此端口
		ServerSocket serverSocket = new ServerSocket(8888);
		// 调用accept()方法开始监听,等待客户端的连接
		System.out.println("**********服务器即将启动,等待客户端的连接*************");
		Socket socket = serverSocket.accept();
		// 获取输入流,并读取客户端信息
		InputStream inp = socket.getInputStream();
		// 把字节流转换成字符流
		InputStreamReader isr = new InputStreamReader(inp);
		// 为字符流增加缓冲区
		BufferedReader bfr = new BufferedReader(isr);
		String info = null;
		while ((info = bfr.readLine()) != null) {// 循环读取数据
			System.out.println("我是服务器,客户端说:" + info);
		}
		socket.shutdownInput();// 关闭输入流
		// 向客户端传递的信息
		OutputStream ots = socket.getOutputStream();
		PrintWriter pw = new PrintWriter(ots);
		Book book = new Book(1, "钢铁是怎样炼成的", "保尔柯察金", 12, 29);
		List list = new ArrayList();
		list.add(book);
		// 将list集合转换成json字符串
		pw.write(JSON.toJSONString(list));
		pw.flush();

		// 关闭资源
		pw.close();
		ots.close();
		bfr.close();
		isr.close();
		inp.close();
		socket.close();
		serverSocket.close();
	}

}

客户端

package com.modou.workday.word;

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.net.UnknownHostException;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.modou.workday.domain.Book;

public class socketClientDemo {
	public static void main(String[] args) throws Exception {
		try {
			// 创建一个客户端socket
			Socket socket = new Socket("localhost", 8888);
			// 向服务器端传递信息
			OutputStream ots = socket.getOutputStream();
			PrintWriter pw = new PrintWriter(ots);
			pw.write("用户名:admin;密码:123");
			pw.flush();
			// 关闭输出流
			socket.shutdownOutput();
			// 获取服务器端传递的数据
			InputStream is = socket.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String info = null;
			while ((info = br.readLine()) != null) {
				// 将json字符串转换成实体类集合
				List list = JSON.parseArray(info, Book.class);
				for (Book book : list) {
					System.out.println(book.getAuthor());
				}
				System.out.println("我是客户端,服务器说:" + info);
			}
			// 关闭资源
			br.close();
			isr.close();
			is.close();
			pw.close();
			ots.close();
			socket.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

结果:

**********服务器即将启动,等待客户端的连接*************
我是服务器,客户端说:用户名:admin;密码:123
保尔柯察金
我是客户端,服务器说:[{"author":"保尔柯察金","id":1,"name":"钢铁是怎样炼成的","price":29.0,"year":12}]

你可能感兴趣的:(接口)