[Go to site: main page, start]

0% found this document useful (0 votes)
65 views11 pages

Java Network Programming Examples

This document provides examples of Java network programming including client-server applications. Example 1 shows a simple client-server application with a server sending a message to a client. Example 2 demonstrates a date server and client where the server sends the current date to any connecting clients. Example 3 illustrates a client sending a message to a server which capitalizes the message and returns it to the client.

Uploaded by

Aliyan Aman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views11 pages

Java Network Programming Examples

This document provides examples of Java network programming including client-server applications. Example 1 shows a simple client-server application with a server sending a message to a client. Example 2 demonstrates a date server and client where the server sends the current date to any connecting clients. Example 3 illustrates a client sending a message to a server which capitalizes the message and returns it to the client.

Uploaded by

Aliyan Aman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
  • Document Cover
  • IP Access Example
  • Example 1 - Basic Server and Client
  • Example 2 - Advanced Server and Client
  • Example 3 - TCP Communication

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]();
}
}

Java Network Programming 
Example
IP Access
import java.net.*;
import java.io.*;
public class ip 
{
  public static void main ( String[] args ) throws 
IOExcep
IP Access
try 
    {
    InetAddress ipaddress = 
InetAddress.getByName(hostname);
    System.out.println("IP address: " + 
i
Example 1- Server
4
import java.lang.*;
import java.io.*;
import java.net.*;
class Server {
   public static void main(String
Example 1- Client
5
class Client {
   public static void main(String args[]) {
      try {
         Socket skt = new Socket("
Example 2-Server
import java.io.*;
import java.net.*;
import java.util.Date;
public class DateServer {
    public static void
Example 2-Client
import java.io.*;
import java.net.Socket;
import javax.swing.JOptionPane;
public class DateClient {
   publi
Example 3-Server
import java.io.*; 
import java.net.*; 
class TCPServer { 
  public static void main(String argv[]) throws IO
Example 3-Server
while(true) { 
           Socket connectionSocket = welcomeSocket.accept(); 
           BufferedReader inFro
Example 3-Client
import java.io.*; 
import java.net.*; 
class TCPClient { 
    public static void main(String argv[]) throws

You might also like