4 Replies - 13765 Views - Last Post: 19 January 2011 - 02:51 AM Rate Topic: -----

#1 parita2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 4
  • Joined: 18-January 11

chat application in java

Posted 18 January 2011 - 04:21 AM

i am building chat application in applet.

can i get some help like codesnippet or site for chat application
Is This A Good Question/Topic? 0
  • +

Replies To: chat application in java

#2 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 144
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: chat application in java

Posted 18 January 2011 - 04:24 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 parita2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 4
  • Joined: 18-January 11

Re: chat application in java

Posted 18 January 2011 - 04:33 AM

import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

import java.awt.event.*;
import java.awt.TextField;
import java.lang.*;
public class MyClient extends Applet implements ActionListener{
	
    Socket localSocket;
    PrintWriter out;
    BufferedReader in;
    Socket s;
    Frame window=new Frame("2nd page");
    TextField name=new TextField(15);
	Label label=new Label("Please enter your name");
    Button btn=new Button("Login");
    TextArea room;
    TextField txt;
    Button send;
    String name1;
    DataOutputStream dout;
    public void init(){
    	add(label);
    	add(name);
    	add(btn);
    	window.setLayout(new FlowLayout());
    	window.add(room=new TextArea("",9, 38, TextArea.SCROLLBARS_VERTICAL_ONLY));
    	window.add(new Label( "Message"));
    	window.add(txt=new TextField(27));
    	window.add(send=new Button("Send"));
    	txt.addActionListener(this);
    	send.addActionListener(this);
        setLayout(new FlowLayout());
          
       
    }
    
    public boolean action(Event evt, Object whatAction) 
    {
    	if(evt.target instanceof Button)
        { 
    	String buttonLabel = (String) whatAction;
    	if(buttonLabel=="Login");
    	{
    		window.resize(300, 300);
        	window.show();
    	}
    	return true;
    	}	
        
    	return false;
    }
    /*	if(Button==name){
    		window.resize(300, 300);
        	window.show();	
  		  String str="";
  		  str=name.getText();
  		  name.setText("");
  		  str=name1+": > "+str;*/
  		
    
    
    
    public void actionPerformed(ActionEvent e)
    {	
    	  
        try
        
        {
       
       room.append(name.getText()+":");
       room.append(txt.getText()+'\n');
        	Socket s=new Socket("localhost",1004);
        	txt.setText("");
        DataOutputStream dout=new DataOutputStream(s.getOutputStream());
     System.out.println(s);
        DataInputStream din=new DataInputStream(s.getInputStream());
        System.out.println(s);
           // localSocket = new Socket(this.getCodeBase().getHost(), 2000);
            //Setup data stream in and out of socket and from KeyBoard
           // in = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
            //out= new PrintWriter(localSocket.getOutputStream());
            //While we have a connection
            // get textfield value
            // String s = t1.getText();
            //Read Texfield value
              
        	 //flush the buffer if not full!  
            // read incoming string from socket  
            //System.out.println(in.readLine());  
      
        }
        catch(UnknownHostException unc)
        {
            System.out.println("Connection why not connected");
        }
        catch(IOException ioe)
        {
            System.out.println(ioe.getMessage());
        }}
    
    class My implements Runnable{
    	  DataInputStream din;
    	  My(DataInputStream din)
    	  {
    		  this.din=din;  
    	  }
   public void run()
   {
	   String str1="";
	   while(true){
		      try{
		        str1=din.readUTF(); // receive the message
		        // add the message in list box
		       
		      }catch(Exception e){}
		    }
	 }
   }
   
}
]

[import java.io.*;
import java.net.*;
import java.util.*;

public class MyServer{
  ServerSocket ss;
  Socket s;
  String txt1;
	static DataInputStream din;  
	static DataInputStream dout;  
  MyServer()throws IOException{
    ss=new ServerSocket(1004);  // create server socket
    while(true){
      s=ss.accept();  //accept the client socket
      
      System.out.println("Client is Connected");
    }
    }
    /* class MyThread implements Runnable{
  	  Socket s;
  	
  
  	  
  	 MyThread(Socket s2){
  	    this.s=s;
  	  
    }
  	  */
    public void run(){
    	 try{
  din= new DataInputStream(s.getInputStream());
 
  DataOutputStream dout=new DataOutputStream(s.getOutputStream());
 	
    	 }
    	 catch(Exception oe)
    	 {}   	
    }
    
    class MyThread implements Runnable{
    	  Socket s;
    	  
    	  DataInputStream din;
    	  DataOutputStream dout;

    	  MyThread(Socket s){
    	    this.s=s;
    	   
    	  }
    	  public void run(){
    	    String str;
    	  
    	    try{
    	    	
    	    
    	    din=new DataInputStream(s.getInputStream());
    	    }catch(Exception e){}
    	    
    	   {
    	        try{
    	          
    	          str=din.readUTF(); //read the message
    	         
    	        }catch (IOException e){}
    	      }
    	  }
    	  
    	 
    } }
    

 


This post has been edited by macosxnerd101: 18 January 2011 - 07:12 AM
Reason for edit:: Please use code tags

Was This Post Helpful? -1
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,103
  • Joined: 27-December 08

Re: chat application in java

Posted 18 January 2011 - 07:15 AM

What specific problems or errors are you encountering? What is your question?
Was This Post Helpful? 0
  • +
  • -

#5 parita2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 4
  • Joined: 18-January 11

Re: chat application in java

Posted 19 January 2011 - 02:51 AM

Hello Sir,

Thanks for the reply. Actually the problem is that i am not able to connect client with server. Can you guide me how to make chat-server application?

I have to give username and email-id as input, as soon i hit on Login, it should start as a Live Chat application. I am building this Live Chat Application in Applet.

Please help me. Also is there any good sites where i'vl get Live Chat-Server application.

kindly waiting for you reply. :dontgetit:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1