0% found this document useful (0 votes)
20 views8 pages

UDP Practice

The document contains code for UDP client-server programs that send and receive numeric data. A client program takes input, converts it to bytes, and sends it to a server program. The server receives the bytes, converts it back to a number, performs calculations on it, and prints the results.

Uploaded by

Tejas Shukla
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)
20 views8 pages

UDP Practice

The document contains code for UDP client-server programs that send and receive numeric data. A client program takes input, converts it to bytes, and sends it to a server program. The server receives the bytes, converts it back to a number, performs calculations on it, and prints the results.

Uploaded by

Tejas Shukla
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/ 8

package class_practice;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class fudpserver {

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


{
System.out.println("Waiting for client");
DatagramSocket ds = new DatagramSocket(9800);
byte b[] = new byte[100];
DatagramPacket dp= new DatagramPacket(b,100);
ds.receive(dp);
String s= new String(b);

System.out.println(s);
ds.close();
// TODO Auto-generated method stub

}
}

package class_practice;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class fudpclient {

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


DatagramSocket ds = new DatagramSocket();
byte b[] = "This is my UDP client".getBytes();
int len= b.length;
InetAddress ip=
InetAddress.getByName("localhost");
int port_no=9800;
DatagramPacket dp= new
DatagramPacket(b,len,ip,port_no);
ds.send(dp);
ds.close();

// TODO Auto-generated method stub


}

package class_practice;

import java.math.BigInteger;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class fudpclient2 {


public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
System.out.println("Enter The Number");
Scanner sc= new Scanner(System.in);
Integer n = sc.nextInt();
BigInteger bigInt = BigInteger.valueOf(n);
byte b[] = bigInt.toByteArray();
int len= b.length;
System.out.println(len);
InetAddress ip=
InetAddress.getByName("localhost");
int port_no=9800;
DatagramPacket dp= new
DatagramPacket(b,len,ip,port_no);
ds.send(dp);
ds.close();

// TODO Auto-generated method stub


}

}
package class_practice;

import java.math.BigInteger;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class fudpserver2 {

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


{
System.out.println("Waiting for client");
DatagramSocket ds = new DatagramSocket(9800);
byte b[] = new byte[1];
DatagramPacket dp= new DatagramPacket(b,1);
ds.receive(dp);
int s = new BigInteger(b).intValue();
System.out.println(s);
ds.close();
// TODO Auto-generated method stub

package class_practice;

import java.math.BigInteger;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class fudpserver3 {


public static void main(String[] args) throws Exception
{
System.out.println("Waiting for client");
DatagramSocket ds = new DatagramSocket(9800);
byte b[] = new byte[1];
DatagramPacket dp= new DatagramPacket(b,1);
ds.receive(dp);
int s = new BigInteger(b).intValue();
double a= 3.14*s*s;
System.out.println(a);
ds.close();
// TODO Auto-generated method stub

}
}
package class_practice;

import java.math.BigInteger;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class fudpclient3 {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
System.out.println("Enter The Number");
Scanner sc= new Scanner(System.in);
Integer n = sc.nextInt();
BigInteger bigInt = BigInteger.valueOf(n);
byte b[] = bigInt.toByteArray();
int len= b.length;
System.out.println(len);
InetAddress ip=
InetAddress.getByName("localhost");
int port_no=9800;
DatagramPacket dp= new
DatagramPacket(b,len,ip,port_no);
ds.send(dp);
ds.close();

// TODO Auto-generated method stub

}
}

You might also like