Java Network Programming
Example
import [Link].*; IP Access
import [Link].*;
public class ip
{
public static void main ( String[] args ) throws
IOException
{
String hostname;
Reader read = new InputStreamReader([Link]);
BufferedReader input = new BufferedReader(read);
[Link]("Please Enter the URL: ");
hostname = [Link]();
try IP Access
{
InetAddress ipaddress =
[Link](hostname);
[Link]("IP address: " +
[Link]());
}
catch ( UnknownHostException e )
{
[Link]("Could not find IP address for: " +
hostname);
}
}
}
import [Link].*;
import [Link].*; Example 1- Server
import [Link].*;
class Server {
public static void main(String args[]) {
String data = "This is a test msg";
try {
ServerSocket srvr = new ServerSocket(1234);
[Link]("Waiting for connection request...\n");
Socket skt = [Link]();
[Link]("Server has connected!\n");
PrintWriter out = new PrintWriter([Link](), true);
[Link]("Sending string: '" + data + "'\n");
[Link](data);
[Link](); [Link](); [Link]();
}
catch(Exception e) {
[Link]("Oops! It didn't work!\n");
}
}} 4
class Client {
public static void main(String args[]) {
Example 1- Client
try {
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
[Link]("Received string: '");
while (![Link]()) {}
[Link]([Link]()); // Read one line and output it
[Link]("'\n");
[Link]();
}
catch(Exception e) {
[Link]("Oops! It didn't work!\n");
}
}
} 5
import [Link].*;
import [Link].*; Example 2-Server
import [Link];
public class DateServer {
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9090);
[Link]("Socket opened and Server Running...");
try {
while (true) {
Socket socket = [Link]();
try {
PrintWriter out =
new PrintWriter([Link](), true);
[Link](new Date().toString());
} finally {
[Link]();
}}}
finally {
[Link]();
}}}
import [Link].*; Example 2-Client
import [Link];
import [Link];
public class DateClient {
public static void main(String[] args) throws IOException {
String serverAddress = [Link](
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
Socket s = new Socket(serverAddress, 9090);
BufferedReader input =
new BufferedReader(new
InputStreamReader([Link]()));
String answer = [Link]();
[Link](null, answer);
[Link](0);
}
}
import [Link].*;
Example 3-Server
import [Link].*;
class TCPServer {
public static void main(String argv[]) throws IOException
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
[Link]("Server waiting for connection ...");
Example 3-Server
while(true) {
Socket connectionSocket = [Link]();
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader([Link]()));
DataOutputStream outToClient =
new DataOutputStream([Link]());
clientSentence = [Link]();
capitalizedSentence = [Link]() + '\n';
[Link](capitalizedSentence);
}
}
}
Example 3-Client
import [Link].*;
import [Link].*;
class TCPClient {
public static void main(String argv[]) throws IOException
{
String sentence;
String modifiedSentence;
[Link]("Enter a string...");
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader([Link]));
Example 3-Client
Socket clientSocket = new Socket("[Link]", 6789);
DataOutputStream outToServer =
new DataOutputStream([Link]());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader([Link]()));
sentence = [Link]();
[Link](sentence + '\n');
modifiedSentence = [Link]();
[Link]("FROM SERVER: " + modifiedSentence);
[Link]();
}
}