服务端
- package com.kinth;
- 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 ServerSocketThread extends Thread {
- private Socket socket;
- public ServerSocketThread(Socket socket) {
- this.socket = socket;
- }
- @Override
- public void run() {
- InputStream is = null;
- InputStreamReader isr = null;
- BufferedReader br = null;
- OutputStream os = null;
- PrintWriter pw = null;
- try {
- // socket 获取字节输入流
- is = socket.getInputStream();
- // 将字节输入流转字符输入流
- isr = new InputStreamReader(is,"GB2312");
- // 将字符输入流转行输入流
- br = new BufferedReader(isr); //
- String message = "";
- while(!(message=br.readLine()).equals("bye")){
- System.out.println(message);
- }
- /* while(br.readLine()!=null) {
- message=line;
- System.out.println("客户端发来告警异常消息:"+message);
- message="";
- }*/
- // 必须先关闭输入流才能获取下面的输出流
- // socket.shutdownInput();
- // 获取输出流
- os = socket.getOutputStream();
- pw = new PrintWriter(os);
- pw.write("欢饮您进入 socket");
- pw.flush();
- // 关闭输入流
- //socket.shutdownOutput();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- /*// 关闭资源
- if (pw != null) {
- pw.close();
- }
- try {
- if (br != null) {
- br.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (isr != null) {
- isr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (os != null) {
- os.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }*/
- }
- }
- }
服务开启
- package com.kinth;
- import java.io.IOException;
- import java.NET.ServerSocket;
- public class Socket {
- private static ServerSocket SERVER_SOCKET =null;;
- static{
- try {
- SERVER_SOCKET = new ServerSocket(9773);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- try {
- System.out.println("****** 服务器已启动,--- 等待客户端连接 *****");
- java.NET.Socket socket = null;
- while(true){
- // 循环监听客户端的连接
- socket = SERVER_SOCKET.accept();
- // 新建一个线程 ServerSocket, 并开启
- new ServerSocketThread(socket).start();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally{
- try {
- if(SERVER_SOCKET != null){
- SERVER_SOCKET.close();
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- }
客户端调用
- package com.kinth;
- 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;
- /**
- * <p>
- * 基于 socket 通讯 - 客户端
- * <p>
- *
- * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a>
- * @version
- * @since 2017 年 1 月 12 日
- */
- public class Client {
- public static void main(String[] args) {
- Socket socket = null;
- OutputStream os = null;
- PrintWriter pw = null;
- InputStream is = null;
- InputStreamReader isr = null;
- BufferedReader br = null;
- try {
- socket = new Socket("127.0.0.1", 9773);
- // 获取输出流向服务端写入数据
- os = socket.getOutputStream();
- pw = new PrintWriter(os);
- pw.write("kiadgf 1221 agdfnth");
- pw.flush();
- socket.shutdownOutput();
- // 获取输入流接受服务端返回的信息
- is = socket.getInputStream();
- isr = new InputStreamReader(is);
- br = new BufferedReader(isr);
- String message = null;
- while ((message = br.readLine()) != null) {
- System.out.println("服务器说:" + message);
- }
- socket.shutdownInput();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (br != null) {
- br.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (isr != null) {
- isr.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (is != null) {
- is.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (pw != null) {
- pw.close();
- }
- try {
- if (os != null) {
- os.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- if (socket != null) {
- socket.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2969236.html