LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3345|回复: 2

Java UDP编程问题

[复制链接]
发表于 2006-3-8 21:01:35 | 显示全部楼层 |阅读模式
最近在学Java网络通讯,做毕业设计,我想使用UDP协议,今天写了一个C/S通讯,但服务器收不到发送出去的数据。
程序如下,Server端:

  1. import java.net.DatagramSocket;
  2. import java.net.SocketException;
  3. import java.nio.channels.DatagramChannel;
  4. import java.net.*;
  5. import java.io.*;
  6. import java.nio.*;
  7. public class STest {
  8.         public static void main(String args[]){
  9.                 new Server();
  10.         }
  11. }

  12. class Server{
  13.         private DatagramChannel channel;
  14.         DatagramSocket socket;
  15.         public Server(){
  16.                 try{
  17.                         channel=DatagramChannel.open();
  18.                         socket=channel.socket();
  19.                         socket.bind(new InetSocketAddress(5569));
  20.                         SThread s=new SThread(channel);
  21.                         s.start();
  22.                 }catch(SocketException e){
  23.                         e.printStackTrace();
  24.                 }catch(IOException e){
  25.                         e.printStackTrace();
  26.                 }
  27.         }
  28. }

  29. class SThread extends Thread{
  30.         private DatagramChannel socket;
  31.         public SThread(DatagramChannel socket){
  32.                 this.socket=socket;
  33.         }
  34.         public void run(){
  35.                 ByteBuffer buf=ByteBuffer.allocate(3000);
  36.                 while(true){
  37.                         buf.clear();
  38.                         try{
  39.                                 socket.receive(buf);
  40.                                 if(buf.limit()==0){
  41.                                         System.out.println("Empty buf");
  42.                                         continue;
  43.                                 }
  44.                                 buf.flip();
  45.                                 System.out.println(buf.getInt());
  46.                         }catch(IOException e){
  47.                                 e.printStackTrace();
  48.                         }
  49.                 }
  50.         }
  51. }
复制代码


Client端

  1. import java.net.*;
  2. import java.nio.*;
  3. import java.nio.channels.DatagramChannel;
  4. import java.io.*;
  5. public class CTest {
  6.         public static void main(String args[]){
  7.                         try{
  8.                 new Client().start();
  9.                 Thread.sleep(1000);
  10.                         }catch(InterruptedException e){
  11.                                
  12.                         }
  13.         }

  14. }

  15. class Client extends Thread{
  16.         public Client(){
  17.         }
  18.         public void run(){
  19.                 while(true){
  20.                 ByteBuffer buf=ByteBuffer.allocate(3000);
  21.                 buf.flip();
  22.                 try{
  23.                         DatagramChannel dc=DatagramChannel.open();
  24.                         InetSocketAddress ad=new InetSocketAddress("localhost",5569);
  25.                         dc.connect(ad);
  26.                         dc.socket().setSoTimeout(1000);
  27.                         buf.putInt(33559900);
  28.                         int out=dc.write(buf);
  29.                         this.sleep(1000);
  30.                 }catch(IOException e){
  31.                         e.printStackTrace();
  32.                 }catch(InterruptedException e){
  33.                         e.printStackTrace();
  34.                 }
  35.                 }
  36.         }
  37. }
复制代码


当客户端发送数据后,服务器端收到的总是空的ByteBuffer
谁能给我一个有关DatagramChannel的服务器端和客户端的正确的例子?
还有一问,为什么linuxsir越来越慢了。。。。。:ask
发表于 2006-5-20 09:01:33 | 显示全部楼层
我试了你的代码,好像不能用哦。我用的jdk1.4
回复 支持 反对

使用道具 举报

发表于 2006-6-11 19:37:17 | 显示全部楼层
你的Client类代码有问题!
改成
buf.putInt(33559900);
buf.flip();
int out=dc.write(buf);
buf.clear();
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表