Java Reference
In-Depth Information
1. Socket setup: line 13
2. Send using text encoding: lines 18-24
3. Receive using binary encoding: lines 26-30
RecvTCP.java
0 import java.io.*; // for Input/OutputStream
1 import java.net.*; // for Socket and ServerSocket
2
3 public class RecvTCP {
4
5
public static void main(String args[]) throws Exception {
6
7
if (args.length != 1) // Test for correct # of args
8
throw new IllegalArgumentException("Parameter(s): <Port>");
9
10
int port = Integer.parseInt(args[0]);
// Receiving Port
11
12
ServerSocket servSock = new ServerSocket(port);
13
Socket clntSock = servSock.accept();
14
15
// Receive text−encoded quote
16
ItemQuoteDecoder decoder = new ItemQuoteDecoderText();
17
ItemQuote quote = decoder.decode(clntSock.getInputStream());
18
System.out.println("Received Text−Encoded Quote:");
19
System.out.println(quote);
20
21
// Repeat quote with binary encoding, adding 10 cents to the price
22
ItemQuoteEncoder encoder = new ItemQuoteEncoderBin();
23
quote.unitPrice += 10; // Add 10 cents to unit price
24
System.out.println("Sending (binary)...");
25
clntSock.getOutputStream().write(encoder.encode(quote));
26
27
clntSock.close();
28
servSock.close();
29
}
30 }
RecvTCP.java
1. Socket setup: line 12
2. Accept client connection: line 13
 
Search WWH ::




Custom Search