








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









Socket Programming
Socket
Steps – To Make a Simple Client
Steps – To Make a Simple Server
// step 5: Send / Receive message //String reading name name = br.readLine(); sent by client //String appending msg = “hello”"Hello "with + name the +received " from Server";name // sending back to client pw.println(msg); //s.close(); closing communication sockey } // end while }catch(Exception ex){ System.out.println(ex); } } } // end class
// step 1: importing required package importimport java.net.;java.io.; import javax.swing.*; public class EchoClient{ public static void main(String args[]){ try { //step 2: create a communication socket // if your server will run on the same machine then you can pass “localhost” as server address / / Noticewhile creatingthat port server no is socketsimilar */ to one passed server Socket s = new Socket(“localhost”, 2222);
// step 3: Get I/O streams InputStream is = s.getInputStream(); InputStreamReader isr= new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); OutputStreamPrintWriter pwos == news.getOutputStream(); PrintWriter(os,true);
// step 4: Send / Receive message // asking use to enter his/her name String msg = JOptionPane.showInputDialog( "Enter your name"); // pw.println(msg); sending name to server
//// readingserver message (name appended with hello) from msg = br.readLine();
response from server
eventually run. Replace “localhost” with the new IP and start conversion over network ☺