TCP - Udp Example Programs PDF
TCP - Udp Example Programs PDF
TCP - Udp Example Programs PDF
1. Network Terms
2. IP address, Port Numbers
3. Socket : TCP and UDP
4. Process
5. HTTP and HTTPS
6. Peer to Peer
7. FTP, Email, SMTP, MIME, PGP, ICMP
8. Unicast, Broadcast, Multicast
9. Client Side and Server Side Technologies
10. Firewall and Proxy
11. CORBA, J2EE, .NET
12. RIP, OSPF, BGP – Protocols
13. RTP, RTCP, RTSP Protocols
14. IP Overview
15. Router Architecture
16. MPLS, Control Plane, SIP, IP, Multicast, Packet Forwading, PBR
Lab Programs
1. Find IP
2. IP and MAC
3. Ports
4. Socket : UDP and TCP
5. Process
6. Thread
7. MultiThread
8. Thread: Extends, Runnable
9. Thread States
10. HTTP
11. HTTP Server
12. Peer to Peer Server
13. Broadcast
14. Multicast
15. RIP Protocol (Packet Tracer)
• Communication: UDP: Datagram, TCP: Stream
Example:
• Socket: Used by clients; servers use it too for communication with clients
2. Declaring a message
5. Closing streams
2. Declaring a message
ObjectOutputStream_name.writeObject(msg);
Eg. oos.writeObject(msg);
ObjectOutputStream_name.close();
OutputStream_name.close();
Eg. oos.close();
os.close();
Socket_name.close();
Eg. skt.close();
// declaring a message
String msg = "Hello Server";
catch(Exception e)
{ System.out.println(e);
}} }
TCP SERVER: CONNECTION CREATION STEPS
5. Closing Streams
• Binding is nothing but dedicating the port number to the client as long as it would like;
• accept() is used to bind the connection on the port number, requested by the client. .
3. Creating Streams (InputStream and ObjectInputStream)
• getInputStream() method of Socket returns an object of InputStream and this is the starting point on the server
program.
• The server uses input stream as it is receiving the message.
•As InputStream is an abstract class, it cannot be used directly. It is chained to a concrete class ObjectInputStream.
•readObject() method of ObjectInputStream reads the message string from the socket and
returns. This message is printed at the console.
Eg. skt.close();
ss.close();
Example: MyServer.java
//import packages
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args)
{ try {
// Create a new server socket that listens at port 6999
ServerSocket ss = new ServerSocket(6999);
System.out.println("Server is lisenting ... ");
catch(Exception e)
{ System.out.println(e);
}
}}
DatagramSocket and DatagramPacket
CONSTRUCTORS: DatagramSocket
• DatagramSocket()
• DatagramSocket(int port)
METHODS
CONSTRUCTORS: DatagramPacket
• DatagramPacket(byte[] barr, int length)
– it creates a datagram packet to receive the packets.
// getData() is used to Return the data contained in this packet as a byte array.
String str = new String(dp.getData(), 0, dp.getLength());
import java.net.*;
public class Sender
{
public static void main(String[] args) throws Exception
{
// Creating DatagramSocket
DatagramSocket ds = new DatagramSocket();
// Defining IP address
InetAddress ip = InetAddress.getByName("127.0.0.1");
1001101001010110 +
0000101110001110 +
0000110111001100 +
0100110001001111 =
---------------------------- if there is one 0, then that means errors
1111111111111111