Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Swing socket
I am try to build a client server chat application using swing.In that when i start client i send a message that i can received at client site but when i press a button on Server site it will not open up a new Frame (that is in my case error here).How can i solve it...
And can any one suggest me some other way..
My code is(server site):
 

 
 import java.net.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.sql.*;
    class Cli extends JFrame
    {
       ServerSocket ss;
       Socket soc;
       InputStream in;
       OutputStream out;
 
       DataInputStream sin;
       DataOutputStream sout;
 
       JLabel l1;
       JTextField txt1;
       JButton addd;
      Cli()  throws Exception
      {
	Class.forName("com.mysql.jdbc.Driver");
	Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/nirav","root","root");
 	JPanel pan=new JPanel(new GridLayout(2,1));
	ss=new ServerSocket(8888);
 	//ss.setSOLimit(10000);
	l1=new JLabel("waiting for client");
	txt1=new JTextField(40);
	addd=new JButton("click here to send");
	addd.addActionListener(new ActionListener()
          {
	    public void actionPerformed(ActionEvent	ae) 
			{	try{
					JOptionPane.showMessageDialog(null,"Data is successfully inserted into the database." );	
					l1.setText("waiting");
					Recieve c=new Recieve();
					c.setSize(300,300);	
					c.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
					c.setVisible(true);
					
					}catch(Exception e)
					{
					System.out.println(e.toString());
					}
				}								
			}); 
	soc=ss.accept();
 	in=soc.getInputStream();
 	out=soc.getOutputStream();
	sin=new DataInputStream(in);
	sout=new DataOutputStream(out);	
	String s="";
	try{
		s=sin.readUTF();		
		}catch(Exception e)
		{
		}
		l1.setText("client :"+s);
	pan.add(l1);
	pan.add(txt1);
	pan.add(addd);
	add(pan);
    }
    class Recieve extends JFrame
    {
    JLabel msg;
    JTextField t1;
    JButton a1;
     Recieve() throws Exception
     {	
	JPanel pan=new JPanel(new GridLayout(2,1));
	t1=new JTextField(40);
	a1=new JButton("click here to send");
	//msg=new JLabel("waiting...");
	 a1.addActionListener(new ActionListener()
	{	public void actionPerformed(ActionEvent	ae)
			{	try{
					String s;
					String str="Server :";
					sout.writeUTF(t1.getText());
					//msg.setText(str);
					sout.flush();
				}catch(Exception e)
	{
	System.out.println(e.toString());
	}
	}});
	pan.add(t1);
	pan.add(a1);
	add(pan);
       }
       }
      }
     class Server 
    {	
      public static void main(String args[]) throws Exception
	{
	try{
		
	Cli c=new Cli();
	c.setSize(300,300);
	c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	c.setVisible(true);
	}
	catch(Exception e)
	{
	System.out.println(e.toString());
	}
	
	}
      }
Posted 14-Dec-12 5:12am
Comments
Shubhashish_Mandal at 8-Apr-14 2:47am
   
You have One Server running on port 8888. But I can't see any client which will send the message to that server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 469
1 CHill60 465
2 OriginalGriff 325
3 PIEBALDconsult 285
4 BillWoodruff 193


Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100