944,157 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 27104
  • Java RSS
Sep 12th, 2005
0

Simple Client Server Instant Messaging Java code using UDP datagrams

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
woolf is offline Offline
1 posts
since Sep 2005
Aug 10th, 2009
0

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

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();
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ashu_305 is offline Offline
3 posts
since Aug 2009
Aug 10th, 2009
-1

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

@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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
May 28th, 2010
0
Re: Simple Client Server Instant Messaging Java code using UDP datagrams
How to establish the connection between two hosts through socket programming(TCP) in java.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vani.golakoti is offline Offline
4 posts
since May 2010
Dec 22nd, 2010
-1

chat code

hi

Iwant simple code chat in java
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zahertal is offline Offline
1 posts
since Dec 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: check this out
Next Thread in Java Forum Timeline: Lottery





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC