Program : DatagramSocket and DatagramPacket (Client-Server Program using UDP/IP)
By : Kapil Lohia
Program 1 :
import java.net.*; class udpip_server { public static DatagramSocket ds; public static byte buffer[]=new byte[1024]; public static void Myserver() throws Exception { int pos=0; while(true) { int c=System.in.read(); switch(c) { case -1: System.out.println("Server quits"); return; case '\r':break; case '\n':ds.send(new DatagramPacket(buffer,pos,InetAddress.getLocalHost(),777)); pos=0; break; default: buffer[pos++]=(byte) c; } } } public static void main(String args[]) throws Exception { System.out.println("Server ready..\n Please type here"); ds=new DatagramSocket(888); Myserver(); } }
Program 2:
import java.net.*; class udpip_client { public static DatagramSocket ds; public static byte buffer[]=new byte[1024]; public static void Myclient() throws Exception { while(true) { DatagramPacket p=new DatagramPacket(buffer,buffer.length); ds.receive(p); System.out.println(new String(p.getData(),0,p.getLength())); } } public static void main(String args[]) throws Exception { System.out.println("Client - Press CTRL+C to quit"); ds=new DatagramSocket(777); Myclient(); } }
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
November 8th, 2010 at 7:07 pm
plzz explain the output
November 8th, 2010 at 7:07 pm
plzz explain the output for this program
September 17th, 2011 at 10:22 pm
nice and easy example to understand client server application
December 25th, 2011 at 10:19 am
y thr r dffrnt prt nmbr fr clnt(777) & svr(888)…?