Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java socket
when i use this code , the sender successfully send the message byt the receiver can't receive this message
package UDP_Socket;
 
import java.net.*;
import java.io.*;
 
public class PacketReceiver {
    public static void main(String[] args) {
        try {
            
              
// Create a datagram socket, bound to the specific port 9999
            DatagramSocket socket = new DatagramSocket(9999);
// Create a datagram packet, containing a maximum buffer of 256 bytes
            DatagramPacket packet = new DatagramPacket(new byte[256], 256);
// Receive a packet - remember by default this is a blocking operation
            //socket.setSoTimeout(10000);
            socket.receive(packet);
            System.out.println("Packet received!");
// Display packet information
            InetAddress remote_addr = packet.getAddress();
            System.out.println("Sent by host: " + remote_addr.getHostAddress());
            System.out.println("Sent from port: " + packet.getPort());
// Display packet contents, by reading from byte array
            ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData());
// Display only up to the length of the original UDP packet
            for (int i = 0; i < packet.getLength(); i++) {
                int data = bin.read();
                if (data == -1)
                    break;
                else
                    System.out.print((char)data);
            }
            socket.close();
        } catch (IOException ioe) {
            System.err.println("Error - " + ioe);
        }
    }
}
 

package UDP_Socket;
 
import java.net.*;
import java.io.*;
 
public class PacketSender {
    public static void main(String[] args) {
 
        try {
// Hostname/IP address of the remote server  
            String hostname = "localhost";
// Lookup the specified hostname, and get an InetAddress
            System.out.println("Looking up hostname " + hostname);
            InetAddress remote_addr = InetAddress.getByName(hostname);
            System.out.println("Hostname resolved as " + remote_addr.getHostAddress());
// Create a datagram socket, bound to any available local port
            DatagramSocket socket = new DatagramSocket();
// Create a message to send using a UDP packet
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            PrintStream pout = new PrintStream(bout);
            pout.print("Greetings!");
// Get the contents of our message as an array of bytes
            byte[] barray = bout.toByteArray();
// Create a datagram packet, containing our byte array
            DatagramPacket packet = new DatagramPacket(barray, barray.length);
// Set the destination address of the packet 
            packet.setAddress(remote_addr);
// Set port number to 9999
            packet.setPort(9999);
// Send the packet - remember no guarantee of delivery
            socket.send(packet);
            System.out.println("Packet sent!");
            socket.close();
        } catch (UnknownHostException uhe) {
            System.err.println("Can't find host " + uhe.getMessage());
        } catch (IOException ioe) {
            System.err.println("Error - " + ioe);
        }
    }
}
Posted 29-Nov-11 15:37pm
Edited 29-Nov-11 15:42pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First of all you should try another socket. the sockets 0-10000 are often used by different applications, it's much saver to use a higher value here (and please not again 33333 or 22222 and... you know which...)
 
Also can I recommend to read This Java Tutorial on Datagrams[^].
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 275
1 OriginalGriff 264
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,048
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 30 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100