0% found this document useful (0 votes)
16 views

Program10 UDP Socket Programming

The document contains code for a client and server program that communicate over UDP. The client program sends a message to the server, which receives it, prompts the user for a response, and sends a reply message back to the client.

Uploaded by

study material
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Program10 UDP Socket Programming

The document contains code for a client and server program that communicate over UDP. The client program sends a message to the server, which receives it, prompts the user for a response, and sends a reply message back to the client.

Uploaded by

study material
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Client program import java.net.

*;

public class UDPC

public static void main(String[] args)

DatagramSocket skt;

try

skt=new DatagramSocket();

String msg= "text message ";

byte[] b = msg.getBytes();

InetAddress host=InetAddress.getByName("127.0.0.1");

int serverSocket=6788;

DatagramPacket request =new DatagramPacket(b,b.length,host,serverSocket);

skt.send(request);

byte[] buffer =new byte[1000];

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

skt.receive(reply);

System.out.println("client received:" +new String(reply.getData()));


skt.close();

catch(Exception ex) {}

Server program

import java.util.Scanner; import java.net.*;

public class UDPS

public static void main(String[] args)

DatagramSocket skt=null;

String ch;

Scanner input=new Scanner(System.in);

try

skt=new DatagramSocket(6788);

byte[] buffer = new byte[1000];


while(true)

DatagramPacket request = new DatagramPacket(buffer,buffer.length); skt.receive(request);

System.out.print("enter the data"); ch=input.nextLine();

byte[] sendMsg= (ch+ " server processed").getBytes();

DatagramPacket reply = new DatagramPacket(sendMsg,sendMsg.length,request.getAddress


(),request.getPort());

skt.send(reply);

catch(Exception ex) {}

Output At Server on running:


Output At Client on running:

You might also like