I've a server in Java which listens the request on the UDP socket port 100 and puts it's response on the UDP socket port 200.
Now I need to write a client in PHP which should put it's request on the port 100 and should receive the response on 200.
My program is able to send the request to the server on the port 100 and the server is also putting it's response on the port 200. But my PHP program is not receiving the response. It is going into a waiting state.
Interesting part is, if I send response on to the same port on which the request came, my PHP code is able to receive. But the server design is to use two ports. One incoming and one outgoing.
Here is my PHP code
<?php
class SocketHandle {
public function requestService($message) {
// Choose proper domain
$domain = (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? AF_INET : AF_UNIX);
$socketHandle = socket_create($domain, SOCK_DGRAM, SOL_UDP);
$serverIP = "127.0.0.1";
$serverSendPort = 100;
$serverRecvPort = 200;
socket_sendto($socketHandle, $message, strlen($message), 0, $serverIP, $serverSendPort);
$response = "";
socket_recvfrom($socketHandle, $response, strlen($message), 0, $serverIP, $serverRecvPort开发者_StackOverflow中文版);
return $response;
}
}
?>
and my Java server code is
class UDPServer {
public static void main(java.lang.String args[]) throws java.lang.Exception{
try{
java.net.DatagramSocket serverSocket = new java.net.DatagramSocket(100);
java.net.DatagramSocket serverSocket200 = new java.net.DatagramSocket(200);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true){
receiveData = new byte[1024];
java.net.DatagramPacket receivePacket = new java.net.DatagramPacket(receiveData, receiveData.length);
java.lang.System.out.println ("Waiting for datagram packet");
serverSocket.receive(receivePacket);
java.lang.String sentence = new java.lang.String(receivePacket.getData()).trim();
java.net.InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
java.lang.System.out.println ("From: " + IPAddress + ":" + port);
java.lang.System.out.println ("Message: " + sentence);
java.lang.String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
//java.net.DatagramPacket sendPacket = new java.net.DatagramPacket(sendData, sendData.length, IPAddress, port);
java.net.DatagramPacket sendPacket = new java.net.DatagramPacket(sendData, sendData.length, IPAddress, 200);
java.lang.System.out.println("Sending : " + capitalizedSentence);
//serverSocket.send(sendPacket);
serverSocket200.send(sendPacket);
}
}catch (java.net.SocketException ex) {
java.lang.System.out.println("UDP Port 100 / 200 is occupied.");
java.lang.System.exit(1);
}
}
}
Here is the output of my Java server
>java UDPServer
Waiting for datagram packet
From: /127.0.0.1:54491
Message: Hello
Sending : HELLO
Waiting for datagram packet
The socket_recvfrom
port parameter sets the remote port that the reply needs to come from, so don't pass anything here. The listening port is set when the socket is made, and thus it is listening on port 100.
In Linux, it is not listening on ports below 1024. If you use a port above 1024, it will work.
精彩评论