UDP transmission of win7 games often appears in some unpopular Internet games or online games. It will affect people’s transmission speed. If the speed is too slow, the game interface, sound, etc. will freeze. We can Use the following code to design game UDP transmission. Let’s take a look at it below.
win7 game UDP transmission optimization technology:
1. Coordinator Receive
1. First, we need to use a word processing tool to open the environment variables of the game.
2. Then type the following code:
packagecom.heima.socket;
importjava.io.IOException;
importjava.net.DatagramPacket;
importjava.net.DatagramSocket;
publicclassDemo02_Receive{
publicstaticvoidmain(String[]args)throwsIOException{
DatagramSocketsocket=newDatagramSocket(6666);//Create Socket is equivalent to creating a port
DatagramPacketpacket=newDatagramPacket(newbyte[1024],1024);//Creating Packet is equivalent to creating a shipping container
while(true){
socket .receive(packet);//Pick up the goods and get the data
byte[]arr=packet.getData();//Get the data information
intlen=packet.getLength();// Get a reasonable number of bytes
Stringip=packet.getAddress().getHostAddress();//Get the detailed ip address
intport=packet.getPort();//Get the port
System.out.println(ip ":" port ":" newString(arr,0,len));
}
}
}
2. Send
#1. Similarly, we need to use a word processing tool to open the environment variables of the game.
2. Then enter the following code:
packagecom.heima.socket;
importjava.io.IOException;
importjava.net.DatagramPacket;
importjava.net.DatagramSocket;
importjava.net.InetAddress;
importjava.util.Scanner;
publicclassDemo02_Send{
publicstaticvoidmain (String[]args)throwsIOException{
Scannersc=newScanner(System.in);//Create computer keyboard entry target
DatagramSocketsocket=newDatagramSocket();//Creating Socket is equivalent to creating Port
while(true){
Stringline=sc.nextLine();//Get the string array entered by the computer keyboard
if("quit".equals( line)){
break;
}
DatagramPacketpacket=//Creating a Packet is equivalent to a shipping container
newDatagramPacket(line.getBytes(),line .getBytes().length,
InetAddress.getByName("127.0.0.1"),6666);
socket.send(packet);//Arrange delivery and transfer data information Get out
}
socket.close();
}
}
The above is the detailed content of Example tutorial on how to improve game UDP transmission in win7. For more information, please follow other related articles on the PHP Chinese website!