Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help needed with Network Programming

Reply
Thread Tools

Help needed with Network Programming

 
 
M Maloney
Guest
Posts: n/a
 
      04-23-2005
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
 
Reply With Quote
 
 
 
 
Peter MacMillan
Guest
Posts: n/a
 
      04-23-2005
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/...ramSocket.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.p...4&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:
 
Reply With Quote
 
 
 
 
M Maloney
Guest
Posts: n/a
 
      04-24-2005
> Sure, I just hacked together this example for you (1.5.0):
> http://www.nomorepasting.com/paste.p...4&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?
 
Reply With Quote
 
Peter MacMillan
Guest
Posts: n/a
 
      04-24-2005
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:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed on this 857W config. Repost to be clearer what the problemsare and the help needed sparticle Cisco 3 08-30-2007 07:47 PM
Computer Programming Website Help Needed Kathy A. Hertzog ASP .Net 3 04-02-2006 07:23 AM
socket Programming in java Help needed GMHK Java 4 09-01-2005 04:03 PM
HELP with Programming USB needed skill C Programming 3 08-21-2004 04:33 AM
Re: Socket programming urgent help needed Joona I Palaste C Programming 0 06-25-2003 09:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57