Skip to main content

Posts

Showing posts with the label Networking

Java Socket Interview Questions

Explain how to create and use sockets in java. Client Side: //creating client socket socket=new Socket("localhost",4000); //InetAddress.getByName ("127.0.0.1") //cretaing output stream for writting a data write=new DataOutputStream(socket.getOutputStream()); //reading from a console read=new DataInputStream(System.in); String data=null; while(true) { data=read.readLine(); write.writeUTF(data); } Server : //creating severSocket object. server=new ServerSocket(4000, 2); System.out.println("Socket open"); //accept() method listens for a connection to be made to this socket and accepts it. client=server.accept(); System.out.println("Client connected"); //receive inputs from client receive=new DataInputStream(client.getInputStream()); String data=null; while(true) { data=receive.readUTF(); System.out.println(data); } How to send to receive datagram packet? DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketA...

Sample Coding in Networking (java.net)

Creating a URL try { // With components. URL url = new URL( "http" , "hostname" , 80 , "index.html" ); // With a single string. url = new URL( "http://hostname:80/index.html" ); } catch (MalformedURLException e) { } Parsing a URL try { URL url = new URL( "http://hostname:80/index.html#_top_" ); String protocol = url.getProtocol(); // http String host = url.getHost(); // hostname int port = url.getPort(); // 80 String file = url.getFile(); // index.html String ref = url.getRef(); // _top_ } catch (MalformedURLException e) { } Reading Text from a URL try { URL url = new URL( "http://hostname:80/index.html" ); BufferedReader in = new BufferedReader( new InputStreamReader(url.openStream())); String str; while ((str = in.readLine()) != null) { process (str); } in.close(); } catch (MalformedURLException e) { } catch (IOException e) { } Resolving a Hostname Creating a Client Socket try { InetAddress...

Networking (java.net)

This example demonstrates the declaration and use of all three return types. 1. Define the remote interface. import java.rmi.*; public interface RObject extends Remote { // This return value is primitive. int primitiveRet() throws RemoteException; // This return value implements Serializable. Integer byValueRet() throws RemoteException; // This return value implements Remote. ArgObject byRefRet() throws RemoteException; } public interface ArgObject extends Remote { int aMethod() throws RemoteException; } 2. Define the remote object implementation. import java.rmi.*; import java.rmi.server.UnicastRemoteObject; public class RObjectImpl extends UnicastRemoteObject implements RObject { public RObjectImpl() throws RemoteException { super(); } public int primitiveRet() throws RemoteException { return 3000; } public Integer byValueRet() throws RemoteException { return new Integer(2000); } public ArgObject byRefRet() throws RemoteException { return new ArgObjectImpl(); } } 3. Compile the remot...