导航:首页 > 编程语言 > 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代码相关的资料

热点内容
appstore怎么设置 浏览:950
在哪个app买韩国女装 浏览:105
php写入文件换行 浏览:749
dsp实现fft算法 浏览:485
棋牌源码转让交易手续费 浏览:293
云上服务器贵州 浏览:647
qq三国怎么使用云服务器 浏览:303
一键加密字体怎么设置 浏览:145
majority算法 浏览:822
如何开启电脑的dlna服务器 浏览:11
3提成怎么算法 浏览:972
php是不是解释性语言 浏览:27
手机设置远程定位服务器地址 浏览:913
android模拟器装apk 浏览:773
炒黄金app哪个好用 浏览:993
恐惧症app哪个最好用 浏览:288
亿赛通加密软件好用吗 浏览:578
为什么光遇排队服务器忙 浏览:826
哪个app能把手p瘦 浏览:253
java中的date类型 浏览:431