浏览 2451 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-11-03
项目已经做的差不多了,其中做了一个消息的转发功能,所以自己也写一个简单的例子看看
先一步步的来吧,先写好一个Socket的消息传输的原型来,再一步步的改进吧。
其实在这个基础上要中的东西还是很多的 1.客户端的登陆 2.客户端与服务端的断连后重连 3.消息的编解码 4.将传输改成由接收,发送,处理队列与接收,发送,处理线程来做 5.队列的超时机制 6.多线程安全 7.服务端收到消息可以进行群发或是转发 .....
问题还是很多,先放个简单的东西来做吧
Server原型 服务端主要是为了接收客户端发来的消息并把这个消息群发到每一个客户端去。所以设置了一个List以存入Socket 这里主要做的是在start()方法中开一个线程,让服务端通过这个线程进行进行监听客户端发来的消息并拿到这个Socket对像,其实这东西本不需要开一个线程去做,但是为了不在ss.accept()的时候让整个调用这个方法的主线程在那里等待,所以开了个线程去做,一但接到客户端的消息就把接收线程拉起 package demo; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.LinkedList; import java.util.List; public class Server { private ServerSocket ss = null; private List<Socket> list = new LinkedList<Socket>(); private class ReceiveThread extends Thread { private Socket socket = null; public ReceiveThread(Socket socket) { super("ReceiveThread"); this.socket = socket; } @Override public void run() { InputStream in = null; DataInputStream dis = null; while (true) { try { sleep(50); } catch (InterruptedException e1) { e1.printStackTrace(); } if (socket == null || socket.isClosed() || !socket.isConnected()) { continue; } try { in = socket.getInputStream(); dis = new DataInputStream(in); String readUTF = dis.readUTF(); System.out.println("receive client message :" + socket + " " + readUTF); send(readUTF); } catch (IOException e) { try { in.close(); dis.close(); } catch (IOException e1) { e1.printStackTrace(); } list.remove(socket); e.printStackTrace(); } } } } public void send(String msg) throws IOException { for (int i = 0; i < list.size(); i++) { Socket groupSocket = list.get(i); OutputStream out = groupSocket.getOutputStream(); DataOutputStream dout = new DataOutputStream(out); dout.writeUTF(msg); dout.flush(); out.flush(); } } public class AcceptSocketThread extends Thread { public AcceptSocketThread() { super("AcceptSocketThread"); } public void run() { try { ss = new ServerSocket(7788); while (true) { Socket socket = ss.accept(); list.add(socket); new ReceiveThread(socket).start(); } } catch (IOException e) { e.printStackTrace(); } } } private void start() throws IOException { new AcceptSocketThread().start(); } public static void main(String[] args) throws Exception { System.out.println("Server start ...."); new Server().start(); } }
2.客户端Client 这个就比较简单了,只是为了发送消息和接收消息 package demo; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.util.Scanner; public class Client { private Socket socket = null; private class ReceiveThread extends Thread { @Override public void run() { while (true) { try { InputStream in = socket.getInputStream(); DataInputStream din = new DataInputStream(in); String readUTF = din.readUTF(); System.out.println("Server Send Message: " + readUTF); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public void start() throws IOException { socket = new Socket(InetAddress.getByName("127.0.0.1"), 7788); new ReceiveThread().start(); System.out.println("Client start ....\nPlease input message : "); while (true) { Scanner scaner = new Scanner(System.in); String next = scaner.next(); OutputStream out = socket.getOutputStream(); DataOutputStream dout = new DataOutputStream(out); dout.writeUTF(next); // scaner.close(); dout.flush(); } } public static void main(String[] args) throws IOException { System.out.println("client ..."); Client client = new Client(); client.start(); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |