public class Sever { ServerSocket ss = null; public Sever() {
try {
init();
} catch (Exception e) { System.out.println(e.getMessage());
}
} void init() throws Exception {
ss = new ServerSocket(6666);
System.out.println(ss);
} void waitForClient() {
try {
while (true) {
System.out.println("Waiting for connect!");
Thread t = new Thread(new Client(ss.accept()));

t.start();
} } catch (Exception e) {
System.out.println("waitForClient ->" + e.getClass().getName()
+ ":" + e.getMessage());
} } public static void main(String[] args) {
Sever sever = new Sever();
sever.waitForClient();
}
}

解决方案 »

  1.   

    public class Client {
    private BufferedReader inStream = null; private PrintWriter outStream = null; private Socket s = null; public Client(Socket socket) {
    s = socket;
    try {
    System.out.println("Socket accepted!");
    s.setSoTimeout(300000);
    inStream = new BufferedReader(new InputStreamReader(s
    .getInputStream()));
    outStream = new PrintWriter(new BufferedWriter(
    new OutputStreamWriter(s.getOutputStream())), true); } catch (Exception e) {
    } } /**
     * 用户中止程序执行
     */
    public void destroy() { } public void run() { try { while (true) { String str = inStream.readLine();
    System.out.println("Got String :" + str);
    //对内容进行处理..

    }
    } catch (Exception e) {

    } finally {
    try {
    destroy();
    inStream.close();
    outStream.close();
    s.close();
    } catch (Exception e) {
    } } }
    }
      

  2.   

    上面是以前写的代码~~如何很方便的转化为UDP?
      

  3.   

    把ServerSocket换成DatagramSocket不就得了
      

  4.   

    为什么要用UDP,UDP不能保证数据传输的正确性
      

  5.   

    UDP的流量比较小,在GPRS环境中还是有自己的独到好处的.唯一的缺陷就是丢包比较严重.
      

  6.   

    难道真的没人搞过java下的UDP应用吗?