Skip to main content

Connection oriented socket programming in java

Write a connection oriented socket program which can exchange the message between server and client once the connection is established.

Java Sockets makes networked programming easy. the java.net package offers developers a wide range of tools to create networked applications. the socket class provides the access to a TCP connection between hosts. Unfortunately, it doesn't provide an API for detecting a dropped connection from the remote system.

Java sockets can facilitate both TCP and UDP type protocols. While UDP connections are non-persistent, TCP connections often support a dynamic back-and-forth exchange of information between two network end systems.

Example of Java Socket Programming (Read and Write from both client and server)

//save this code in file name MyOwnServer.java

  1. import java.net.*;  
  2. import java.io.*;  
  3. class MyFirstServer{  
  4. public static void main(String[] args)throws Exception{  
  5. ServerSocket servSocket=new ServerSocket(3333);  
  6. Socket sock=servSocket.accept();  
  7. DataInputStream dataInput=new DataInputStream(sock.getInputStream());  
  8. DataOutputStream dataOutput=new DataOutputStream(spck.getOutputStream());  
  9. BufferedReader buffers=new BufferedReader(new InputStreamReader(System.in));  
  10.   
  11. String string1="";
  12. String string2="";  
  13. while(!string1.equals("stop")){  
  14. string1=dataInput.readUTF();  
  15. System.out.println("client says: "+string1);  
  16. string2=buffers.readLine();  
  17. dataOutput.writeUTF(string2);  
  18. dataOutput.flush();  
  19. }  
  20. dataInput.close();  
  21. sock.close();  
  22. servSocket.close();  
  23. }
  24. }

//save this code in file name MyOwnClient.java

  1. import java.net.*;  
  2. import java.io.*;  
  3. class MyFirstClient{  
  4. public static void main(String[] args)throws Exception{  
  5. Socket sock=new Socket("localhost",3333);  
  6. DataInputStream dataInput=new DataInputStream(sock.getInputStream());  
  7. DataOutputStream dataOutput=new DataOutputStream(spck.getOutputStream());  
  8. BufferedReader buffers=new BufferedReader(new InputStreamReader(System.in));  
  9.   
  10. String string1="";
  11. String string2="";  
  12. while(!string1.equals("stop")){  
  13. string1=buffers.readLine(); 
  14. dataOutput.writeUTF(string1);  
  15. dataOutput.flush();  
  16. string2=dataInput.readUTF();
  17. System.out.println("server says:",string2);
  18. }  
  19. dataOutput.close();  
  20. sock.close();  
  21. }
  22. }

To run the above given program you must open two command prompts and run each program at each command prompt.

After running the program first the client will write the message to the server then server will receive and display the message written by client. then Server will write the message to the client and client will receive and display the message sent by server. Then this process will go on.

Comments

Popular posts from this blog

Write a program using the algorithm count() to count how many elements in a container have a specified value.

 Here's an example program using the count() algorithm to count the number of occurrences of a specific value in a vector container: #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() {     vector<int> numbers = { 2, 5, 3, 7, 8, 5, 1, 5, 4 };          // count the number of occurrences of the value 5 in the vector     int count = count(numbers.begin(), numbers.end(), 5);          cout << "The number of occurrences of 5 in the vector is: " << count << endl;          return 0; } Output: The number of occurrences of 5 in the vector is: 3 Explanation: The program starts by creating a vector named numbers that contains several integer values. The count() algorithm is used to count the number of occurrences of the value 5 in the numbers vector. The function takes three arguments: the beginning and end iterators of...

What are Stub and Skeleton in Distributed Application? Explain its function with block diagram.

 Stub and Skeleton are two important components of distributed applications. A distributed application is a software system that runs on multiple computers connected through a network. It allows users to access resources and services on different computers as if they were on a local computer. In a distributed application, a client program on one computer sends a request to a server program on another computer. The server program processes the request and sends a response back to the client program. Stub and Skeleton help to facilitate this communication between the client and server programs. A Stub is a client-side proxy that represents the remote object on the client machine. It acts as a gateway for the client to communicate with the server. When a client invokes a method on the Stub, it marshals the arguments and sends them to the server over the network. The Stub then waits for the server to send a response. When the response is received, the Stub unmarshals the data and retur...

Explain the lifecycle of Servlet with block diagram.

 The lifecycle of a Servlet can be divided into several stages. Here's a block diagram that illustrates the different stages: Servlet API: The Servlet API provides a standard set of interfaces and classes for creating and interacting with Servlets. It is typically included in the web application's classpath as a JAR file. Servlet Container: The Servlet Container is a web server or application server that implements the Servlet API. It provides a runtime environment for executing Servlets and manages their lifecycle. Servlet Class: The Servlet Class is the Java class that implements the javax.servlet.Servlet interface. It contains the logic for processing HTTP requests and generating HTTP responses. init(): The init() method is called once when the Servlet is first loaded by the Servlet Container. It is used for initialization tasks, such as setting up database connections, loading configuration settings, or initializing other resources that will be used by the Servlet. service(...