Simple Client Server Instant Messaging Java code using UDP datagrams

Reply

Join Date: Sep 2005
Posts: 1
Reputation: woolf is an unknown quantity at this point 
Solved Threads: 0
woolf woolf is offline Offline
Newbie Poster

Simple Client Server Instant Messaging Java code using UDP datagrams

 
0
  #1
Sep 12th, 2005
Hi,

I am trying to write a java program for a client server instant messaging system using UPD datagram. I am trying to think of how to start. Anyone has any suggestions? Please advise
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: ashu_305 is an unknown quantity at this point 
Solved Threads: 0
ashu_305 ashu_305 is offline Offline
Newbie Poster

Re: Simple Client Server Instant Messaging Java code using UDP datagrams

 
0
  #2
Aug 10th, 2009
Hi !!! I built a program for msg transfer in java using udp.

//udp server
import java.net.*;
import java.io.*;
class Server
{
public static void main(String args[])throws IOException
{
byte b[]=new byte[1024];
DatagramSocket ds=new DatagramSocket(5001);
DatagramPacket dp=new DatagramPacket(b,1024);

//System.out.println("receiving");
ds.receive(dp);
String str=new String(dp.getData(),0,dp.getLength());
//receive file name
System.out.println("Received file "+str);
FileReader fr=new FileReader(str);
BufferedReader br1=new BufferedReader(fr);
while((str=br1.readLine())!=NULL)
{
b=str.getBytes();
ds.send(new DatagramPacket(b,b.length,InetAddreass.getLocalHost(),5000));
}
ds.close();
}
}



//udp client

import java.net.*;
import java.io.*;
class Client
{
public static void main(String args[])throws IOException
{
byte b[]=new byte[1024];
DatagramSocket ds=new DatagramSocket(5000);
DatagramPacket dp=new DatagramPacket(b,1024);
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter file name\n");
String str1;
str1=dis.readLine();
b=str1.getBytes();
ds.send(new DatagramPacket(b,b.length,InetAddress.getLocalHost(),5001));
System.out.println("Contents of file:"+str1+"\n");
while(str1!=NULL)
{
ds.receive(dp);
str1=new String(dp.getData(),0,dp.getLength());//receive file contents
System.out.println(str1);
}
ds.close();
}
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,179
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 481
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Simple Client Server Instant Messaging Java code using UDP datagrams

 
0
  #3
Aug 10th, 2009
@ashu_305 congratulation on your program, just shame you are about 4 year too late...

PS: Next step in your messaging service would be perhaps some GUI and use of threads. Good luck
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC