Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2011
    Posts
    23
    Rep Power
    0

    Lightbulb Unreadable socket data

    I write a Server socket and try to read request from a software that request a license on that server on specific port.

    But the data is unreadable format. How can i interpret it. Is there any way to read that?

    OUTPUT:
    Java Code:
    ..................................................
    Address : 132.23.13.151
    Data in Bytes : [B@19821f
    Data :                       $; p  
         +? J                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    Port : 52346
    ..................................................
    ..................................................
    Address : 132.23.13.151
    Data in Bytes : [B@19821f
    Data :                      $; p  
         +? J                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    Port : 52346
    ..................................................
    Code for server socket is below and running on 1500 port.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class UDPServer1 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            byte data[] = new byte[1024];
            String msg = null;
    	  int port;
            String msg1 = null;
    
            
            try{
                DatagramSocket server = new DatagramSocket(Integer.parseInt(args[0]));
                
                while(true)
                {
                   DatagramPacket receive = new DatagramPacket(data, data.length);
                   server.receive(receive);
                   System.out.println("..................................................");
                   System.out.println("Address : "+receive.getAddress().getHostAddress());
    		   System.out.println("Data in Bytes : "+receive.getData());
    		   msg = new String(receive.getData());
    		   msg = msg.toString();
                   System.out.println("Data : "+msg);
                   System.out.println("Port : "+receive.getPort());
                   System.out.println("..................................................");
              }
                
                
                
            }catch(IOException e)
            {
                System.out.print(e);
            }
            
            
            
        }
    }

    Please help me...Thanks in advance

  2. #2
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Unreadable socket data

    most probably you have a problem with encoding in language difference

    Java uses UTF-16 as default.

    But a computer can use different encoding.

    If you know your encoding system, then Look example

    String text=new String(data,0,length,Charset.forName("sended encoded language code"));

    Charset.forName("Window-1254"); //My machines encoding in text , I am not sure about exact name of my encoding system("Window-1254"?) :)

    and you can see the encoding systems that is supported, by this method ->Charset.availableCharsets();

    Hint: you can see your encoding system in OS -> String myEncoding = System.getProperty("sun.jnu.encoding"); //I use this method to get my machine text encoding system

    I am not sure what is sun.jnu.encoding but most probably java keeps owners encoding system in that property.

    //Notification :>you used UDP so you have take precautions about message loss

Similar Threads

  1. how to handle data over two distinct socket connection
    By krishforever in forum New To Java
    Replies: 7
    Last Post: 02-03-2011, 11:57 AM
  2. Socket, streams and data types?
    By Tomtefaen in forum Networking
    Replies: 0
    Last Post: 01-15-2011, 12:36 AM
  3. [Socket] Send audio data
    By Dennis in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2010, 08:31 PM
  4. pls..transfer data in socket
    By langkhach_codon in forum Networking
    Replies: 7
    Last Post: 03-28-2010, 06:21 AM
  5. Replies: 3
    Last Post: 07-16-2007, 12:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •