| | |
Simple Client Server Instant Messaging Java code using UDP datagrams
![]() |
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
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();
}
}
//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();
}
}
@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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- creating an instant messenger program in java (Java)
- Java Client/Server (Java)
- adding an image to a java code (Java)
- Instant messaging (IT Professionals' Lounge)
- URGENT: FTP Client / Server using RMI (Java)
- java client server quiz. interesting!!! (Java)
- Client-server vs RMI using Java (Java)
Other Threads in the Java Forum
- Previous Thread: ftp using udp with gui
- Next Thread: java help needed - incompatible types
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health hql html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows






