我在这里有一个服务器-客户端基础,但似乎当服务器和客户端握手时,我可以让服务器将消息发送回客户端。
这是服务器端
package me.game.net;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import me.game.WorldOfTrade;
public class NetServerHandler implements Runnable {
public static final int max_packet_size = 512;
public static final int max_clients = 5;
DatagramSocket socket;
List<DatagramSocket> connectedClients;
public NetServerHandler(short port) {
try {
socket = new DatagramSocket(port);
connectedClients = new ArrayList<DatagramSocket>();
} catch (SocketException e) {
System.out.println("Failed to initialize the server...");
e.printStackTrace();
}
}
public boolean handlePacket(byte[] pck, InetAddress add) throws IOException{
String str = new String(pck);
str = str.trim();
if(str.equalsIgnoreCase("ping")){
System.out.println("Client ==>> Us: ping");
sendPacket(easyPacket("pong").getData(), add);
return true;
}
return false;
}
public DatagramPacket easyPacket(String buffer){
byte[] buff = buffer.getBytes();
return new DatagramPacket(buff, buff.length);
}
public void run() {
System.out.println("Running server!");
new Thread(new Runnable() {
@Override
public void run() {
while(WorldOfTrade.isRunning){
DatagramPacket pck = new DatagramPacket(new byte[max_packet_size], max_packet_size);
try {
socket.setSoTimeout(200);
socket.receive(pck);
DatagramSocket newSocket = new DatagramSocket();
newSocket.connect(pck.getAddress(), 25565);
if(connectedClients.size() > 0){
continue;
}
connectedClients.add(newSocket);
System.out.println("Found new client! "+new String(pck.getData()));
sendPacket(easyPacket("pong").getData(), pck.getAddress());
} catch (IOException e) {
if(!(e instanceof SocketTimeoutException)){
e.printStackTrace();
}
}
}
}
}
).start();
while (WorldOfTrade.isRunning) {
if(connectedClients.isEmpty()){
continue;
}
int i = 0;
//System.out.println("Checking "+i);
DatagramSocket sck = socket;
DatagramPacket pck = new DatagramPacket(new byte[max_packet_size], max_packet_size);
try {
sck.setSoTimeout(0);
sck.receive(pck);
} catch (IOException e) {
if(e instanceof SocketTimeoutException){
}else{
e.printStackTrace();
}
}
try {
if(!handlePacket(pck.getData(), pck.getAddress())){
//sendPacket(easyPacket("pong").getData(), i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
public void sendPacket(byte[] buffer, InetAddress add) throws IOException {
if(buffer.length > max_packet_size){
System.out.println("Couldnt send packet because it was to big!");
return;
}
DatagramPacket pck = new DatagramPacket(buffer, buffer.length, add, 25565);
System.out.println("Sending packet to "+add.getHostAddress()+":"+25565);
socket.connect(add, 25565);
socket.send(pck);
}
}
这是客户端
package me.game.net;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import me.game.WorldOfTrade;
public class NetHandler implements Runnable {
DatagramSocket sck;
public NetHandler(String connect){
try {
sck = new DatagramSocket();
sck.connect(InetAddress.getByName(connect), (short)25565);
sck.setSoTimeout(0);
} catch (SocketException | UnknownHostException e) {
e.printStackTrace();
}
}
public boolean handlePacket(byte[] pck) throws IOException{
String str = new String(pck);
str = str.trim();
System.out.println("Recieved Packet: "+str);
if(str.equalsIgnoreCase("pong")){
System.out.println("Server ==>> Us : pong");
return true;
}
System.out.println("!");
return false;
}
public DatagramPacket easyPacket(String buffer){
byte[] buff = buffer.getBytes();
return new DatagramPacket(buff, buff.length);
}
public void run() {
System.out.println("Running client connector!");
boolean timedOut = false;
try {
sendPacket(easyPacket("ping").getData(), sck);
} catch (IOException e) {
e.printStackTrace();
}
int update = 0;
while (WorldOfTrade.isRunning) {
if(WorldOfTrade.frame == null){
continue;
}
WorldOfTrade.frame.setTitle("Timed Out: "+timedOut);
update++;
if (update % 2 == 0) {
try {
sendPacket(easyPacket("ping").getData(), sck);
} catch (IOException e) {
e.printStackTrace();
}
}else{
DatagramPacket pck = new DatagramPacket(new byte[NetServerHandler.max_packet_size], NetServerHandler.max_packet_size);
while(true){
try {
//System.out.println("Client: Listening for packet "+System.currentTimeMillis());
sck.setSoTimeout(500);
sck.receive(pck);
System.out.println(new String(pck.getData()));
handlePacket(pck.getData());
timedOut = false;
break;
} catch (SocketTimeoutException e) {
// if(e instanceof SocketTimeoutException){
// timedOut = true;
// continue;
// }
try {
sendPacket(easyPacket("ping").getData(), sck);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
sck.close();
}
public void sendPacket(byte[] buffer, DatagramSocket sck) throws IOException {
if(buffer.length > NetServerHandler.max_packet_size){
System.out.println("Couldnt send packet because it was to big!");
return;
}
DatagramPacket pck = new DatagramPacket(buffer, buffer.length, sck.getInetAddress(), 25565);
sck.send(pck);
}
}