win7遊戲的UDP傳輸經常出現在一些小眾網絡遊戲或者聯機遊戲中,它會影響我們的傳輸速度,如果速度過慢會就會讓遊戲畫面、聲音等出現卡頓,我們可以使用下面的代碼優化遊戲UDP傳輸,下面一起來看一下吧。
win7遊戲UDP傳輸優化教程:一、接收端Receive
1、首先我們需要使用文本編輯工具打開遊戲的配置文件。
2、接着輸入下方代碼:
package et;
import ception;
import gramPacket;
import gramSocket;
public class Demo02_Receive {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(6666); // 創建Socket相當於創建碼頭
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);// 創建Packet相當於創建集裝箱
while (true) {
ive(packet); // 接貨,接收數據
byte[] arr = ata(); // 獲取數據
int len = ength(); // 獲取有效的字節個數
String ip = ddress()ostAddress(); // 獲取ip地址
int port = ort(); // 獲取端口號
tln(ip + "" + port + "" + new String(arr, 0, len));
}
}
}
二、發送端Send
1、同樣我們需要使用文本編輯工具打開遊戲的配置文件。
2、然後輸入下方代碼:
package et;
import ception;
import gramPacket;
import gramSocket;
import Address;
import ner;
public class Demo02_Send {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(); // 創建鍵盤錄入對象
DatagramSocket socket = new DatagramSocket(); // 創建Socket相當於創建碼頭
while (true) {
String line = Line(); // 獲取鍵盤錄入的字符串
if ("quit"ls(line)) {
break;
}
DatagramPacket packet = // 創建Packet相當於集裝箱
new DatagramPacket(ytes(), ytes()th,
yName(""), 6666);
(packet); // 發貨,將數據發出去
}
e();
}
}