Java Program To Create Socket At Specific Port

Chapter: Networking Last Updated: 28-10-2016 07:26:56 UTC

Program:

            /* ............... START ............... */
                
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

class JavaSocketSpecificPort {

	public static void main(String[] args) {
		try {
			InetAddress addr = InetAddress.getByName("74.125.67.100");
			Socket theSocket = new Socket(addr, 80);
			System.out.println("Connected to " + theSocket.getInetAddress() + " "
					+ "on port " + theSocket.getPort()
					+ " from port " + theSocket.getLocalPort() + " of " + 
					theSocket.getLocalAddress());
		} catch (UnknownHostException e) {
			System.err.println("I can't find " + e);
		} catch (SocketException e) {
			System.err.println("Could not connect to " + e);
		} catch (IOException e) {
			System.err.println(e);
		}
	}
}
                /* ............... END ............... */
        

Output

Connected to /74.125.67.100 on port 80 from port 50044 of /192.168.1.114

Tags

create Socket At Specific Port, Java, Networking

Similar Programs Chapter Last Updated
Java URL Parts Example Networking 28-10-2016
Java Find Proxy Settings Networking 10-09-2016
Java Download Webpage Example Networking 09-09-2016
Java URL Connection Date Networking 09-09-2016
Java Socket To Single Client Networking 09-09-2016
Java Server File Size Example Networking 09-09-2016
Java Multithreaded Server Example Networking 09-09-2016
Java Server File Modification Time Networking 09-09-2016
Java Connect To Server Example Networking 09-09-2016
Http Response Header In Java Example Networking 21-06-2016
Server Socket In Java Example Networking 30-04-2016
Socket Programming In Java Networking 30-04-2016
IP Address And HostName Of Local Computer In Java Networking 30-04-2016
HostName And IP Address In Java Networking 30-04-2016
Client Socket In Java Example Networking 30-04-2016
Read And Download Webpage In Java Example Networking 29-04-2016
Date Of URL Connection In Java Example Networking 29-04-2016
Who Is Lookup In Java Example Networking 21-07-2016
System Proxy In Java Example Networking 21-09-2018
Port Scanner In Java Example Networking 21-07-2016
Ping IP Address In Java Example Networking 21-09-2018
MAC Address In Java Example Networking 22-09-2018
Get IP Address In Java Example Networking 07-04-2016
URL Properties In Java Example Networking 09-09-2016
Default URL Object In Java Example Networking 18-07-2016

1