Network Programming Basic
Network Programming Basic
Java implements both TCP/IP sockets and datagram sockets (UDP sockets).
Very often, the two communicating processes will have a client/server
relationship.
The steps required creating client/ server programs via each of these methods
are very similar and are outlined in the following two sub-sections.
TCP Sockets-Server
For example:
Scanner input = new Scanner(link.getInputStream());
We simply use method nextLine for receiving data and method println for
sending data, just as we might do for console I/O. For example:
output.println("Awaiting data...");
String input = input.nextLine();
5) Close the connection (after completion of the dialogue).
import java.io.*;
import java.net.*;
import java.util.*;
try
{
link = servSock.accept(); //Step 2.
int numMessages = 0;
String message = input.nextLine(); //Step 4.
while (!message.equals("***CLOSE***"))
{
System.out.println("Message received.");
numMessages++;
output.println("Message " + numMessages
+ ": " + message); //Step 4.
message = input.nextLine();
}
output.println(numMessages
+ " messages received.");//Step 4.
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
System.out.println(
"\n* Closing connection... *");
link.close(); //Step 5.
}
catch(IOException ioEx)
{
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}
TCP-Client
1) Establish a connection to the server.
We create a Socket object, supplying its constructor with the following two arguments:
the server’s IP address (of type InetAddress);
the appropriate port number for the service.
These are set up in exactly the same way as the server streams were set up (by call-
ing methods getInputStream and getOutputStream of the Socket object that was created
in step 2).
The Scanner object at the client end will receive messages sent by the PrintWriter object
at the server end, while the PrintWriter object at the client end will send messages that are
received by the Scanner object at the server end (using methods next- Line and println
respectively).
4) Close the connection.
The same way to Server Program
Example
import java.io.*;
import java.net.*;
import java.util.*;
try {
link = new Socket(host, PORT); //Step 1.
PrintWriter output =
new PrintWriter(
link.getOutputStream(), true);//Step 2.
do {
buffer = new byte[256]; //Step 2.
inPacket =
new DatagramPacket(
buffer, buffer.length); //Step 3.
datagramSocket.receive(inPacket); //Step 4.
InetAddress clientAddress =
inPacket.getAddress(); //Step 5.
int clientPort =
inPacket.getPort(); //Step 5.
messageIn =
new String(inPacket.getData(), 0,
inPacket.getLength()); //Step 6.
System.out.println("Message received.");
numMessages++;
messageOut = "Message " + numMessages
+ ": " + messageIn;
outPacket = new DatagramPacket(
messageOut.getBytes(),
messageOut.length(),
clientAddress,
clientPort); //Step 7.
datagramSocket.send(outPacket); //Step 8.
} while (true);
} catch (IOException ioEx) {
ioEx.printStackTrace();
} finally //If exception thrown, close connection.
{
System.out.println("\n* Closing connection... *");
datagramSocket.close(); //Step 9.
}
}
}
UDP CLIENT PROGRAM
datagramSocket.send(outPacket);
Steps 4-6 below are exactly the same as steps 2-4 of the server procedure.
4. Create a buffer for incoming datagrams.
For example:
byte[] buffer = new byte[256];