0% found this document useful (0 votes)
9 views3 pages

CN5 1 1

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)
9 views3 pages

CN5 1 1

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/ 3

NAME :

STD : T.E.
ROLL NO. :
PRACTICAL NO. 05
Socket programming.

import java.net.*;

import java.io.*;

public class Server2 {

public static void main(String[] args) {

DatagramSocket dsoc = null;

PrintWriter pw = null;

try {

byte[] buffer = new byte[3072];

dsoc = new DatagramSocket(1000);

FileOutputStream fos = new FileOutputStream("testudp1.txt", true);

pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos)));

while (true) {

DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

dsoc.receive(dp);

String receivedData = new String(dp.getData(), 0, dp.getLength());

System.out.println(receivedData);

pw.println(receivedData);

pw.flush(); // Ensure data is written to the file immediately

} catch (IOException e) {

e.printStackTrace();

} finally {

if (pw != null) {
pw.close();

if (dsoc != null && !dsoc.isClosed()) {

dsoc.close();

Output :

import java.net.*;

import java.io.*;

public class Client2 {

public static void main(String[] args) {

DatagramSocket dsoc = null;

FileInputStream fis = null;

try {

byte[] buffer = new byte[1024]; // Adjust buffer size as needed

fis = new FileInputStream("testudp.txt");


dsoc = new DatagramSocket();

int bytesRead;

while ((bytesRead = fis.read(buffer)) != -1) {

DatagramPacket packet = new DatagramPacket(buffer, bytesRead,


InetAddress.getLocalHost(), 1000);

dsoc.send(packet);

} catch (IOException e) {

e.printStackTrace();

} finally {

// Close resources

if (fis != null) {

try {

fis.close();

} catch (IOException e) {

e.printStackTrace();

if (dsoc != null && !dsoc.isClosed()) {

dsoc.close();

} Output :

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