Simple Chatting (Beta ver ) Interface

rahul8590 0 Tallied Votes 277 Views Share

Well , i have created a simple chatting interface . i would not call it a chatting interface actually cuz i had something else in my mind and finally landed on something else .

i would like if you guys could excecute it and help me making it a real chatting interface .

Well the basic thing is that . i have wriiten a server side code and then client side code .
the user complies one after the another and then runs those files in different DOS windows . Well i want to extend this to a real time chatting interface


the first is the UDPServer program
u will have to save the file as UDPServer (its case sensitive ) and compile it and run in in the first DOS window


the second file is UDPClient program . u will have to follow the same and then run it.
u might be able to chat (well not the traditional meaning here) in the two windows .

i hope u guys will give me ideas n help me improving it .

import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class UDPServer
{
public static void main(String args[]) 
{
try
{
System.out.println("UDPSERVER");
DatagramSocket ds=new DatagramSocket(10000);
System.out.println("socket created");
DatagramPacket p1,p2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
byte arr[]=new byte [255];
String input,output;
System.out.println("packet created");
do
{
arr=new byte[255];
p1=new DatagramPacket (arr,255);
ds.receive (p1);
arr=p1.getData ();

input=new String (arr);
System.out.print("Message from client \t");
System.out.println(input);
if(input.charAt (0)=='*')
{
break;
}
System.out.println("Enter your message");
output=br.readLine();
arr=output.getBytes();
p2 = new DatagramPacket (arr,arr.length,p1.getAddress(),p1.getPort());
ds.send(p2);
}while (true);
ds.close();
System.out.println("closed server");
}catch(Exception e)
{
System.out.println("ERROR  :"+e);
}
}
}



-------------------------------------------------------------------------------


import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class UDPClient
{
public static void main(String args[]) 
{
try
{
System.out.println("UDPCLIENT");
DatagramSocket ds;
System.out.println("socket created");
DatagramPacket p1,p2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
byte arr[]=new byte [255];
String input,output;
InetAddress i=InetAddress.getLocalHost();
output="Hi i am client, send me a message";
arr=output.getBytes();
p1=new DatagramPacket(arr,arr.length,i,10000);
ds=new DatagramSocket();
ds.send(p1);
System.out.println("packet created");
do
{
arr=new byte[255];
p1=new DatagramPacket (arr,255);
ds.receive (p1);
arr=p1.getData();
input=new String (arr);
System.out.print("Message from server \t");
System.out.println(input);
if(input.charAt(0)=='*')
{
break;
}
System.out.println("Enter your message");
output=br.readLine();
arr=new byte[255];
arr=output.getBytes();
p2=new DatagramPacket(arr,arr.length,i,10000);
ds.send(p2);
}while (true);
ds.close();
System.out.println("closed server");
}catch(Exception e)
{
System.out.println("ERROR  :"+e);
}
}
}
Ezzaral 2,714 Posting Sage Team Colleague Featured Poster

Learn to indent code.
Learn what "object oriented" means.

toyi 0 Newbie Poster

also provide / write comments in your programme for others to be
able to under stand what is hapening on you code . coz they can or we can also learn something from your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.