Program to read information from Server Socket through TCP Client Socket
First compile both files. Run the server and run the client program.
Output of the program :
package com.hubberspot.networking.example; import java.net.*;
import java.io.*; public class MyServerSocket { public static void main(String args[]) throws UnknownHostException, IOException { ServerSocket ss=new ServerSocket(4444); System.out.println("Server Started"); Socket s = ss.accept(); } }
package com.hubberspot.networking.example; import java.net.*; import java.io.*; public class MyClientSocket { public static void main(String args[]) { try{ InetAddress addr = InetAddress.getByName("localhost"); Socket s=new Socket (addr, 4444); System.out.println (s.getInetAddress()); System.out.println (s.getPort()); System.out.println (s.getLocalPort()); s.close(); } catch(Exception e) { e.printStackTrace(); } } }
First compile both files. Run the server and run the client program.
Output of the program :