Professional Documents
Culture Documents
Java Sockets
Java Sockets
Source:
http://java.sun.com/docs/books/tutorial/networking/sockets/
Reading from and Writing to a
Socket in Java
try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}
Echo Client code, cont’d
• The three statements in the try block of the main method are
critical. These lines establish the socket connection between
the client and the server and open a PrintWriter and a
BufferedReader on the socket:
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
echoSocket = new Socket("taranis", 7);
• Open a socket.
• Open an input stream and output stream to the
socket.
• Read from and write to the stream according to
the server's protocol.
• Close the streams.
• Close the socket.
• Only step 3 differs from client to client, depending
on the server. The other steps remain largely the
same.
Writing a Datagram Client and
Server
try {
in = new BufferedReader(
new FileReader("one-
liners.txt"));
} catch (FileNotFoundException e)
System.err.println("Couldn't open
quote file. " + "Serving time instead.");
}
}
Ports
• A computer usually has one physical connection to the
network. So all the data from the the network comes
through this connection.
• If all the data comes through the same interface how
does the computer redirect to specific applications?
• Recall, with IP network addresses
– The computer is identified by its 32-bit IP address
– The port address is used to identify the specific program or
application to send the packet to.
Ports
• Port numbers range from 0 to 65,535 because ports are
represented by 16-bit numbers. The port numbers
ranging from 0 - 1023 are restricted. They are reserved
for use by well-known services such as HTTP and FTP
and other system services.
• Remember that certain ports are dedicated to well-
known services and you cannot use them. If you specify
a port that is in use, the creation of the
DatagramSocket will fail..
Constructor
• The constructor also opens a BufferedReader
on a file named oneliners.txt which contains a
list of quotes. Each quote in the file is on a line
by itself.
Run Method
socket.close();
The QuoteClient Class
• The QuoteClient class implements a client
application for the QuoteServer. This
application sends a request to the
QuoteServer, waits for the response, and,
when the response is received, displays it to
the standard output. Let's look at the code in
detail.
Local Variables
import java.net.*;
import java.io.*;
public class KKMultiServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
Main Server Skeleton
File: KKmultiServer.java (continued)
while (listening)
new KKMultiServerThread(serverSocket.accept()).start();
serverSocket.close();
}
}
JAVA Classes for Networking
Protocol : //java.sun.com
URL Class
• Key Constructors
URL(String urlText)
URL(URL baseURL, String urlText)
URL Class
• Key Constructors
Socket(String host, int port)
Socket(InetAddress ipNumber , int port)
• Key Public Methods
void close()
InetAddress getInetAddress()
InputStream getInputStream()
int getLocalPort()
OutputStream getOutputStream()
int get(Port()
Most common problem in Java -
Setting Environment