Help needed with Network Programming

M

M Maloney

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
 
P

Peter MacMillan

M said:
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.
 
M

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.

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?
 
P

Peter MacMillan

M said:
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 ;).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top