Help needed with Network Programming

Discussion in 'Java' started by M Maloney, Apr 23, 2005.

  1. M Maloney

    M Maloney Guest

    Hey everyone,
    I am relatively new to java network programming, and I need a
    bit of help with this task I am doing to help me learn network
    programming.
    To start off with I am writing some simple programs. All I want
    to do to begin is implement a client and iterative server using UDP.
    The client asks for time of day, and the server should reply with time
    of day - then both terminate connections and clean up.
    I am having troubles getting started - I know I have to use
    DatagramSocket, and DatagamPacket - but how do I connect the client to
    the server and which host/port do I use? I don't want to connect to an
    internet server, just the server I create. Also, how can I send
    packets to and from server and client - as in how do I know who I'm
    sending to?

    If anyone could show me how its done, I would be very grateful.

    Thanks for your time and help, M.Maloney
    M Maloney, Apr 23, 2005
    #1
    1. Advertising

  2. M Maloney wrote:
    > Hey everyone,
    > I am relatively new to java network programming, and I need a
    > bit of help with this task I am doing to help me learn network
    > programming.
    > To start off with I am writing some simple programs. All I want
    > to do to begin is implement a client and iterative server using UDP.
    > The client asks for time of day, and the server should reply with time
    > of day - then both terminate connections and clean up.
    > I am having troubles getting started - I know I have to use
    > DatagramSocket, and DatagamPacket - but how do I connect the client to
    > the server and which host/port do I use?


    The port you choose is (mostly) arbitrary (on some systems, you have to
    be a super-user to bind to ports <1024). For the socket, I highly
    recommend that you familarize yourself with the API.

    http://java.sun.com/j2se/1.5.0/docs/api/java/net/DatagramSocket.html


    >
    > If anyone could show me how its done, I would be very grateful.
    >
    > Thanks for your time and help, M.Maloney



    Sure, I just hacked together this example for you (1.5.0):
    http://www.nomorepasting.com/paste.php?pasteID=38194&noLineNums=1

    The actual data transfer is a bit funny (I turn the date into a sequence
    of UTF-8 bytes on the servr and then back into a string on the client).
    Perhaps not the best way, certainly not the only way, but it shows how
    you have to convert to a byte[] array.

    --
    Peter MacMillan
    e-mail/msn:
    Peter MacMillan, Apr 23, 2005
    #2
    1. Advertising

  3. M Maloney

    M Maloney Guest

    > Sure, I just hacked together this example for you (1.5.0):
    > http://www.nomorepasting.com/paste.php?pasteID=38194&noLineNums=1
    >
    > The actual data transfer is a bit funny (I turn the date into a sequence
    > of UTF-8 bytes on the servr and then back into a string on the client).
    > Perhaps not the best way, certainly not the only way, but it shows how
    > you have to convert to a byte[] array.


    Thanks for your help. So you need to put a socket in the client and
    sever? And if I wanted to connect to a server on the net I'd just use
    the Inet.getAddressByName(...) method for the particular site on both
    client and server?
    M Maloney, Apr 24, 2005
    #3
  4. M Maloney wrote:
    > Thanks for your help. So you need to put a socket in the client and
    > sever? And if I wanted to connect to a server on the net I'd just use
    > the Inet.getAddressByName(...) method for the particular site on both
    > client and server?


    You only need the getAddressByName functionality on the client since,
    once the server gets the data, some of the included information is the
    address of the client.

    The server opens a UDP socket and listens for data. The code s = new
    DatagramSocket(9800); says to create a socket on port 9800. UDP is
    connectionless - meaning that you don't have to establish a connection;
    you just send data or listen on the port for data to arrive. So when we
    write s.receive(p); on the server, it blocks and waits for data to arrive.

    The client needs to know two things: the server ip and the port. The
    first thing we do is get a socket. Since the client can tell the server
    what port it is using, we can just accept whatever default port we get
    (depends on socket implementation, essentially random). We then call
    connect with an ip address and use the port number that the server is
    listening on. Once we have a "connection" (that is, our datagramsocket
    knows where to send the data), we can send out our request (and wait for
    a response, if we like).

    I hope I haven't confused you with my explanation ;).

    --
    Peter MacMillan
    e-mail/msn:
    Peter MacMillan, Apr 24, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kal
    Replies:
    1
    Views:
    9,502
    Kevin Spencer
    Jun 21, 2004
  2. Jane Davis

    Network Service account over network

    Jane Davis, Jun 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    425
    Kevin Spencer
    Jun 22, 2005
  3. Bill Volk
    Replies:
    1
    Views:
    3,129
    Bill Volk
    Jul 2, 2003
  4. jaialai technology
    Replies:
    4
    Views:
    767
    Sanjay
    Feb 7, 2007
  5. kin
    Replies:
    0
    Views:
    908
Loading...

Share This Page