導航:首頁 > 編程語言 > javaudp代碼

javaudp代碼

發布時間:2024-11-27 11:20:13

『壹』 請教大神,怎麼使用java實現UDP埠掃描

給你個UDP服務端與客戶端的示例:

服務端代碼:


importjava.net.DatagramPacket;
importjava.net.InetAddress;
importjava.net.MulticastSocket;

publicclassUDPMulticastServer{
finalstaticintRECEIVE_LENGTH=1024;

staticStringmulticastHost="224.0.0.1";

staticintlocalPort=9998;

publicstaticvoidmain(String[]args)throwsException{

InetAddressreceiveAddress=InetAddress.getByName(multicastHost);

if(!receiveAddress.isMulticastAddress()){//測試是否為多播地址

thrownewException("請使用多播地址");

}

intport=localPort;

=newMulticastSocket(port);

receiveMulticast.joinGroup(receiveAddress);


booleanisStop=false;
while(!isStop){
DatagramPacketdp=newDatagramPacket(newbyte[RECEIVE_LENGTH],RECEIVE_LENGTH);
receiveMulticast.receive(dp);
Stringdata=newString(dp.getData()).trim();
System.out.println(data);
if("exit".equals(data)){
System.out.println("程序退出");
isStop=true;
}
}

receiveMulticast.close();
}
}

客戶端代碼:


importjava.net.DatagramPacket;
importjava.net.InetAddress;
importjava.net.MulticastSocket;

publicclassUDPMulticastClient{
staticStringdestAddressStr="224.0.0.1";

staticintdestPortInt=9998;

staticintTTLTime=4;

publicstaticvoidmain(String[]args)throwsException{ InetAddressdestAddress=InetAddress.getByName(destAddressStr);

if(!destAddress.isMulticastAddress()){//檢測該地址是否是多播地址

thrownewException("地址不是多播地址");

}

intdestPort=destPortInt;


MulticastSocketmultiSocket=newMulticastSocket();

// intTTL=TTLTime;
// multiSocket.setTimeToLive(TTL);

byte[]sendMSG="exit".getBytes();

DatagramPacketdp=newDatagramPacket(sendMSG,sendMSG.length,destAddress,destPort);

multiSocket.send(dp);

multiSocket.close();

}

}

『貳』 我要一份用java網路編程寫的點對點的兩人聊天程序(TCP和UDP)

Server端:
import java.io.*;
import java.net.*;
import java.applet.Applet;
public class TalkServer{
public static void main(String args[]) {
try{
ServerSocket server=null;
try{
server=new ServerSocket(4700);
}catch(Exception e) {
System.out.println("can not listen to:"+e);
}
Socket socket=null;
try{
socket=server.accept();
}catch(Exception e) {
System.out.println("Error."+e);
}
String line;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client:"+is.readLine());
line=sin.readLine();
while(!line.equals("bye")){
os.println(line);
os.flush();
System.out.println("Server:"+line);
System.out.println("Client:"+is.readLine());
line=sin.readLine();
}
os.close();
is.close();
socket.close();
server.close();
}catch(Exception e){
System.out.println("Error:"+e);
}
}
}

Client端:
import java.io.*;
import java.net.*;
public class TalkClient {
public static void main(String args[]) {
try{
Socket socket=new Socket("127.0.0.1",4700);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter os=new PrintWriter(socket.getOutputStream());
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String readline;
readline=sin.readLine(); //從系統標准輸入讀入一字元串
while(!readline.equals("bye")){
os.println(readline);
os.flush();
System.out.println("Client:"+readline);
System.out.println("Server:"+is.readLine());
readline=sin.readLine(); //從系統標准輸入讀入一字元串
}
os.close(); //關閉Socket輸出流
is.close(); //關閉Socket輸入流
socket.close(); //關閉Socket
}catch(Exception e) {
System.out.println("Error"+e); //出錯,則列印出錯信息
}
}
}

『叄』 關於java UDP方面的一些問題

看你的程序好像要實現多點傳輸(多播組), 可以參考MulticastSocket,也是基於UDP協議的 ,可以自動廣播數據到連接上的客戶端,不用等待每個客戶端都要復制。那樣如果數據量大,客戶端多就太耗資源了。

閱讀全文

與javaudp代碼相關的資料

熱點內容
為什麼安卓手機開淘寶會卡 瀏覽:934
啟迪人生編程教程 瀏覽:294
intouch正在編譯無法打開 瀏覽:835
什麼編譯器適合做網頁 瀏覽:850
如何破解騰訊伺服器 瀏覽:87
嵌入式能編譯文件的許可權 瀏覽:12
轎車空調壓縮機突然怎麼不工作了 瀏覽:432
dd命令怎麼解壓 瀏覽:397
舊版安卓怎麼開鎖 瀏覽:247
開發程序員推薦 瀏覽:263
企信如何禁止訪問其他app 瀏覽:71
程序員辦公本推薦2019 瀏覽:197
手機qq下載app在哪裡 瀏覽:536
阿里程序員喊話 瀏覽:982
軟體伺服器地址不對怎麼辦 瀏覽:156
gcco的編譯格式 瀏覽:192
手持噴碼機加密怎麼買墨盒 瀏覽:744
cadcc命令 瀏覽:902
安卓手機攝像頭為什麼不用藍寶石 瀏覽:958
編譯器保留數字 瀏覽:8