参数中的0的意义是什么-字符串消息=新字符串(recievepacket.getData(),0,recievepacket.getLength());

时间:2018-10-18 06:56:45

标签: java networking

如果我使用任何其他数字而不是0,则看不到任何消息。为什么在将接收到的数据包转换为字符串时使用0?

public class UdpClient {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    DatagramSocket cs=new DatagramSocket();
    byte [] recievedata=new byte[512];
    String message=null;
    byte [] senddata=new byte[512];
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("client created");
    InetAddress addr=InetAddress.getByName("127.0.0.1");
    int port=9003;

    while(true) {

        System.out.println( "client says");
        message=br.readLine();

        senddata=message.getBytes();
        DatagramPacket sendpacket=new DatagramPacket(senddata, senddata.length, addr, port);
cs.send(sendpacket);        

DatagramPacket recievepacket=new DatagramPacket(recievedata, recievedata.length);
cs.receive(recievepacket);
message=new String(recievepacket.getData(), 0,recievepacket.getLength());
System.out.println("server says"+message);

    }

}
 }

2 个答案:

答案 0 :(得分:1)

请参见 String 类的the javadoc

var $modal = $("#redirectconfirm-modal");
var currentDomain = 'blabla';
$modal.on('click', '.btn-continue', function(e) {
    window.location = $modal.data('redirectTo');
});
$('a').each(function() {
    var $a = jQuery(this);
    if( $a.get(0).hostname && getDomain($a.get(0).hostname)!=currentDomain ) {
        $a.click(function(e) {
			$modal.data('redirectTo', $a.attr('href'));
            $modal.modal('show');
        });
    };
});

答案 1 :(得分:0)

请阅读String类here的Javadoc

String的原始方法

Model Class:
class TestProject(val name: String,val location: String)

Main Class:     
     private class ItemDetailAdapter(val TestProjectList:Array<TestProject>): RecyclerView.Adapter<ItemDetailViewHolder>()
    {

        override fun onBindViewHolder(p0: ItemDetailViewHolder, p1: Int) {
            val TestProject=TestProjectList.get(p1)
            p0?.customView?.TestProjectName?.text=TestProject.name
            val TestProjectPicture=p0?.customView?.itemPicture
            Picasso.get().load(TestProject.location).into(TestProjectPicture)
//Below code solved the Title Problem
                p0?.Title=TestProject
        }
        override fun getItemCount(): Int {
            return TestProjectList.size

        }
        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ItemDetailViewHolder {
            val layoutInflater=LayoutInflater.from(p0?.context)
            val customView=layoutInflater.inflate(R.layout.items_details,p0,false)
                        return ItemDetailViewHolder(customView)
        }
    }
    class ItemDetailViewHolder(val customView:View,var Title: TestProject?=null):RecyclerView.ViewHolder(customView)
    {
        companion object {
        val ITEM_TITLE_KEY="TestProject"
        }
        init {
            customView.setOnClickListener {
                val intent= Intent(customView.context,TestProjectMenuList::class.java)
                intent.putExtra(ITEM_TITLE_KEY,Title?.name)
                print("Printting Title :$Title?.name")
                println("Hello Test $ITEM_TITLE_KEY")
                customView.context.startActivity(intent)
                println("Test")
            }
        }

    TestProjectList Class:
        val navBarTitle=intent.getStringExtra(MainClass.ItemDetailViewHolder.ITEM_TITLE_KEY)
        supportActionBar?.title=navBarTitle