简体   繁体   English

套接字PHP Java

[英]Sockets php java

I'm having problems with sockets, in fact I'm trying to send a socket from a php script to a java server: 我在使用套接字时遇到问题,实际上我正在尝试将套接字从php脚本发送到Java服务器:

Here is my php: 这是我的PHP:

<form action="#" method="Post">
    <input type="text" placeholder="Message" name="msg">
    <input type="submit" value="Send message">
</form>

<?php
if (isset($_POST['msg'])) {
    $address="127.0.0.1";
    $port="43278";
    $msg = $_POST['msg'];

    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

    socket_sendto($socket, $msg, strlen($msg), 0, $address, $port);

    echo "<p>Message sent</p>";
}

And here is my java server: 这是我的Java服务器:

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(43278);

    Socket clientSocket = serverSocket.accept();
    System.out.println("Socket received");

    serverSocket.close();
    clientSocket.close();
}

The java server does'nt receive anything, so I tried to send a socket via a java program and it works: Java服务器什么也没收到,因此我尝试通过Java程序发送套接字,并且它可以正常工作:

public static void main(String[] args) throws IOException {
    Socket socket;
    socket = new Socket(InetAddress.getLocalHost(), 43278);
    socket.close();
}

But I'm not sure if the problem is due to the php script because this software seems to receive the socket from php: 但是我不确定问题是否是由于php脚本引起的,因为此软件似乎从php接收了套接字:

图片

Finaly I think it's due to the ip address but I could'nt find informations about it so I'm not sure... 最后,我认为这是由于IP地址造成的,但是我找不到有关它的信息,所以我不确定...

Regards. 问候。

it worked, I used the UDPServer.java example. 它有效,我使用了UDPServer.java示例。 In fact I had to use a DatagramSocket instead of the ServerSocket. 实际上,我不得不使用DatagramSocket而不是ServerSocket。 – Florian Merle –弗洛里安·梅尔(Florian Merle)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM