Socket Programming(From Client to Server)
Socket Programming: From Client to Server Server End: import java.net.*; import java.util.*; import java.io.*; public class Server { public static void main(String arr[]) throws Exception { ServerSocket ss=new ServerSocket(12345); Socket s=ss.accept(); DataInputStream din=new DataInputStream(s.getInputStream()); DataOutputStream dout=new DataOutputStream(s.getOutputStream()); String str=din.readUTF(); System.out.println("From Client: "+str); str=str.toUpperCase(); dout.writeUTF(str); dout.flush(); din.close(); dout.close(); s.close(); ss.close(); } } Client End: import java.net.*; import java.util.*; import java.io.*; import java.util.*; public class Client { public static void main(String arr[]) throws Exception { Socket s=new Socket("localhost",12345); Scanner sc=new Scanner(System.in); DataInputStream din=new DataInputStream(s.getInputStream()); DataOutputStream dout=new DataOutputStream(s.getOutputStream()); System.out.print("Enter text: "); String str=sc.nextLine(); dout.writeUTF(str); dout.flush(); str=din.readUTF(); System.out.println("From Server: "+str); din.close(); dout.close(); s.close(); } } Output: Client end: $ javac Client.java $ java Client Enter text: Hello Server..! :) From Server: HELLO SERVER..! :) Server End: $ java Server.java $ java Server From Client: Hello Server..! :)
Tags:
Socket Programming
0 comments