Impossible d'initialiser un socket UDP
Salut à tous
J'ai un programme en C qui envoie un tableau d'entier via une liaison UDP. De l'autre coté j'essaye d'écrire un thread qui reçois ces données. Lorsque j'initialise mon socket j'ai l'exception suivante:
Code:
1 2 3 4 5 6 7 8 9
|
java.net.BindException: Address already in use: Cannot bind
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.PlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at communication.AcqCourbe.run(AcqCourbe.java:31) |
Voici le thread Java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
public class AcqCourbe extends Thread {
private DatagramSocket m_UDPSocket = null;
private PageCameras m_PgCamera = null;
private boolean m_InitOK=false;
public AcqCourbe(PageCameras Pg) {
m_PgCamera = Pg;
}
public void run() {
byte[] buff = new byte[4 * 440];
DatagramPacket packet = new DatagramPacket(buff, buff.length);
try {
m_UDPSocket = new DatagramSocket(35001);//<-Exception
m_UDPSocket.setSoTimeout(200);
m_InitOK=true;
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (m_InitOK) {
try {
m_UDPSocket.receive(packet);
ByteBuffer buf = ByteBuffer.wrap(packet.getData());
buf.order(ByteOrder.LITTLE_ENDIAN);
IntBuffer val = buf.asIntBuffer();
int nb = val.capacity();
int recv[] = new int[nb];
for (int i = 0; i < nb; recv[i] = val.get(i), i++);
m_PgCamera.AfficheCourbe(1, nb, recv);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} |
Voici le code C
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
DWORD WINAPI Simu(LPVOID Param)
{
#define BUFFSIZE 440
int nbcar,Err;
int n,ph;
int buffer[BUFFSIZE];
SOCKET sock;
SOCKADDR_IN elec={0};
struct hostent *hostinfo=NULL;
int Telec=sizeof(elec);
hostinfo=gethostbyname("localhost");
elec.sin_addr=*(IN_ADDR*) hostinfo->h_addr;
elec.sin_port=htons(35001);
elec.sin_family=AF_INET;
sock=socket(AF_INET,SOCK_DGRAM,0);
bind(sock,(struct sockaddr*)&elec,Telec);
ph=0;
while(sock!=INVALID_SOCKET)
{
for(n=0;n<BUFFSIZE;n++)
{
double x=100*sin(3.1415926535897/50*(double)(n+ph));
buffer[n]=(int)x;
}
ph++;
if (ph>100)
ph=0;
nbcar=sendto(sock,(char*)buffer,BUFFSIZE*sizeof(int),0,(SOCKADDR*)&elec,Telec);
if(nbcar==-1)
{
Err=WSAGetLastError();
}
Sleep(50);
}
return 0;
} |
Normalement le code C est en exécution avant que le code Java ne s'exécute. Si on fait l'inverse ça marche mais dans mon cas ce n'est pas possible pour des raisons autres.
Si quelqu'un a une idée d'où vient le problème?