最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

客戶(hù)端不能將接收到信息完全打印出來(lái),請(qǐng)問(wèn)是怎么回事呢?(客戶(hù)端顯示在代碼后)

/*
*UDP服務(wù)端
*/

package com.imooc;


import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.SocketException;


public class UDPserver {

UDPserver() throws IOException{

DatagramSocket ds = new DatagramSocket(5555);

byte[] b = new byte[2048];

DatagramPacket dp = new DatagramPacket(b,0,b.length);

ds.receive(dp);

String s = new String(b);

System.out.println("我是服務(wù)器,客戶(hù)端說(shuō): "+s);

InetAddress address = InetAddress.getByName("localhost");

int port = dp.getPort();

byte[] b1 = new byte[2048];

b1= "客戶(hù)端你好,你的信息已經(jīng)收到,我是服務(wù)器".getBytes();

DatagramPacket dp2 = new DatagramPacket(b1,0,b1.length,address,port);

ds.send(dp2);

ds.close();

}

public static void main(String[] args) throws IOException {

UDPserver us = new UDPserver();

}

}

/*
*UDP客戶(hù)端
*/

package com.imooc;


import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.SocketException;


public class UDPclient {

public static void main(String[] args) throws IOException{


DatagramSocket ds = new DatagramSocket();

InetAddress address = InetAddress.getByName("localhost");

int port = 5555;

byte[] b = new byte[2048];

b ="用戶(hù)名:1;密碼:2".getBytes();

DatagramPacket dp = new DatagramPacket(b,b.length,address,port);

ds.send(dp);

DatagramPacket dp1 = new DatagramPacket(b,b.length);

ds.receive(dp1);

String s1 = new String(b);

System.out.println("我是客戶(hù)端,服務(wù)器說(shuō): ?"+s1);

ds.close();

}

}

客戶(hù)端打?。?br />

客戶(hù)端你好,你?

正在回答

4 回答

你前面那個(gè)服務(wù)器端的代碼都沒(méi)有放到main方法里去。

UDPserver us = new UDPserver();

你用的這種方式根本就不能夠調(diào)用上面的那些方法,因?yàn)檫@個(gè)是創(chuàng)建實(shí)例對(duì)象......而你想要的是運(yùn)用那些方法。

0 回復(fù) 有任何疑惑可以回復(fù)我~

接受客戶(hù)端信息時(shí):String s1 = new String(b);應(yīng)該修改為String s1 = new String(b,0,dp1.getLength());

0 回復(fù) 有任何疑惑可以回復(fù)我~

byte[] b = new byte[2048];

b ="用戶(hù)名:1;密碼:2".getBytes();

byte[] b1 = new byte[2048];

b1= "客戶(hù)端你好,你的信息已經(jīng)收到,我是服務(wù)器".getBytes();

你分別給b和b1都賦值了,所以他們的長(zhǎng)度都改變了,當(dāng)你用b來(lái)接受b1時(shí),由于b的長(zhǎng)度比b1短,所以接收不完。

0 回復(fù) 有任何疑惑可以回復(fù)我~

System.out.println("我是服務(wù)器,客戶(hù)端說(shuō): "+s);

InetAddress address = InetAddress.getByName("localhost");

int port = dp.getPort();


address 弄粗了,應(yīng)該發(fā)往的地址是?InetAddress address = packet.getAddress();


0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

客戶(hù)端不能將接收到信息完全打印出來(lái),請(qǐng)問(wèn)是怎么回事呢?(客戶(hù)端顯示在代碼后)

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)