Lets look at the syntax below

Syntax
ServerSocket ss;
ss=new ServerSocket(Port_No);

Example
ServerSocket ss = new ServerSocket(80);
Lets Look at TCP IP Server Program

 

TCP IP Server Program

import java.io.*;
import java.net.*;

public class MyDemoServer {

public static void main(String[] args) {

try{
ServerSocket ss = new ServerSocket(80);
Socket s=ss.accept();//Establishing Connection
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = (String)dis.readUTF();
System.out.println("Message="+str);
ss.close();
}catch(Exception e){System.out.println(e);}

}

}
 Output:
Message=Hello Server

 

TCP/IP Client Socket : Socket Class

The Client in Socket Programming must know two information
1.IP address of the server.
2.Port Number

 

Syntax

Socket myClient ;//create object of socket class
myClient = new Socket(“Machine name”,PortNumber);
Machine name is DNS or IP address
PortNumber is the Port on which the server you are trying to connect

 

TCP IP Client Program
For Example
Socket s = new Socket(“Localhost”,21);

import java.io.*;
import java.net.*;

public class MyClientDemo {

public static void main(String[] args) {

try{
Socket s= new Socket("localhost",21);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hi Server");
}catch(Exception e){System.out.println(e);}

}

}
Output:
Message=Hi Server