{"id":109,"date":"2025-02-23T04:42:22","date_gmt":"2025-02-23T04:42:22","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=109"},"modified":"2025-02-23T04:42:22","modified_gmt":"2025-02-23T04:42:22","slug":"request-and-response-between-client-and-server-using-socket-programming","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2025\/02\/23\/request-and-response-between-client-and-server-using-socket-programming\/","title":{"rendered":"Request and Response between Client and Server using Socket Programming"},"content":{"rendered":"\n<p>Develop Simple Application for Request and Response between Client and Server using Socket Programming<\/p>\n\n\n\n<p><strong>Answer :<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Server Application<\/strong> (Server.java): This will accept client requests, process them, and send a response back to the client.<\/li>\n\n\n\n<li><strong>Client Application<\/strong> (Client.java): This will send requests to the server and receive responses.<\/li>\n<\/ol>\n\n\n\n<p>Here\u2019s how to implement it:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Server.java<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\nimport java.net.*;\n\npublic class Server {\n    public static void main(String&#91;] args) {\n        try {\n            \/\/ Create server socket on port 12345\n            ServerSocket serverSocket = new ServerSocket(12345);\n            System.out.println(\"Server is waiting for client requests...\");\n\n            \/\/ Wait for client connection\n            Socket clientSocket = serverSocket.accept();\n            System.out.println(\"Client connected!\");\n\n            \/\/ Get input and output streams\n            InputStream inputStream = clientSocket.getInputStream();\n            OutputStream outputStream = clientSocket.getOutputStream();\n\n            \/\/ Create buffered reader and writer for reading and sending data\n            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));\n            PrintWriter writer = new PrintWriter(outputStream, true);\n\n            \/\/ Read request from client\n            String clientMessage = reader.readLine();\n            System.out.println(\"Received from client: \" + clientMessage);\n\n            \/\/ Process the request and send a response\n            String response = \"Hello from Server: You said, '\" + clientMessage + \"'\";\n            writer.println(response);\n            System.out.println(\"Response sent to client: \" + response);\n\n            \/\/ Close the streams and socket\n            reader.close();\n            writer.close();\n            clientSocket.close();\n            serverSocket.close();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Client.java<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\nimport java.net.*;\n\npublic class Client {\n    public static void main(String&#91;] args) {\n        try {\n            \/\/ Connect to the server at localhost and port 12345\n            Socket socket = new Socket(\"localhost\", 12345);\n            System.out.println(\"Connected to server!\");\n\n            \/\/ Create input and output streams\n            InputStream inputStream = socket.getInputStream();\n            OutputStream outputStream = socket.getOutputStream();\n\n            \/\/ Create buffered reader and writer for reading and sending data\n            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));\n            PrintWriter writer = new PrintWriter(outputStream, true);\n\n            \/\/ Send request to server\n            String message = \"Hello Server!\";\n            writer.println(message);\n            System.out.println(\"Sent to server: \" + message);\n\n            \/\/ Read response from server\n            String serverResponse = reader.readLine();\n            System.out.println(\"Received from server: \" + serverResponse);\n\n            \/\/ Close the streams and socket\n            reader.close();\n            writer.close();\n            socket.close();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Steps for Running the Application:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Start the Server:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Open a terminal or command prompt.<\/li>\n\n\n\n<li>Navigate to the directory containing <code>Server.java<\/code>.<\/li>\n\n\n\n<li>Compile and run the server: <code>javac Server.java java Server<\/code><\/li>\n\n\n\n<li>The server will now be listening for client connections on port 12345.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Start the Client:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Open another terminal or command prompt.<\/li>\n\n\n\n<li>Navigate to the directory containing <code>Client.java<\/code>.<\/li>\n\n\n\n<li>Compile and run the client: <code>javac Client.java java Client<\/code><\/li>\n\n\n\n<li>The client will connect to the server, send a message, and receive a response.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Notes:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Server:<\/strong> The <code>Server<\/code> listens on port <code>12345<\/code> for incoming client connections using <code>ServerSocket<\/code>. When a client connects, it creates a <code>Socket<\/code> to communicate with that client. It reads the message sent by the client and sends a response back.<\/li>\n\n\n\n<li><strong>Client:<\/strong> The <code>Client<\/code> connects to the server at <code>localhost<\/code> and port <code>12345<\/code>. It sends a request and waits for the server\u2019s response.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Develop Simple Application for Request and Response between Client and Server using Socket Programming Answer : Here\u2019s how to implement it: Server.java Client.java Steps for Running the Application: Notes:<\/p>\n","protected":false},"author":1,"featured_media":97,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,4],"tags":[35,33,34],"class_list":["post-109","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-advanced-java","category-bscit","tag-advanced-java","tag-bsc-it","tag-bsc-it-paper-solution"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions\/110"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/97"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}