Академический Документы
Профессиональный Документы
Культура Документы
Note from archiver<at>cs.uu.nl: This page is part of a big collection of Usenet postings,
archived here for your convenience. For matters concerning the content of this page,
please contact its author(s); use the source, if all else fails. For matters concerning the
archive as a whole, please refer to the archive description or contact the archiver.
Archive-name: computer-lang/java/network-programming
Posting-Frequency: monthly
Last-modified: October 6, 1999
Copyright: Copyright 1998, 1999 David Reilly. All Rights Reserved.
Maintainer: David Reilly (dodo@fan.net.au)
URL: http://www.davidreilly.com/java/java_network_programming/
------------------------------------------------------------
Table of contents
Overview
Legal
Comments
Books
Websites
1. Socket Questions
local port?
1.7 What are socket options, and why should I use them?
1.8 When my client connects to my server, why does no data
come out?
2. HTTP Questions
6. Servlets
------------------------------------------------------------
Overview
3
Legal
Comments
Books
[more information]
http://www.davidreilly.com/goto.cgi?isbn=188477749X
[more information]
http://www.davidreilly.com/goto.cgi?isbn=1565922271
4
http://java.sun.com/docs/books/tutorial/
Websites
Site : JavaWorld
URL : http://www.javaworld.com/
JavaWorld is an online magazine, published monthly.
Covers a wide variety of topics, some of which are
networking related.
------------------------------------------------------------
1. Socket questions
InetAddress inet =
InetAddress.getByName("www.davidreilly.com");
System.out.println ("IP : " + inet.getHostAddress());
1.5 How can I find out the current IP address for my machine?
// Print address
System.out.println ("Local IP : " + local.getHostAddress());
port?
1.7 What are socket options, and why should I use them?
If you're writing a TCP service, then you can telnet to the port
the server uses, and check to see if it is responding to data. If
so, then the fault is more than likely in the client, and if not,
you've found your problem. A debugger can be very helpful in
tracking down the precise location of server errors. You could
try jdb, which comes with JDK, or use an IDE's debugger like
Visual J++ or Borland JBuilder.
as the buffer will flush sometimes (when it becomes full) but not
other times.
2. HTTP Questions
The following code snippet shows you how this can be done.
The show page method is capable of displaying any URL passed
to it.
import java.net.*;
import java.awt.*;
import java.applet.*;
// Show me a page
public void showPage ( String mypage )
{
URL myurl = null;
// Show URL
if (myurl != null)
{
getAppletContext().showDocument (myurl);
}
}
}
For example,
import java.net.*;
import java.io.*;
CGI scripts. Let's take a look at how URL encoding works first
though.
http://www.yourwebhost.com/yourcgi.cgi?name=your%20name&emai
l=email@email.com
than that from which they were loaded. Like socket connections, HTTP
connections will cause security exceptions to be thrown. If
you absolutely, positively, have to access other hosts (and
replacing your applet with a Java servlet is impractical),
consider using a digitally signed applet.
You'll also need to create a policy file (if one does not
already exist). Here's a sample policy file that will allow
you to accept conections from ports higher than 1024, but
connect to all ports as a client.
grant {
permission java.net.SocketPermission "*:1024-65535",
"connect,accept,resolve";
permission java.net.SocketPermission "*:1-1023",
"connect,resolve";
};
6. Java Servlets
Reading cookies from a servlet is quite easy. You can gain access
to any cookies sent by the browser from the
javax.servlet.http.HttpServletRequest passed to the servlet's
doGet, doPost, etc methods. HttpServletResponse offers a method,
Cookies[] getCookies() which returns an array of Cookie objects.
However, if no cookies are available, this value may be null, so be
sure to check before accessing any array elements.
eg http://webserver/servlet/servletname
Once you've set the path, any script (for example, stored in
/cgi-bin/) can access the cookies stored by your servlets.
6.8 How can I void a cookie, and delete it from the browser?
------------------------------------------------------------
Copyright 1998, 1999 David Reilly. All Rights Reserved.