GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Mar-2006, 10:25
shereen shereen is offline
New Member
 
Join Date: Mar 2006
Posts: 2
shereen is on a distinguished road

Multi-client application using sockets


i make code that it for one client for make chat room it take what
client said and type it in text file but i have problem that i want
now to make multiclient using the socket .what i know that ineed
thread but i donot know how to do that
the code for the server is

JAVA Code:
import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.*; 
import java.awt.event.*; 
import java.io.*; 
import java.awt.Window.*; 
import java.net.*; 

class Serverchat extends JFrame implements ActionListener,Runnable 

{ 
Color color = Color.blue; 
String s,tete,tete1; 
ServerSocket Sserver; 
Socket SSoket; 
BufferedReader br; 
BufferedWriter bw; 

//------------panel---------- 
JPanel panel = new JPanel(); 
JPanel panel2 = new JPanel(); 
//------------TextArea------- 
List ta = new List(); 

JTextArea tfiled = new JTextArea(3,30); 
//------------ScrollBar------- 
JScrollPane sp = new JScrollPane(ta); 
JScrollPane sp1 = new JScrollPane(tfiled); 
//------------TollBar---------- 
JToolBar tools = new JToolBar(); 
String names[] ={"1","2" }; 
JComboBox fontsize = new JComboBox(names); 

ImageIcon asave1 =new ImageIcon("pic/save.gif"); 
JButton save1 = new JButton(asave1); 

ImageIcon tsize =new ImageIcon("pic/bullets.gif"); 
JButton tsize1 = new JButton(tsize); 

ImageIcon acolor1 =new ImageIcon("pic/fg.gif"); 
JButton color1 = new JButton(acolor1); 

JButton gemy =new JButton("Gemy Color"); 
ImageIcon psend1 =new ImageIcon("pic/se.gif"); 
JButton psend =new JButton(psend1); 
//============MenuBar========= 
JMenuBar menu = new JMenuBar(); 
//===========File Menu======== 
JMenu file = new JMenu("File"); 
//----------MenuItem------- 


JMenuItem save = new JMenuItem("Save"); 

JMenuItem exit = new JMenuItem("Exit"); 


//=========Font Menu======== 
JMenu font = new JMenu("Font"); 
//----------MenuItem------- 
JMenuItem size = new JMenuItem("Size"); 
JMenuItem fcolor = new JMenuItem("Font Color"); 

JMenu help = new JMenu("Help"); 
//----------MenuItem------- 
JMenuItem about = new JMenuItem("About"); 
//-------------------End Menu----------------- 
public void run() 
{ 
try{SSoket.setSoTimeout(1);}catch(Exception e){} 
while (true) 
{ 
try{ 
ta.addItem(br.readLine()); 
}catch (Exception h){} 
} 
} 


Serverchat(String nn) 
{ 
super (nn); 

setupGui(); 
psend.addActionListener(this); 

addWindowListener(new WindowAdapter() 
{ 
public void windowClosing(WindowEvent e) 
{ 
System.exit(0); 
} 
}); 
try{ 
Sserver = new ServerSocket(100); 

SSoket=Sserver.accept(); 
br = new BufferedReader(new InputStreamReader( 
SSoket.getInputStream())); 
bw = new BufferedWriter(new OutputStreamWriter( 
SSoket.getOutputStream())); 
File out=new File("output.txt"); 
FileWriter fw=null; 
PrintWriter pw=null; 
fw=new FileWriter(out); 
pw=new PrintWriter(fw); 
String s; 
while((s=br.readLine())!=null) 
{ 
pw.println(s); 
pw.flush(); 
} 
br.close(); 
fw.close(); 
pw.close(); 

/*bw.write("Hello")*/;bw.newLine();bw.flush(); 
Thread th; 
th = new Thread(this); 
th.start(); 


}catch(Exception e){} 
} 
public void actionPerformed ( ActionEvent e) 
{ 
/*if (e.getSource().equals(button2)) 
System.exit(0); 
else{*/try{ 
bw.write(tfiled.getText()); 
String mywrite=tfiled.getText(); 
ta.addItem(mywrite); 
System.out.println(mywrite); 
bw.newLine();bw.flush(); 
tfiled.setText(""); 
}catch(Exception m){} 


} 



private void setupGui() 
{ 
//ta.disable(); 

fontsize.setMaximumRowCount(2); 
fontsize.setBounds(1,2,3,4); 
//fontsize.setBackground(color.BLACK); 
//============container============= 
Container c = getContentPane(); 


c.setLayout(new BorderLayout()); 
panel.setLayout(new BorderLayout()); 
//===============Menu Items============ 
menu.add(file); 

menu.add(font); 
menu.add(help); 
//------------file------------------- 

file.add(save); 

file.add(exit); 

//------------font------------------ 
font.add(size); 
font.add(fcolor); 
//------------help------------------ 
help.add(about); 
//------------tools------------------ 
tools.add(save1); 
tools.add(fontsize); 
tools.add(color1); 
tools.add(gemy); 

//**************************************************** 



about.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JOptionPane.showMessageDialog(null,"tank you"); 
} 
}); 

color1.addActionListener(new ActionListener() { 
public void actionPerformed( ActionEvent e ) 
{ 
color = 
JColorChooser.showDialog( Serverchat.this, 
"Choose a color", color ); 

if ( color == null ) 
color = Color.lightGray; 


tfiled.setForeground(color); 
tfiled.repaint(); 
} 
} 
); 

fcolor.addActionListener(new ActionListener() { 
public void actionPerformed( ActionEvent e ) 
{ 
color = 
JColorChooser.showDialog( Serverchat.this, 
"Choose a color", color ); 

if ( color == null ) 
color = Color.lightGray; 


tfiled.setForeground(color); 
tfiled.repaint(); 
} 
} 
); 
//tool.getDefaultToolkit(); 
exit.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JOptionPane.showConfirmDialog(null,"Do you really want to exit!"); 
System.exit(0); 
} 
}); 
tsize1.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
tfiled.setFont(new Font("Tahoma",Font.ITALIC,14)); 
} 
}); 
gemy.setBounds(500,500,70,30); 



s= gemy.getActionCommand(); 
gemy.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
if( s.equals("Gemy Color")) 
ta.setBackground(new Color((int)(Math.random()*255),(int)(Math.random
()*255),(int)(Math.random()*255))); 
} 
}); 


save1.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) { 
JFileChooser chooser= new JFileChooser(); 
Frame frame = new Frame(); 

FileDialog fileDialog = new FileDialog(frame); 

fileDialog.setMode(FileDialog.SAVE); 
fileDialog.show(); 
tete=fileDialog.getDirectory(); 
tete1=fileDialog.getFile(); 
System.out.println(tete+tete1); 
try{ 
File file =new File(tete+tete1); 

file.createNewFile(); 


// Write to File 
FileOutputStream filelow = new FileOutputStream(file); 

DataOutputStream datahigh= new DataOutputStream(filelow); 
String infile[]=ta.getItems(); 
// datahigh.writeUTF(infile); 
} 

catch(IOException i) 
{ 
i.printStackTrace(); 

} 

} 
}); 


save.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) { 
JFileChooser chooser= new JFileChooser(); 
Frame frame = new Frame(); 

FileDialog fileDialog = new FileDialog(frame); 

fileDialog.setMode(FileDialog.SAVE); 
fileDialog.show(); 
tete=fileDialog.getDirectory(); 
tete1=fileDialog.getFile(); 
System.out.println(tete+tete1); 
try{ 
File file =new File(tete+tete1); 

file.createNewFile(); 


// Write to File 
FileOutputStream filelow = new FileOutputStream(file); 

DataOutputStream datahigh= new DataOutputStream(filelow); 
String infile[]=ta.getItems(); 
//datahigh.writeUTF(infile); 
} 

catch(IOException i) 
{ 
i.printStackTrace(); 

} 

} 
}); 
//**************************************************** 

c.add("Center" ,new JScrollPane(ta)); 
c.add("North" ,panel); 
c.add("South" ,panel2); 
panel.add("North" ,menu); 
panel.add("South" ,tools); 
panel2.add("East" ,sp1); 
panel2.add("West" ,psend); 
//======================== 
setSize(500,500); 
//setVisible(true); 


} 


public static void main(String[]arg) 
{ 
Serverchat z = new Serverchat("Gamal Chat"); 
} 






} 
the code of client is
JAVA Code:
import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.*; 
import java.awt.event.*; 
import java.io.*; 
import java.awt.Window.*; 
import java.net.*; 

class clientchat extends JFrame implements ActionListener,Runnable 

{ 
Color color = Color.blue; 
String s,tete,tete1; 
ServerSocket Sserver; 
Socket SSoket; 
BufferedReader br; 
BufferedWriter bw; 
JScrollPane scrolli; 
//------------panel---------- 
JPanel panel = new JPanel(); 
JPanel panel2 = new JPanel(); 
//------------TextArea------- 
List ta = new List(); 

JTextArea tfiled = new JTextArea(3,30); 
//------------ScrollBar------- 
JScrollPane sp = new JScrollPane(ta); 
JScrollPane sp1 = new JScrollPane(tfiled); 
//------------TollBar---------- 
JToolBar tools = new JToolBar(); 

ImageIcon asave1 =new ImageIcon("pic/save.gif"); 
JButton save1 = new JButton(asave1); 

ImageIcon tsize =new ImageIcon("pic/bullets.gif"); 
JButton tsize1 = new JButton(tsize); 

ImageIcon acolor1 =new ImageIcon("pic/fg.gif"); 
JButton color1 = new JButton(acolor1); 

JButton gemy =new JButton("Gemy Color"); 
ImageIcon psend1 =new ImageIcon("pic/se.gif"); 
JButton psend =new JButton(psend1); 

ImageIcon ghd =new ImageIcon("pic/1.gif"); 
JLabel ghd1=new JLabel (ghd); 

ImageIcon ghdd =new ImageIcon("pic/2.gif"); 
JLabel ghd2=new JLabel (ghdd); 

ImageIcon ghddd =new ImageIcon("pic/3.jpg"); 
JLabel ghd3=new JLabel (ghddd); 

ImageIcon ghdddd =new ImageIcon("pic/4.jpg"); 
JLabel ghd4=new JLabel (ghdddd); 

ImageIcon ghddddd =new ImageIcon("pic/5.jpg"); 
JLabel ghd5=new JLabel (ghddddd); 

//============MenuBar========= 
JMenuBar menu = new JMenuBar(); 
//===========File Menu======== 
JMenu file = new JMenu("File"); 
//----------MenuItem------- 




JMenuItem save = new JMenuItem("Save"); 

JMenuItem exit = new JMenuItem("Exit"); 


//=========Font Menu======== 
JMenu font = new JMenu("Font"); 
//----------MenuItem------- 
JMenuItem size = new JMenuItem("Size"); 
JMenuItem fcolor = new JMenuItem("Font Color"); 

JMenu help = new JMenu("Help"); 
//----------MenuItem------- 
JMenuItem about = new JMenuItem("About"); 

//------------font size 

JMenu fontss = new JMenu("Font Size"); 
//----------MenuItem------- 
JMenuItem size12 = new JMenuItem("12"); 
JMenuItem size14 = new JMenuItem("14"); 
JMenuItem size16 = new JMenuItem("16"); 
// JMenuItem size18 = new JMenuItem("18"); 
// JMenuItem size20 = new JMenuItem("20"); 
// JMenuItem size22 = new JMenuItem("22"); 
// JMenuItem size24 = new JMenuItem("24"); 
// JMenuItem size26 = new JMenuItem("26"); 
//-------------------End Menu----------------- 
public void run() 
{ 
try{SSoket.setSoTimeout(1);}catch(Exception e){} 
while (true) 
{ 
try{ 
ta.addItem(br.readLine()); 
}catch (Exception h){} 
} 
} 


clientchat(String nn) 
{ 
super (nn); 

setupGui(); 
psend.addActionListener(this); 

addWindowListener(new WindowAdapter() 
{ 
public void windowClosing(WindowEvent e) 
{ 
System.exit(0); 
} 
}); 
try{ 


SSoket= new Socket("127.0.0.1",100); 
br = new BufferedReader(new InputStreamReader( 
SSoket.getInputStream())); 
bw = new BufferedWriter(new OutputStreamWriter( 
SSoket.getOutputStream())); 
/* bw.write("Hello");*/bw.newLine();bw.flush(); 
Thread th; 
th = new Thread(this); 
th.start(); 


}catch(Exception e){} 
} 
public void actionPerformed ( ActionEvent e) 
{ 
/*if (e.getSource().equals(button2)) 
System.exit(0); 
else{*/try{ 
bw.write(tfiled.getText()); 
String mywrite=tfiled.getText(); 
ta.addItem(mywrite); 
System.out.println(mywrite); 
fileex s=new fileex(); 
bw.newLine();bw.flush(); 
tfiled.setText(""); 
}catch(Exception m){} 


} 



private void setupGui() 
{ 
//ta.disable(); 


//============container============= 
Container c = getContentPane(); 


c.setLayout(new BorderLayout()); 
panel.setLayout(new BorderLayout()); 
scrolli=new JScrollPane( ); 
scrolli.setHorizontalScrollBarPolicy
(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
//===============Menu Items============ 
menu.add(file); 

menu.add(font); 
menu.add(fontss); 
menu.add(help); 
//------------file------------------- 

file.add(save); 

file.add(exit); 
//666666666666666666666666 
fontss.add(size12); 
fontss.add(size14); 
fontss.add(size16); 
// fontss.add(size18); 
// fontss.add(size20); 
// fontss.add(size22); 
// fontss.add(size24); 
// fontss.add(size26); 

//------------font------------------ 
font.add(size); 
font.add(fcolor); 
//------------help------------------ 
help.add(about); 
//------------tools------------------ 
tools.add(save1); 

tools.add(color1); 
tools.add(gemy); 

ghd1.setSize(2,2); 
tools.add(ghd1); 
tools.add(ghd2); 
tools.add(ghd3); 
tools.add(ghd4); 
tools.add(ghd5); 
ta.disable(); 


//**************************************************** 

//*******************************fffffffff 
size12.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
tfiled.setFont(new Font("Tahoma",Font.ITALIC,12)); 
ta.setFont(new Font("Tahoma",Font.ITALIC,12)); 
//ta.clear(); 
} 
}); 

size14.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
tfiled.setFont(new Font("Tahoma",Font.ITALIC,14)); 
ta.setFont(new Font("Tahoma",Font.ITALIC,14)); 
} 
}); 
//***************FFFFFFFFFFFFFFFFFFFFFFF 


about.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JOptionPane.showInternalConfirmDialog(null,""); 
} 
}); 

color1.addActionListener(new ActionListener() { 
public void actionPerformed( ActionEvent e ) 
{ 
color = 
JColorChooser.showDialog( clientchat.this, 
"Choose a color", color ); 

if ( color == null ) 
color = Color.lightGray; 


tfiled.setForeground(color); 
tfiled.repaint(); 
} 
} 
); 

fcolor.addActionListener(new ActionListener() { 
public void actionPerformed( ActionEvent e ) 
{ 
color = 
JColorChooser.showDialog( clientchat.this, 
"Choose a color", color ); 

if ( color == null ) 
color = Color.lightGray; 


tfiled.setForeground(color); 
tfiled.repaint(); 
} 
} 
); 
//tool.getDefaultToolkit(); 
exit.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JOptionPane.showConfirmDialog(null,"Do you really want to exit!"); 
System.exit(0); 
} 
}); 
tsize1.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
tfiled.setFont(new Font("Tahoma",Font.ITALIC,14)); 
} 
}); 
gemy.setBounds(500,500,70,30); 



s= gemy.getActionCommand(); 
gemy.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
if( s.equals("Gemy Color")) 
ta.setBackground(new Color((int)(Math.random()*255),(int)(Math.random
()*255),(int)(Math.random()*255))); 
} 
}); 


save1.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) { 
JFileChooser chooser= new JFileChooser(); 
Frame frame = new Frame(); 

FileDialog fileDialog = new FileDialog(frame); 

fileDialog.setMode(FileDialog.SAVE); 
fileDialog.show(); 
tete=fileDialog.getDirectory(); 
tete1=fileDialog.getFile(); 
System.out.println(tete+tete1); 
try{ 
File file =new File(tete+tete1); 

file.createNewFile(); 


// Write to File 
FileOutputStream filelow = new FileOutputStream(file); 

DataOutputStream datahigh= new DataOutputStream(filelow); 
String infile[]=ta.getItems(); 

//datahigh.writeUTF(infile); 
} 

catch(IOException i) 
{ 
i.printStackTrace(); 

} 

} 
}); 


save.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) { 
JFileChooser chooser= new JFileChooser(); 
Frame frame = new Frame(); 

FileDialog fileDialog = new FileDialog(frame); 

fileDialog.setMode(FileDialog.SAVE); 
fileDialog.show(); 
tete=fileDialog.getDirectory(); 
tete1=fileDialog.getFile(); 
System.out.println(tete+tete1); 
try{ 
File file =new File(tete+tete1); 

file.createNewFile(); 


// Write to File 
FileOutputStream filelow = new FileOutputStream(file); 

DataOutputStream datahigh= new DataOutputStream(filelow); 
String infile[]=ta.getItems(); 
//datahigh.writeUTF(infile); 
} 

catch(IOException i) 
{ 
i.printStackTrace(); 

} 

} 
}); 
//**************************************************** 

c.add("Center" ,new JScrollPane(ta)); 
c.add("North" ,panel); 
c.add("South" ,panel2); 
panel.add("North" ,menu); 
panel.add("South" ,tools); 
panel2.add("Center" ,sp1); 
panel2.add("West" ,psend); 

//======================== 
setSize(500,500); 
setVisible(true); 


} 


public static void main(String[]arg) 
{ 
clientchat z = new clientchat("Gamal Chat Client"); 
} 






} 
class fileex 
{ 
public static void main(String[ ] args) 
{ 
// File in=new File("input.txt"); 
File out=new File("output.txt"); 
// FileReader fr=null; 
// BufferedReader br=null; 
FileWriter fw=null; 
PrintWriter pw=null; 
try 
{InputStream is =System.in; 
InputStreamReader isr=new InputStreamReader(is); 
BufferedReader br=new BufferedReader(isr); 
// fr=new FileReader (in); 
// br=new BufferedReader(fr); 
fw=new FileWriter(out); 
pw=new PrintWriter(fw); 
String s; 
while((s=br.readLine())!=null) 
{ 
pw.println(s); 
pw.flush(); 
} 
br.close(); 
fw.close(); 
pw.close(); 
} 
catch(IOException e) 
{ 
System.out.println(e); 
} 
} 
} 

both are run without error but my problem
1- the output text file is for only the first client
2- i donot know how to modified my program to run multiclient
Last edited by LuciWiz : 03-Mar-2006 at 11:01. Reason: Please insert your Java code between [java] & [/java] tags; Edited title (was "i need immediately help")
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Calendar application for smart device problem adie_w .NET Forum 0 15-Feb-2006 05:13
convert win32 console application to win32 application jaro C Programming Language 3 17-Nov-2005 03:35
[Program/Tutorial] Sockets to send messages L7Sqr C Programming Language 0 27-Jul-2005 12:27
Basic UNIX IPC and BSD Sockets Chris.Dev C Programming Language 1 22-Feb-2005 17:15
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 04:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.