Lab-Java Network Programming Example
Lab-Java Network Programming Example
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;
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 {
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}