Skip to main content

Posts

Showing posts with the label Java Networking

Using SSL in java program

import java.net.*; import java.security.cert.Certificate; import java.io.*; import javax.net.ssl.*; public class SSLTest { public static voi d main(String[] args) throws Exception { String https_url = " https://www.google.com/ " ; URL url; url = new URL(https_url); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); if (con != null ) { System.out.println( " Response Code : " + con.getResponseCode()); System.out.println( " Cipher Suite : " + con.getCipherSuite()); System.out.println( " \n " ); Certificate[] certs = con.getServerCertificates(); int i=0; for (Certificate cert : certs) { System.out.println( " Cert NO : " +i++); System.out.println( " Cert Type : " + cert.getType()); System.out.printl...

Client and server communication program

Here is an example, demonstrating how to connect two programs in Java networking package network; import java.io.*; import java.net.*; import java.util.*; public class Server1{ public static void main(String ar[]) throws Exception{ ServerSocket ss= new ServerSocket(1234); Socket clientSocket=ss.accept(); InputStream in=clientSocket.getInputStream(); OutputStream out=clientSocket.getOutputStream(); Scanner scan= new Scanner(in); PrintStream ps= new PrintStream(out); while (true){ String s=scan.nextLine(); System.out.println( "Client sent : " +s); String str=System.console().readLine( "Entr Something : " ); ps.println(str); } } } package network; import java.io.*; import java.net.*; import java.util.*; public class Client1{ public static void main(String ar[]) throws Exception{ Sock...

Java networking : Sending and Receiving file

We can transfer file from one system to another sysem useing java program. here is an example where one program will read file from local machine and sends it to receiving program. Other receiving program copy all contents reciverd from remote progam and writes it to file. FileSender class is working as client and FileReceiver class working as server. Here , 192.168.0.1 is address of machine where FileReceiver program is running and it can be changed accourding to your machine. 1234 is port number at which server program is listening for client reque FileSender.java package network; import java.io.*; import java.net.*; import java.util.*; public class FileSender{ public static void main(String ar[]) throws Exception{ Socket clientSocket= new Socket( "192.168.0.2" ,1234); //InputStream in=clientSocket.getInputStream(); OutputStream out=clientSocket.getOutputStream(); //PrintStream ps=new PrintStream(out); ...

Client Server program in java

Java has many options to create distributed applications. One of them is Networking. java.net package provides platform to create distributed applications in easy way. Here, I am giving simple client server example to demonstrate java networking. Server program : package net; import java.net.*; import java.io.*; class ServerDemo1{ public static void main(String ar[])throws Exception{ ServerSocket ss=new ServerSocket(1234); Socket clientSocket=ss.accept(); InputStream in=clientSocket.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(in)); OutputStream out=clientSocket.getOutputStream(); while(true){ String m=br.readLine(); //if(m!=null && !(m.length()<1)){ System.out.println("Data Recieved From Client \n"+m); //} out.write(m.toUpperCase().getBytes()); out.write(13); out.write(10); out.flush(); } } } Client Program : package net; import java.net.*; import java.io.*; public class ClientDemo1{ public static void main(String ar[])throws E...

Submit data fields using java program.

In java networking you can fill the form program and send data fields to server. server will treat them as usual. Only you need to get URLConnection object from URL object and write data fields to OutputStream got through URLConnection Object. Example : package net; import java.net.*; import java.io.*; //sending form data public class Net1{ public static void main(String ar[])throws Exception{ URL url=new URL("http://localhost:8080/networking/serv1"); URLConnection con=url.openConnection(); con.setDoOutput(true); OutputStreamWriter out=new OutputStreamWriter(con.getOutputStream()); out.write("t1=abc"); out.write("&t2=123"); out.close(); InputStream in= con.getInputStream(); while(true){ int x=in.read(); if(x==-1)break; System.out.print((char)x); } } }