0% found this document useful (0 votes)
62 views7 pages

Transaction TCP Aim:: To Write A Java Program To Transaction Between Client To Server Using TCP

The document describes a Java program to enable TCP transactions between a client and server. The server side creates a socket, binds it to a port, listens for client requests, and accepts connections, reading and writing data. The client side creates a socket, binds a port, initializes input/output streams, opens the socket to communicate with the server, and closes the socket after communication ends. The program implements two cases - one for message transfer and one for file transfer between the client and server using TCP sockets, input/output streams, and other classes.

Uploaded by

vijithacvijayan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views7 pages

Transaction TCP Aim:: To Write A Java Program To Transaction Between Client To Server Using TCP

The document describes a Java program to enable TCP transactions between a client and server. The server side creates a socket, binds it to a port, listens for client requests, and accepts connections, reading and writing data. The client side creates a socket, binds a port, initializes input/output streams, opens the socket to communicate with the server, and closes the socket after communication ends. The program implements two cases - one for message transfer and one for file transfer between the client and server using TCP sockets, input/output streams, and other classes.

Uploaded by

vijithacvijayan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

TRANSACTION TCP AIM:

To write a java program to transaction between client to server using TCP.

ALGORITHM: SERVER SIDE:


STEP 1: Create a socket for server using socket class. STEP 2: Bind the user defined port to the socket and starts listening for client request. STEP 3: Accept the connection from the client using accept (). STEP 4: Read the IOStream the socket and display in the server window. STEP 5: Close the socket and terminate the program.

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.

PROGRAM: SERVER SIDE:


import java.net.*; import java.io.*; import java.util.*; public class chatserver { public static void main(String[] args)throws IOException { int Port = 2000; int Temp; int ch; System.out.println("Enter your choice(1 to transfer msg/2 to transfer file):"); BufferedReader obj=new BufferedReader(new InputStreamReader(System.in)); String s=obj.readLine(); ch=Integer.parseInt(s); switch(ch) { case 1: try { ServerSocket SocketId = new ServerSocket(Port); System.out.println("Waiting for a client..."); Socket socket = SocketId.accept(); InputStream StrIn = socket.getInputStream(); OutputStream StrOut = socket.getOutputStream(); DataInputStream DataIn = new DataInputStream(StrIn); DataOutputStream DataOut = new DataOutputStream(StrOut); while(true) { System.out.println("Enter message to send : "); BufferedReader GetData=new BufferedReader(neW InputStreamReader(System.in)); String Message=null; Message=GetData.readLine(); DataOut.writeUTF(Message); Message = null; Message = DataIn.readUTF();

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; } } }

OUTPUT: SERVER SIDE:

CLIENT SIDE:

RESULT:
Thus the transaction TCP has been done successfully.

You might also like