关于java udp编程的小问题,好像服务器和客户端都收不到
importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket...
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ServerUDP {
// 定义服务器接受端口和发送端口
private final int PORT = 9110;
// 定义数据报大小为4k
private final int DATE_LEN = 4096;
// 定义服务器DatagramSocket
private DatagramSocket socket = null;
// 定义用于接受和发送的DatagramPacket对象
private DatagramPacket inPacket;
private DatagramPacket outPacket;
// 定义接受字节数组和发送字节数组
byte[] inBuff = new byte[DATE_LEN];
byte[] outBuff = new byte[DATE_LEN];
// 初始化服务器
public void openServer() {
try {
socket = new DatagramSocket(PORT);
System.out.println("服务器启动成功!!!!");
} catch (Exception e) {
System.out.println("无法启动服务器");
}
}
// 服务器监听
public void serverListener() {
while (true) {
try {
socket.receive(inPacket);
inBuff = inPacket.getData();
System.out.println(new String(inBuff.toString()));
// 返回客户端一条信息
outBuff = "ok".getBytes();
outPacket = new DatagramPacket(outBuff, outBuff.length,inPacket.getSocketAddress());
socket.send(outPacket);
} catch (Exception e) {
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerUDP s=new ServerUDP();
s.openServer();
s.serverListener();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ClientUDP {
// 定义服务器的ip,即本机ip,通过百度本机ip取得
private final String ServerIP = "27.159.29.134";
// 定义服务器接受端口和发送端口
private final int PORT = 9110;
// 定义数据报大小为4k
private final int DATE_LEN = 4096;
// 定义服务器DatagramSocket
private DatagramSocket socket = null;
// 定义用于接受和发送的DatagramPacket对象
private DatagramPacket inPacket;
private DatagramPacket outPacket;
// 定义接受字节数组和发送字节数组
byte[] inBuff = new byte[DATE_LEN];
byte[] outBuff = new byte[DATE_LEN];
public void init() {
try {
socket = new DatagramSocket();
// 取得服务器的InetAddress类型地址
InetAddress a = InetAddress.getByName(ServerIP);
// 发送客户端ip给服务端
String msg="hello";
outBuff = msg.getBytes();
outPacket = new DatagramPacket(outBuff, outBuff.length, a, PORT);
socket.send(outPacket);
// 得到服务端的返回信息
socket.receive(inPacket);
inBuff = inPacket.getData();
System.out.println(inBuff.toString());
} catch (Exception e) {
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClientUDP c = new ClientUDP();
c.init();
}
} 展开
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ServerUDP {
// 定义服务器接受端口和发送端口
private final int PORT = 9110;
// 定义数据报大小为4k
private final int DATE_LEN = 4096;
// 定义服务器DatagramSocket
private DatagramSocket socket = null;
// 定义用于接受和发送的DatagramPacket对象
private DatagramPacket inPacket;
private DatagramPacket outPacket;
// 定义接受字节数组和发送字节数组
byte[] inBuff = new byte[DATE_LEN];
byte[] outBuff = new byte[DATE_LEN];
// 初始化服务器
public void openServer() {
try {
socket = new DatagramSocket(PORT);
System.out.println("服务器启动成功!!!!");
} catch (Exception e) {
System.out.println("无法启动服务器");
}
}
// 服务器监听
public void serverListener() {
while (true) {
try {
socket.receive(inPacket);
inBuff = inPacket.getData();
System.out.println(new String(inBuff.toString()));
// 返回客户端一条信息
outBuff = "ok".getBytes();
outPacket = new DatagramPacket(outBuff, outBuff.length,inPacket.getSocketAddress());
socket.send(outPacket);
} catch (Exception e) {
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerUDP s=new ServerUDP();
s.openServer();
s.serverListener();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class ClientUDP {
// 定义服务器的ip,即本机ip,通过百度本机ip取得
private final String ServerIP = "27.159.29.134";
// 定义服务器接受端口和发送端口
private final int PORT = 9110;
// 定义数据报大小为4k
private final int DATE_LEN = 4096;
// 定义服务器DatagramSocket
private DatagramSocket socket = null;
// 定义用于接受和发送的DatagramPacket对象
private DatagramPacket inPacket;
private DatagramPacket outPacket;
// 定义接受字节数组和发送字节数组
byte[] inBuff = new byte[DATE_LEN];
byte[] outBuff = new byte[DATE_LEN];
public void init() {
try {
socket = new DatagramSocket();
// 取得服务器的InetAddress类型地址
InetAddress a = InetAddress.getByName(ServerIP);
// 发送客户端ip给服务端
String msg="hello";
outBuff = msg.getBytes();
outPacket = new DatagramPacket(outBuff, outBuff.length, a, PORT);
socket.send(outPacket);
// 得到服务端的返回信息
socket.receive(inPacket);
inBuff = inPacket.getData();
System.out.println(inBuff.toString());
} catch (Exception e) {
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ClientUDP c = new ClientUDP();
c.init();
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询