Transaction TCP Aim:: To Write A Java Program To Transaction Between Client To Server Using TCP
Transaction TCP Aim:: To Write A Java Program To Transaction Between Client To Server Using TCP
CLIENT SIDE:
STEP 1: Create the socket using socket class. STEP 2: Bind the port to the socket. STEP 3: Initialize variable for socketinput and socketoutput to print and read the message from the server. STEP 4: Open the socket and start communicating with the server. STEP 5: After the communication ends, close the socket and terminate the program.
System.out.println("Message from the Client Side is : "+ Message); } } catch(Exception x) { System.out.println("Exception Occured"); } break; case 2: try { Socket soc=null; ServerSocket ss=null; DataOutputStream sso=null; DataInputStream sin=null; ss=new ServerSocket(55555); System.out.println("Waiting for client"); soc=ss.accept(); sso=new DataOutputStream(soc.getOutputStream()); sin=new DataInputStream(soc.getInputStream()); String s1; s1=sin.readLine(); FileInputStream fos=new FileInputStream(s1); int str; while((str=fos.read())!=-1) sso.writeBytes(""+(char)str); System.out.println("File has been sent successfully"); sso.close(); soc.close(); } catch(Exception x) { System.out.println("Exception Occured"+x); } break; } } }
Client:
import java.net.*; import java.io.*; public class chatclient { public static void main(String[] args)throws IOException { int Port = 2000; int Temp; int ch; System.out.println("Enter your choice(1/2):"); BufferedReader obj=new BufferedReader(new InputStreamReader(System.in)); String s=obj.readLine(); ch=Integer.parseInt(s); switch(ch) { case 1: try { Socket socket = new Socket("localhost",Port); InputStream StrIn = socket.getInputStream(); OutputStream StrOut = socket.getOutputStream(); DataInputStream DataIn = new DataInputStream(StrIn); DataOutputStream DataOut = new DataOutputStream(StrOut); while(true) { String Message = null; Message = DataIn.readUTF(); System.out.println("Message from the Server Side is : " + Message); System.out.println("Enter message to send : "); BufferedReader GetData=new BufferedReader(new InputStreamReader(System.in)); Message=null; Message=GetData.readLine(); DataOut.writeUTF(Message); }
} catch(Exception x) { System.out.println("Exception Occured"); } break; case 2: try { Socket soc=null; DataInputStream si=null; soc=new Socket(InetAddress.getLocalHost(),55555); si=new DataInputStream(soc.getInputStream()); DataInputStream inp=new DataInputStream(System.in); DataOutputStream so=new DataOutputStream(soc.getOutputStream()); String str; System.out.println("\n Enter the filename: "); str=inp.readLine(); so.writeBytes(str); so.writeBytes("\n"); FileOutputStream fos=new FileOutputStream("abc1.txt"); int str1; System.out.println("Transferring the file :"+str+" \n"); while((str1=si.read())!=-1) { fos.write((char)str1); System.out.print((char)str1); } System.out.println("\n File received successfully"); si.close(); } catch(Exception x) { System.out.println("Exception Occured"+x); } break; } } }
CLIENT SIDE:
RESULT:
Thus the transaction TCP has been done successfully.