0

I find the jsp code for send socket, and change the IP to 255.255.255.255, but the result is it says: 'Network is unreachable'.

The code is:

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.util.*,java.net.*,java.io.*,java.lang.*" errorPage="" %>

var errors="";
<%
    try{

        int character;
        Socket socket = new Socket("255.255.255.255", 10000);

        InputStream inSocket = socket.getInputStream();
        OutputStream outSocket = socket.getOutputStream();

        String str = "Message";
        byte buffer[] = str.getBytes();
        outSocket.write(buffer);

        while ((character = inSocket.read()) != -1) {

           out.print((char) character);
        }

        socket.close();

    }
    catch(java.lang.Exception e){
%>
        errors="Something is wrong!"+ "<%= e.getMessage() %>";

<%
    }

My tomcat is version 6( I guess...),

Or if it's impossible( I heard people say jsp is different from java, it only use for presentation), can I write a java class( using DatagramSocket) to show the receiving data and let my jsp import the class and get the data?

Any advice will be appreciated.

Shuinvy
  • 251
  • 2
  • 6
  • 23
  • possible duplicate of [Broadcasting UDP packet to 255.255.255.255](http://stackoverflow.com/questions/19771485/broadcasting-udp-packet-to-255-255-255-255) – Raedwald Jul 25 '14 at 09:16
  • Possible duplicate of http://stackoverflow.com/questions/6579350/sending-packets-to-255-255-255-255-by-java-datagramsocket-fails – Raedwald Jul 25 '14 at 09:18
  • I do have seen the two link before I ask question, and it cannot help me. Or anything is wrong in my code? – Shuinvy Jul 25 '14 at 09:41
  • ***Everything*** is wrong with your code. You're trying to connect a TCP socket to a UDP broadcast address. It doesn't begin to make sense. JSP has nothing to do with it. Why would you want to send a broadcast from a JSP page anyway? What's the problem you're trying to solve? – user207421 Jul 25 '14 at 10:08

2 Answers2

0

You should use MulticastSocket. Here is an example:

public void send(String message) throws IOException {

    MulticastSocket s = new MulticastSocket();

    byte[] msg = message.getBytes();

    DatagramPacket pack = new DatagramPacket(msg, msg.length, InetAddress.getByName("228.5.6.7"), 12345);

    s.send(pack);

    s.close();

}

void recieve() throws IOException {
    MulticastSocket s = new MulticastSocket(12345);

    InetAddress address = InetAddress.getByName("228.5.6.7");
    s.joinGroup(address);

    byte[] buf = new byte[1024];
    DatagramPacket pack = new DatagramPacket(buf, buf.length);
    s.receive(pack);

    String data = new String(pack.getData());

    System.out.println(data);

    s.leaveGroup(address);
    s.close();
}
alex
  • 8,904
  • 6
  • 49
  • 75
  • You can't join 255.255.255.255. It's not a multicast address. – user207421 Jul 25 '14 at 10:07
  • @EJP it was just example – alex Jul 25 '14 at 11:00
  • A wrong example. Now it's arguable that you haven't answered the question, as he states he wants to broadcast. And a receive() method that only opens the socket for the duration of its call is likely to miss more data than it catches. – user207421 Jul 25 '14 at 11:22
0

You say you have seen the links, but I think you did not read carefully this one or at least you have not understood it.

I will try to explain : when you create a socket by new Socket(...) you create a TCP socket. TCP is a connected protocol and you cannot broadcast via TCP

So fix your code by properly using UDP socket (DatagramSocket), ensure you enable broadcast by socket.setBroadcast(true); and if it still does not work give details on remaining problems.

I give no code here since all relevant code is allready in referenced post.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252