0% found this document useful (0 votes)
74 views7 pages

Practical - 4: Aim:-Create An Application Using UDP Protocol

This document describes a UDP client-server application for chat. The server program receives messages from the client and sends responses. The client program allows the user to enter messages and sends them to the server. The document includes code for both the server and client programs in Java using DatagramSockets and DatagramPackets to send and receive UDP datagrams. It also provides an example of modifying the programs to have the server multiply a number entered by the client and send the result.

Uploaded by

Dhrumil Dancer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views7 pages

Practical - 4: Aim:-Create An Application Using UDP Protocol

This document describes a UDP client-server application for chat. The server program receives messages from the client and sends responses. The client program allows the user to enter messages and sends them to the server. The document includes code for both the server and client programs in Java using DatagramSockets and DatagramPackets to send and receive UDP datagrams. It also provides an example of modifying the programs to have the server multiply a number entered by the client and send the result.

Uploaded by

Dhrumil Dancer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

18BECE30063 AJP Practical-4

Practical – 4

Aim:- Create an application using UDP protocol.


Create chat with different methods
Client start to chat with server.

Server Program.-
package practical_4;

import java.net.DatagramSocket;

import java.net.*;

import java.io.*;

public class server {

public static void main(String[] args) throws Exception {

DatagramSocket server_socket = new DatagramSocket(1234);

byte in_data[] = new byte[1024];

DatagramPacket Packet2 = new DatagramPacket(in_data,in_data.length);

BufferedReader server_input = new BufferedReader (new


InputStreamReader(System.in));

InetAddress IP_add = InetAddress.getByName("localhost");

while(true) {
1
Page

server_socket.receive(Packet2);
18BECE30063 AJP Practical-4

String Str = new String(Packet2.getData(),0,Packet2.getLength());

System.out.println("Client : "+Str);

InetAddress IP_add1 = Packet2.getAddress();

int port = Packet2.getPort();

String send_Str = server_input.readLine();

in_data = send_Str.getBytes();

DatagramPacket Packet3 = new


DatagramPacket(in_data,in_data.length,IP_add1,port);

server_socket.send(Packet3);

Client Program.-
package practical_4;

import java.net.*;

import java.io.*;

import java.net.DatagramSocket;

public class client {

public static void main(String[] args) throws Exception

{
2
Page
18BECE30063 AJP Practical-4

BufferedReader user_input = new BufferedReader(new


InputStreamReader(System.in));

DatagramSocket client_socket = new DatagramSocket();

byte in_data[] = new byte[1024];

DatagramPacket Packet4 = new DatagramPacket(in_data,in_data.length);

InetAddress IP_add = InetAddress.getByName("localhost");

while(true) {

String Str = user_input.readLine();

in_data = Str.getBytes();

DatagramPacket Packet1 = new


DatagramPacket(in_data,in_data.length,IP_add,1234);

client_socket.send(Packet1);

client_socket.receive(Packet4);

String receive_str = new String(Packet4.getData(),0,Packet4.getLength());

System.out.println("Server : "+receive_str);

OUTPUT:--
Client Side:
3
Page
18BECE30063 AJP Practical-4

Server Side:

4
Page
18BECE30063 AJP Practical-4

Multiply two numbers


Client : Enter the number.
Serve : multiply the number entered by client.

Server Program.
package practical_4;

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

public class server_3 {


public static void main(String[] args) throws Exception {
DatagramSocket server_socket = new DatagramSocket(1234);
byte in_data[] = new byte[1024];
DatagramPacket Packet2 = new DatagramPacket(in_data,in_data.length);
server_socket.receive(Packet2);
String str = new String(Packet2.getData(),0,Packet2.getLength());
int num = Integer.parseInt(str);
int result = num*num;
byte out_data[] = (result + "").getBytes();
int port = Packet2.getPort();
InetAddress IP_add = InetAddress.getByName("localhost");
DatagramPacket Packet3 = new
DatagramPacket(out_data,out_data.length,IP_add,port);
5

server_socket.send(Packet3);
Page

}
18BECE30063 AJP Practical-4

Client Program.
package practical_4;

import java.net.*;

import java.io.*;

import java.util.Scanner;

public class client_3 {

public static void main(String [] args) throws Exception {

DatagramSocket client_socket = new DatagramSocket();

int num;

Scanner sc = new Scanner(System.in);

System.out.println("Enter any number:");

num = sc.nextInt();

byte in_data[] = (num + "").getBytes();

InetAddress IP_add = InetAddress.getByName("localhost");

DatagramPacket Packet4 = new


DatagramPacket(in_data,in_data.length,IP_add,1234);

client_socket.send(Packet4);

byte out_data[] = new byte[1024];


6
Page
18BECE30063 AJP Practical-4

DatagramPacket Packet1 = new


DatagramPacket(out_data,out_data.length,IP_add,1234);

client_socket.receive(Packet1);

String str = new String(Packet1.getData(),0,Packet1.getLength());

System.out.println("Result : "+str);

OUTPUT:--

7
Page

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy