Future Vision BIE Future Vision BIE


ONE STOP FOR ALL STUDY MATERIALS & LAB PROGRAMS


E MENU Whatsapp Share Join Telegram, to get Instant Updates
× NOTE! Click on MENU to Browse between Subjects...

Advertisement

17CSL57
COMPUTER NETWORK LABORATORY
[As per Choice Based Credit System (CBCS) scheme]
(Effective from the academic year 2017-2018)
SEMESTER - V



This Page Provides Program & Output.
Program 10

Program 10
Write a program on datagram socket for client/server to display the messages on client side, typed at the server side




Advertisement

UDPServer.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
import java.net.*;
class UDPServer
{
	public static DatagramSocket serversocket;
	public static DatagramPacket dp;
	public static BufferedReader br;
	public static InetAddress ia;
	public static byte buf[] = new byte[1024];
	public static int cport = 222,sport=555;
	public static void main(String[] args) throws IOException
       {
         serversocket = new DatagramSocket(sport);
         dp = new DatagramPacket(buf,buf.length);
         br = new BufferedReader (new InputStreamReader(System.in));
         ia = InetAddress.getLocalHost();
 
         System.out.println("Server is Running...");
         while(true)
         {
          serversocket.receive(dp);
          String str2 = new String(dp.getData(), 0, dp.getLength());
          if(str2.equals("exit"))
          {
           System.out.println("Terminated...");
           break;
          }
          System.out.println("Client said : " + str2);
 
          String str3 = new String(br.readLine());
          buf = str3.getBytes();
          serversocket.send(new DatagramPacket(buf,str3.length(), ia, cport));
         }
       }
    }




UDPClient.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.*;
import java.net.*;
class UDPClient
{
	public static DatagramSocket clientsocket;
	public static DatagramPacket dp;
	public static BufferedReader br;
	public static InetAddress ia;
	public static byte buf[] = new byte[1024];
	public static int cport = 222, sport = 555;
	public static void main(String[] args) throws IOException
	{
		clientsocket = new DatagramSocket(cport);
		dp = new DatagramPacket(buf, buf.length);
		br = new BufferedReader(new InputStreamReader(System.in));
		ia = InetAddress.getLocalHost();
		  
		System.out.println("Client is Running...");
		System.out.println("Type some text if u want to Quit type 'exit'.");
		while(true)
		{
			String str1 = new String(br.readLine());
			buf = str1.getBytes();
			if(str1.equals("exit"))
			{
				System.out.println("Terminated..");
				clientsocket.send(new DatagramPacket(buf,str1.length(),ia,sport));
				break;
			}
			clientsocket.send(new DatagramPacket(buf,str1.length(), ia, sport));
			 
			clientsocket.receive(dp);
			String str4 = new String(dp.getData(), 0, dp.getLength());
			System.out.println("Server said : " + str4);
		}
	}
}

Process to Execute the Program

Step 1: We need to have Java JDK installed, So That Java Programs can Run.
Step 2: Copy & Paste the Below Code of UDPServer.java & UDPClient.java.
Step 3: or simple Download the Source Code.

Loading Image
Fig 10.1: Required Files .
Step 4: Open Command Prompt (cmd).
Step 5: Navigate to the UDPServer.java & UDPClient.java File location.
Step 6: Use CD & DIR command on cmd to navigate.
Step 7: First Run the UDPServer.java => javac UDPServer.java
Step 8: => java UDPServer
Loading Image
Fig 10.2: Demonstration of UDPServer .
Step 9: Now Run UDPClient.java => javac UDPClient.java
Step 10: => java UDPClient
Loading Image
Fig 10.3: Demonstration of UDPClient .
Step 11: Once Connection is established between UDPClient & UDPServer. Communication can start from either side of them
Step 12: Enter "exit" to terminate the connection.
Loading Image
Fig 10.4: Terminating of UDPClient .

Loading Image
Fig 10.5: Terminating of UDPClient .

× Note Please Share the website link with Your Friends and known Students...

-ADMIN

× Note Page Number is specified to navigate between Pages...
T = Text book
QB = Question Bank
AS = Amswer Script

-ADMIN