001 package org.protocol; 002 003 import java.util.*; 004 import java.io.*; 005 import java.net.*; 006 import java.awt.*; 007 import java.awt.event.*; 008 import javax.swing.*; 009 import javax.swing.event.*; 010 011 public class SimpleUDPSocket { 012 013 private InputStream in_; 014 private OutputStream out_; 015 private DatagramPacket packet_; 016 private DatagramSocket socket_; 017 private String ip_; 018 private int port_; 019 private int timeout_; 020 private int buf_size_; 021 private boolean is_server_mode_; 022 023 public SimpleUDPSocket(String ip, int port, int timeout, int buf_size) throws Exception { 024 is_server_mode_ = true; 025 ip_ = ip; 026 port_ = port; 027 timeout_ = timeout; 028 buf_size_ = buf_size; 029 init(); 030 } 031 032 public void init() throws Exception { 033 byte[] buf_ = new byte[buf_size_]; 034 InetSocketAddress address = new InetSocketAddress(ip_, port_); 035 packet_ = new DatagramPacket(buf_, buf_.length, address); 036 socket_ = new DatagramSocket(); 037 038 in_ = new InputStream() { 039 int c = 'a'; 040 byte[] receive_buf = new byte[buf_size_]; 041 int length = 0; 042 int offset = 0; 043 public int read () { 044 if(offset==length) { 045 receive(); 046 if(offset==0) 047 return -1; 048 } 049 return receive_buf[offset++]; 050 } 051 public void receive() { 052 try{ 053 packet_.setData(receive_buf, 0, receive_buf.length); 054 socket_.receive(packet_); 055 receive_buf = packet_.getData(); 056 offset = 0; 057 length = packet_.getLength(); 058 }catch(Exception e){ 059 e.printStackTrace(); 060 } 061 } 062 }; 063 064 out_ = new OutputStream() { 065 byte[] send_buf = new byte[buf_size_]; 066 int length = 0; 067 int offset = 0; 068 public void write(int c) { 069 send_buf[length++] = (byte)c; 070 // System.out.println("write: "+(char)c); 071 } 072 public void flush() { 073 try{ 074 packet_.setData(send_buf, offset, length); 075 socket_.send(packet_); 076 offset = 0; 077 length = 0; 078 }catch(Exception e){ 079 e.printStackTrace(); 080 } 081 } 082 }; 083 } 084 public InputStream getInputStream() { 085 return in_; 086 } 087 public OutputStream getOutputStream() { 088 return out_; 089 } 090 091 public static void main(String[] args) throws Exception { 092 SimpleUDPSocket socket = new SimpleUDPSocket("localhost", 3310, 0, 1024); 093 InputStream in = socket.getInputStream(); 094 OutputStream out = socket.getOutputStream(); 095 for(int i=0;i<1000;i++) { 096 byte[] buf = (String.valueOf(i)+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").getBytes(); 097 out.write(buf); 098 out.flush(); 099 // Thread.sleep(5); 100 } 101 102 for(int i=in.read(); i!=-1; i=in.read()) 103 System.out.println("receive: " + (char)i); 104 in.close(); 105 out.close(); 106 } 107 public static void main2(String[] args) throws Exception { 108 SimpleUDPSocket socket = new SimpleUDPSocket("localhost", 3310, 0, 1024); 109 InputStream in = socket.getInputStream(); 110 OutputStream out = socket.getOutputStream(); 111 byte[] buf = "<connect><type>Client</type><protocol_version>1.0</protocol_version><nickname>test</nickname><rc_id>0</rc_id><vt_id>0</vt_id></connect>".getBytes(); 112 out.write(buf); 113 out.flush(); 114 Thread.sleep(1000); 115 out.write(buf); 116 out.flush(); 117 118 for(int i=in.read(); i!=-1; i=in.read()) 119 System.out.println("receive: " + (char)i); 120 in.close(); 121 out.close(); 122 } 123 124 public static void alert(Object message) { 125 System.out.println(message); 126 // javax.swing.JOptionPane.showMessageDialog(null, message); 127 } 128 }