Java基于Socket的简单推送

做过安卓的肯定大多使用过推送,其实我们也可以通过socket做一个简单的推送平台,如果想考虑高并发的情况可以采用mina或者netty等开源框架(如果有时间我会把关于minasocket的文章补上来)。这里就先用Java原生的socket简单实现以下。其实推送的原理也无非就是服务端与客服端要建立一个长连接,建立连接后客户端要不停的去监听来自服务端的消息。

再贴代码之前我先稍微说一下基于socket的简单推送的过程。

首先:开启服务端,并暴露出端口。然后通过一个while的死循环去不停的接收来自客户端的socket,并且通过一个ArrayList来维护。并且通过子线程去对list中的socket进行操作。

其次:客户端去访问服务器的端口。开启一个子线程去不停的接收来自服务端的消息。

服务端代码:

package com.push.mypush;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class MyPushServer {
	ArrayList<Socket> sockets = new ArrayList<Socket>();
	public static void main(String[] args) {
		
		MyPushServer myPushServer = new MyPushServer();
		try {
			myPushServer.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void start() throws IOException {
		
		ServerSocket serverSocket = new ServerSocket(4000);
		System.out.println("服务启动成功");
		while (true) {
			Socket socket = serverSocket.accept();
			System.out.println("server started... http:/"+socket.getLocalAddress()+":"+socket.getPort());
			System.out.println(socket.getLocalSocketAddress()+":"+socket.getLocalPort());
			sockets.add(socket);
			synchronized (ServerSocket.class) {
				ServerThread serverThread = new ServerThread();
				serverThread.start();
			}
		}
	}
	class ServerThread extends Thread{
		private BufferedReader reader;
		private BufferedWriter writer;
		private BufferedReader read;
		public ServerThread() {
			reader = new BufferedReader(new InputStreamReader(System.in));
		}

		@Override
		public void run() {
			String content = null;
			String getContent = null;
			while (true) {
				try {
					if ((content = reader.readLine())!=null) {
						for (Socket socket : sockets) {
							writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
							writer.write(content);
							writer.newLine();
							writer.flush();
							System.out.println("write");
							read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
							if ((getContent = read.readLine()) != null) {
								System.out.println(getContent);
							}
						}
					}
				} catch (Exception e) {
					e.getStackTrace();
				}
			}
		}
	}
	
}
客户端代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class MyPushClient
{
	private Socket socket;
	public static void main(String[] args)
	{
		MyPushClient myPushClient = new MyPushClient();
		try
		{
			myPushClient.start();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	public void  start() throws Exception
	{
		socket = new Socket("127.0.0.1",4000);
		System.out.println("server connection..");
		ClientThread clientThread = new ClientThread();
		clientThread.start();
	}
	class ClientThread extends Thread{
		private BufferedReader reader;
		private BufferedWriter writer;

		public ClientThread()
		{

			try
			{
				reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			} catch (IOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		@Override
		public void run()
		{
			String content = null;
			while (true)
			{
				try
				{
					if ((content = reader.readLine()) != null)
					{
						System.out.println(content);
						writer.write("收到消息");
						writer.newLine();
						writer.flush();
					}
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}




你可能感兴趣的:(java,线程,并发,socket)