0
我在客戶端/服務器上創建了10個包含rtpPacket的線程。 每個都發送到服務器。服務器和客戶端的代碼如下。 在服務器上,沒有收到少量數據包,有些收到的數據包不正確。 任何幫助將是非常有用的。使用10個線程將10個RtpPackets傳輸到服務器
enter code here
Server.java
import java.net.*;
import java.io.*;
public class Server
{
DatagramSocket ss;
InetAddress add;
DatagramPacket dph,dpp;
int port;
static int i=0;
public void receivePacket() throws Exception
{
ss=new DatagramSocket(10000);
byte b[][]=new byte[10][102400];
byte b1[][]=new byte[10][102400];
while(true)
{
dph=new DatagramPacket(b[i],b[i].length);
ss.receive(dph);
dpp=new DatagramPacket(b1[i],b1[i++].length);
ss.receive(dpp);
add=dph.getAddress();
port=dph.getPort();
ss.connect(add,port);
RetrievePacket1 ob=new RetrievePacket1();
ob.getRtpHeader(dph.getData(),dpp.getData());
if(i==10)
i=0;
}
}
public static void main(String args[]) throws Exception
{
Server ob=new Server();
ob.receivePacket();
}
}
class RetrievePacket1
{
int version;
int padd;
int mark;
int ext;
int cc;
int pType;
int sequence;
int time;
int ssrc;
int header_size=12;
public void getRtpHeader(byte []header,byte []payload)
{
System.out.println(payload[0]+" "+payload[1]);
cc=header[0]&15;
ext=(header[0]>>4)&1;
padd=(header[0]>>5)&1;
version=(header[0]>>6)&3;
pType=header[1]&127;
mark=(header[1]>>7)&1;
sequence = unsigned_int(header[3]) + 256*unsigned_int(header[2]);
time= unsigned_int(header[7]) + 256*unsigned_int(header[6]) + 65536*unsigned_int(header[5]) + 16777216*unsigned_int(header[4]);
ssrc=unsigned_int(header[8]) + 256*unsigned_int(header[9]) + 65536*unsigned_int(header[10]) + 16777216*unsigned_int(header[11]);
System.out.println("Version: "+version);
System.out.println("Padding: "+padd);
System.out.println("Extension: "+ext);
System.out.println("CRC Count: "+cc);
System.out.println("Marker: "+mark);
System.out.println("Payload Type: "+pType);
System.out.println("Sequence Number: "+sequence);
System.out.println("Timestamp: "+time);
System.out.println("SSrc: "+ssrc);
System.out.println(version+" "+padd+" "+ext+" "+cc+" "+mark+" "+" "+pType+" "+sequence);
System.out.println(time);
System.out.println(ssrc);
System.out.println("Hello people"+Server.i);
}
public static int unsigned_int(int nb)
{
if (nb >= 0)
return(nb);
else
return(256+nb);
}
}
Client.java
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Client
{
DatagramSocket cs;
InetAddress add;
DatagramPacket dph,dpp;
static int packet=10;
static int seq=100;
static int timestamp=500;
public void sendPacket(byte []header,byte []payload) throws Exception
{
cs=new DatagramSocket();
add=InetAddress.getLocalHost();
//add=InetAddress.getByName("192.168.1.30");
dph=new DatagramPacket(header,header.length,add,10000);
dpp=new DatagramPacket(payload,payload.length,add,10000);
/* packet++;
seq++;
timestamp++;*/
cs.send(dph);
cs.send(dpp);
}
public static void main(String args[]) throws Exception
{
Runnable ob=new RtpHeader1();
Thread t[]=new Thread[10];
Client ob1=new Client();
for(int i=0;i<10;i++)
{
t[i]=new Thread(ob);
t[i].start();
//t[i].join();
//ob1.sendPacket(t[i].header,t[i].payload);
}
}
}
class RtpHeader1 implements Runnable
{
int version;
int padd;
int mark;
int ext;
int cc;
int pType;
int sequence;
int time;
int ssrc;
int header_size=12;
public byte header[];
public int payload_size;
public byte[] payload;
public void makeRtpPacket(int pType,int frameNo,int time,byte []data,int data_length)
{
version=2;
padd=mark=ext=cc=ssrc=0;
sequence=frameNo;
this.time=time;
System.out.println(pType+"\t"+frameNo+"\t"+time);
this.pType=pType;
header=new byte[header_size];
header[0] = (byte)(version << 6);
header[0] = (byte)(header[0] | padd << 5);
header[0] = (byte)(header[0] | ext << 4);
header[0] = (byte)(header[0] | cc);
header[1] = (byte)(header[1] | mark << 7);
header[1] = (byte)(header[1] | this.pType);
header[2] = (byte)(sequence >> 8);
header[3] = (byte)(sequence & 0xFF);
header[4] = (byte)(this.time >> 24);
header[5] = (byte)(this.time >> 16);
header[6] = (byte)(this.time >> 8);
header[7] = (byte)(this.time & 0xFF);
header[8] = (byte)(ssrc >> 24);
header[9] = (byte)(ssrc >> 16);
header[10] = (byte)(ssrc >> 8);
header[11] = (byte)(ssrc & 0xFF);
payload_size = data_length;
payload = new byte[data_length];
for (int i = 0; i < data_length; i++)
payload[i] = data[i];
}
public byte[] getData()
{
Scanner sc=new Scanner(System.in);
// System.out.println("Enter 3 data");
byte data[]=new byte[3];
int i=0;
while(i<3)
data[i++]=1;//sc.nextByte();
return data;
}
public void run()
{
try
{
byte data[]=new byte[3];
data=getData();
makeRtpPacket(Client.packet++,Client.seq++,Client.timestamp++,data,data.length);
new Client().sendPacket(header, payload);
}catch(Exception e){}
}
}
運行 運行Server.java
其次Client.java
Preferrably使用NetBeans
該代碼運行在一臺機器上,所以不應該有任何數據包丟失 – Shashank