Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Sending and Receiving Via Tcp And Udp

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sending and Receiving Via Tcp And Udp

    Hi,

    I want to to implement Sending and Receiving data (messages) via socket connectionד (TCP and UDP).
    I want to use the same interface for the Sending and Receiving .
    I want to allocate all the necessary classes and variables at the initialization (and not at run time).
    I have some Q's:

    1. If I will define class like:
    public class CGeneric implements Serializable
    {
    }

    and:
    public class CTestMsgextends CGeneric
    {
    public int x;
    public int y;
    }

    1. if I send the messages via ObjectOutputStream.writeObject it will send more bytes than the size of the class CTestMsgextends ? (because it Serializable) ?

    2. Can you recommend me (another way, not via Serializable) on how to implement my needs ?

    Thanks


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Sending and Receiving Via Tcp And Udp

    Quote Originally Posted by amit1983 View Post
    I want to to implement Sending and Receiving data (messages) via socket connectionד (TCP and UDP).
    Ok, but remember that TCP and UDP communications are very different, both at technical level (different Java classes) and conceptual level (TCP establishes a (potentially long) communication while UDP handles single, distinct and (potentially) unrelated packets).

    Quote Originally Posted by amit1983 View Post
    I want to use the same interface for the Sending and Receiving .
    If you mean to maintain, for both TCP and UDP, the same data classes for objects to send/receive, it's surely possible.

    Quote Originally Posted by amit1983 View Post
    I want to allocate all the necessary classes and variables at the initialization (and not at run time).
    What do you exactly mean?

    Quote Originally Posted by amit1983 View Post
    1. if I send the messages via ObjectOutputStream.writeObject it will send more bytes than the size of the class CTestMsgextends ? (because it Serializable) ?
    Yes, serialization has its specific "protocol", that contains more informations than the single pure fields of an object to write.

    Quote Originally Posted by amit1983 View Post
    2. Can you recommend me (another way, not via Serializable) on how to implement my needs ?
    There's not an easy answer. It depends on your requirements. Are you developing both ends of the communication? (so you can choose/invent your standard)
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sending and Receiving Via Tcp And Udp

    Hi,
    Thank's for your answer.

    1. when I wrote: "I want to allocate all the necessary classes and variables at the initialization (and not at run time)." I meant that I cant (don't want to do something like:
    byte[] data = new byte[len];
    in the middle of the run time (I can allocate memory just in the start up (using pools))

    2. Just to be sure, if I will use serialization and send structure to an application which was written in c++, then the other application (in c++) will have problem to read the data (because it expect for structure with known fields, and not for a new message (serialize message),
    am I write ?

    3. If I have a structure which I need to send (via UDP or TCP),
    what is the best way too send it ? (and the other side may not be java application) ?


    Thank's again

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sending and Receiving Via Tcp And Udp

    Hi,
    Do you know the answers ?

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Sending and Receiving Via Tcp And Udp

    Quote Originally Posted by amit1983 View Post
    1. when I wrote: "I want to allocate all the necessary classes and variables at the initialization (and not at run time)." I meant that I cant (don't want to do something like:
    byte[] data = new byte[len];
    in the middle of the run time (I can allocate memory just in the start up (using pools))
    So you want to use and re-use one (or more) byte[] buffer(s), isn't it? If the size is sufficient for your datas, technically it is surely possible. If it has sense .... depends on what/how you want to send/receive.

    Quote Originally Posted by amit1983 View Post
    2. Just to be sure, if I will use serialization and send structure to an application which was written in c++, then the other application (in c++) will have problem to read the data (because it expect for structure with known fields, and not for a new message (serialize message),
    am I write ?
    Networking, in general, is based on well defined protocols and standards. Two systems that communicate to each other must "speak" the same protocol/standard. If you develop only one end and the other is already existent (or will be developed by other developers), you must know or agree the communication protocol.
    Without these premises we can sit here and argue until the next year.


    Quote Originally Posted by amit1983 View Post
    3. If I have a structure which I need to send (via UDP or TCP),
    what is the best way too send it ?
    There's no "best" way, said in general. It depends on (just a few points that come to mind):
    - how the data structure is made
    - which data types contains
    - what are the systems (and programming languages) involved
    - if the communication is unidirectional or bidirectional
    - if bidirectional, if it's synchronous or asynchronous
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

Similar Threads

  1. Replies: 2
    Last Post: July 9th, 2013, 06:01 PM
  2. Sending and Receiving File
    By beer-in-box in forum Java Networking
    Replies: 8
    Last Post: March 31st, 2013, 07:57 PM
  3. Try to Converting TCP to UDP
    By raniex in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2013, 12:05 PM
  4. Handling TCP and UDP in same server
    By ToshX in forum Java Networking
    Replies: 2
    Last Post: December 2nd, 2011, 02:19 PM
  5. TCP Over UDP
    By azarakhsh in forum Java Networking
    Replies: 3
    Last Post: July 15th, 2009, 07:21 AM