Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Java Network Programming

Example
import java.net.*; IP Access
import java.io.*;

public class ip
{
public static void main ( String[] args ) throws
IOException
{
String hostname;
Reader read = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(read);
System.out.print("Please Enter the URL: ");
hostname = input.readLine();
try IP Access
{
InetAddress ipaddress =
InetAddress.getByName(hostname);
System.out.println("IP address: " +
ipaddress.getHostAddress());
}
catch ( UnknownHostException e )
{
System.out.println("Could not find IP address for: " +
hostname);
}
}
}
import java.lang.*;
import java.io.*; Example 1- Server
import java.net.*;

class Server {
public static void main(String args[]) {
String data = "This is a test msg";
try {
ServerSocket srvr = new ServerSocket(1234);
System.out.print("Waiting for connection request...\n");
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
System.out.print("Sending string: '" + data + "'\n");
out.print(data);
out.close(); skt.close(); srvr.close();
}
catch(Exception e) {
System.out.print("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(skt.getInputStream()));
System.out.print("Received string: '");

while (!in.ready()) {}
System.out.println(in.readLine()); // Read one line and output it

System.out.print("'\n");
in.close();
}
catch(Exception e) {
System.out.print("Oops! It didn't work!\n");
}
}
} 5
import java.io.*;
import java.net.*; Example 2-Server
import java.util.Date;
public class DateServer {
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9090);
System.out.print("Socket opened and Server Running...");
try {
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
} finally {
socket.close();
}}}
finally {
listener.close();
}}}
import java.io.*; Example 2-Client
import java.net.Socket;
import javax.swing.JOptionPane;

public class DateClient {


public static void main(String[] args) throws IOException {
String serverAddress = JOptionPane.showInputDialog(
"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(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
System.exit(0);
}
}
import java.io.*;
Example 3-Server
import java.net.*;

class TCPServer {
public static void main(String argv[]) throws IOException
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
System.out.println("Server waiting for connection ...");
Example 3-Server
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Example 3-Client

import java.io.*;
import java.net.*;
class TCPClient {

public static void main(String argv[]) throws IOException


{
String sentence;
String modifiedSentence;
System.out.println("Enter a string...");
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Example 3-Client
Socket clientSocket = new Socket("127.0.0.1", 6789);
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}

You might also like