Click here to Skip to main content
15,919,434 members
Home / Discussions / Article Writing
   

Article Writing

 
Generalarticles in threading Pin
BicycleTheif29-Apr-05 0:28
BicycleTheif29-Apr-05 0:28 
GeneralRe: articles in threading Pin
ThatsAlok29-Apr-05 2:26
ThatsAlok29-Apr-05 2:26 
GeneralCrystal Reports & rdl Pin
tvprithiv18-Apr-05 1:56
tvprithiv18-Apr-05 1:56 
QuestionIs this article useful on the code project? Pin
User 19428914-Apr-05 23:36
User 19428914-Apr-05 23:36 
AnswerRe: Is this article useful on the code project? Pin
DavidNohejl15-Apr-05 1:25
DavidNohejl15-Apr-05 1:25 
GeneralRe: Is this article useful on the code project? Pin
User 19428910-May-05 3:33
User 19428910-May-05 3:33 
Generalftp tutorial Pin
V.G14-Apr-05 18:48
V.G14-Apr-05 18:48 
Generaljava chat application Pin
gizzle12311-Apr-05 9:45
gizzle12311-Apr-05 9:45 
I'm trying to make a chat application for an assignment i have......
i'm using java .........
i have 2 files... 1 for the server and a next for the client

the client works fine....i can send messages to the server.........
but i cant send messages to the client from the server........

i'm asking for any help possible.............

here is the SERVER CODE

//set up the gizzle server that will recieve packets from a client

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class GizzleServer extends JFrame implements ActionListener
{
private JTextArea display;
private JTextField sendMessage;
private JButton sendButton;
private JButton resetButton;
private JLabel javaLabel;
private Color colorValues[]=
{ Color.black,Color.blue,Color.red,Color.green};
private JRadioButtonMenuItem colorItems[],fonts[];
private JCheckBoxMenuItem styleItems[];


private DatagramPacket sendPacket,receivePacket;
private DatagramSocket socket;


public GizzleServer()
{
super("Gizzle Chatter Box Server");


Container c = getContentPane();



//creating IconLabels
/*
Icon javaCup = new ImageIcon("javaIcon.jpg");
javaLabel= new JLabel("java Messenger",javaCup);
javaLabel.setToolTipText("java Messenger");
c.add(javaLabel,BorderLayout.EAST);
*/

//creating buttons
Icon send = new ImageIcon("sendButton.jpg");
Icon sendRollover = new ImageIcon("sendButtonRollover.jpg");
Icon reset = new ImageIcon("resetButton.jpg");

sendButton = new JButton("Send ",send);
sendButton.setRolloverIcon(sendRollover);
resetButton = new JButton("Reset",reset);
c.add(resetButton,BorderLayout.SOUTH);
c.add(sendButton,BorderLayout.WEST);
sendMessage = new JTextField("Server User Type the Message Here");

sendMessage.addActionListener(this);

getContentPane().add(sendMessage,BorderLayout.NORTH);


display = new JTextArea();
getContentPane().add(new JScrollPane(display),BorderLayout.CENTER);

setSize(500,400);
show();

try
{
socket = new DatagramSocket(5000);
} //end of try

catch(SocketException se)
{
se.printStackTrace();
System.exit(1);
}
} //end of constructor


public void waitForPackets()
{
while (true)
{
try
{
//set up packet
byte data[] = new byte[100];
receivePacket = new DatagramPacket(data,data.length);

//wait for packet
socket.receive(receivePacket);

//process packet

display.append("\n" +
"\nFrom Gizzle : " + receivePacket.getAddress() +
"\nGizzle says>>>> \n\t" +
new String(receivePacket.getData(),0,receivePacket.getLength() ) );
display.setCaretPosition(display.getText().length() );


//echo information from back to client

display.append("\n\n Data to Client...");
sendPacket= new DatagramPacket(receivePacket.getData(),receivePacket.getLength(),receivePacket.getAddress(),receivePacket.getPort() );

socket.send(sendPacket);
display.append("Message Succesfull\n");
display.setCaretPosition(display.getText().length() );

}//end of try

catch(IOException io)
{
display.append(io.toString() + "\n" );
io.printStackTrace();

} //end of catch

} //end of while loop
} //end of waitForPackets

public void actionPerformed(ActionEvent e)
{
try
{
display.append("Message being sent" + e.getActionCommand() +"\n");

String s = e.getActionCommand();
byte data[] = s.getBytes();

sendPacket = new DatagramPacket(data,data.length,InetAddress.getLocalHost(),5000);
socket.send(sendPacket);
display.append("Message Sent\n");
display.setCaretPosition(display.getText().length() );

} //end of try

catch(IOException exception)
{
display.append(exception.toString() + "\n");
exception.printStackTrace();
}
}


public static void main(String args[])
{
GizzleServer app = new GizzleServer();

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

app.waitForPackets();
}//end of p.s.v.m
}//end of main class




THIS IS THE CLIENT CODE

//setup for gizzle client
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class GizzleClient extends JFrame implements ActionListener
{
private JTextField enter;
private JTextArea display;
private JButton sendButton;
private JButton resetButton;
private Color colorValues[]=
{ Color.black,Color.blue,Color.red,Color.green};
private JRadioButtonMenuItem colorItems[],fonts[];
private JCheckBoxMenuItem styleItems[];
private ButtonGroup fontGroup,colorGroup;
private int style;

private DatagramPacket sendPacket,receivePacket;
private DatagramSocket socket;

public GizzleClient()
{
super("Gizzle Chatter Box Messenger v1.0");

Container c = getContentPane();


//creating buttons
Icon send = new ImageIcon("sendButton.jpg");
Icon sendRollover = new ImageIcon("sendButtonRollover.jpg");
Icon reset = new ImageIcon("resetButton.jpg");

sendButton = new JButton("Send ",send);
sendButton.setRolloverIcon(sendRollover);
resetButton = new JButton("Reset",reset);
c.add(resetButton,BorderLayout.SOUTH);
c.add(sendButton,BorderLayout.WEST);
sendButton.addActionListener(this);

JMenuBar bar = new JMenuBar(); //creating Menu Bar
setJMenuBar(bar); //set the menubar for the JFrame

//create File menu annd Exit menu item

JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem aboutItem= new JMenuItem("About Gizzle Chatter...");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"This is Gizzle's Chatter Box V1.0","About Chatter Box",JOptionPane.PLAIN_MESSAGE);
}
}//end of new actionListener
); //end of addActionlistener


fileMenu.add(aboutItem);
bar.add(fileMenu); //add File Menu

JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('x');
exitItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"GoodBye","Gizzle Chatter Box",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
);
fileMenu.add(exitItem);



//create the Format menu

JMenu formatMenu = new JMenu("Look & Feel");
formatMenu.setMnemonic('r');

//creating the COlor submenu

String colors[]= { "Black","Blue","Red","Green"};

JMenu colorMenu = new JMenu("Background Color");
colorMenu.setMnemonic('C');
colorItems= new JRadioButtonMenuItem[colors.length];
colorGroup = new ButtonGroup();

ItemHandler itemHandler = new ItemHandler();

for (int i=0;i<colors.length;i++)
{
="" coloritems[i]="new" jradiobuttonmenuitem(colors[i]);
="" colormenu.add(coloritems[i]);
="" colorgroup.add(coloritems[i]);
="" coloritems[i].addactionlistener(itemhandler);
="" }="" end="" of="" for="" loop

="" coloritems[0].setselected(true);
="" formatmenu.add(colormenu);
="" formatmenu.addseparator();
=""
=""

="" formatmenu.add(fontmenu);
="" bar.add(formatmenu);

="" text="" field="" box
="" enter="new" jtextfield("user="" type="" your="" message="" here");
="" enter.addactionlistener(this);
="" getcontentpane().add(enter,borderlayout.north);

="" display="new" jtextarea();
="" display.setforeground(colorvalues[0]);
="" getcontentpane().add(new="" jscrollpane(display),borderlayout.center);
="" setsize(500,400);
="" show();
="" try
="" socket="new" datagramsocket();
="" }
="" catch(socketexception="" se)="" se.printstacktrace();
="" system.exit(1);
="" constructor




="" public="" void="" waitforpackets()
="" while(true)
="" byte="" data[]="new" byte[100];
="" receivepacket="new" datagrampacket(data,data.length);
="" wait="" packet
="" socket.receive(receivepacket);
="" process="" display.append("\nmessage="" received="" from="" server:---------------------="">>> "+
"\nTo Server: " + receivePacket.getAddress() +
"\nMessage Sent>>>>\n\n\n\n\t " +
new String(receivePacket.getData(),0,receivePacket.getLength() ) );
display.setCaretPosition(display.getText().length() );

} //end of try

catch(IOException exception)
{
display.append(exception.toString() + "\n");
exception.printStackTrace();
} //end of catch
} //end of while loop
} //end of waitForPackets()

public void actionPerformed(ActionEvent e)
{
/*
if (e.getSource()==enter)
{

enter="message here");
}*/

try
{
display.append("\nMessage being sent>>>>" + e.getActionCommand() + "\n");
String s = e.getActionCommand();
byte data[] = s.getBytes();

sendPacket = new DatagramPacket(data,data.length, InetAddress.getLocalHost(),5000);
socket.send(sendPacket);
display.append("Message sent\n");
display.setCaretPosition(display.getText().length());
} //end of try

catch(IOException exception)
{
display.append(exception.toString() + "\n");
exception.printStackTrace();
} //end of catch
} //end of actionPerformed


public static void main(String args[])
{
GizzleClient app= new GizzleClient();

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

class ItemHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
for(int i=0;i
GeneralDesktop Sharing / Application Sharing Pin
Dittto217-Apr-05 13:46
Dittto217-Apr-05 13:46 
GeneralRe: Desktop Sharing / Application Sharing Pin
Ray Cassick13-Apr-05 7:58
Ray Cassick13-Apr-05 7:58 
GeneralRe: Desktop Sharing / Application Sharing Pin
DeepToot20-Apr-05 15:55
DeepToot20-Apr-05 15:55 
GeneralGenerating patterns from a collection of strings Pin
ShajeeDotNet7-Apr-05 5:12
ShajeeDotNet7-Apr-05 5:12 
Generalemail client Pin
_tasleem5-Apr-05 10:45
_tasleem5-Apr-05 10:45 
GeneralC# Speech Recognition Pin
Anonymous5-Apr-05 8:20
Anonymous5-Apr-05 8:20 
Generalabout crystal report Pin
satishrg4-Apr-05 17:32
satishrg4-Apr-05 17:32 
GeneralRe: about crystal report Pin
Claudio Grazioli4-Apr-05 20:37
Claudio Grazioli4-Apr-05 20:37 
GeneralREQ: Article on Extending .NET System Classes (C#) Pin
rts@Dallas3-Apr-05 6:31
rts@Dallas3-Apr-05 6:31 
AnswerRe: REQ: Article on Extending .NET System Classes (C#) Pin
peterchen7-Aug-05 8:05
peterchen7-Aug-05 8:05 
GeneralAutoScale Pin
ACorbs28-Mar-05 20:37
ACorbs28-Mar-05 20:37 
QuestionArticle Help: Should I split my article? Pin
oshah26-Mar-05 1:09
oshah26-Mar-05 1:09 
AnswerRe: Article Help: Should I split my article? Pin
Michael Dunn26-Mar-05 10:02
sitebuilderMichael Dunn26-Mar-05 10:02 
GeneralRe: Article Help: Should I split my article? Pin
oshah26-Mar-05 11:02
oshah26-Mar-05 11:02 
GeneralPlease post your valuable comment. Pin
sreejith ss nair17-Mar-05 20:04
sreejith ss nair17-Mar-05 20:04 
GeneralRe: Please post your valuable comment. Pin
Renjith Ramachandran22-Mar-05 15:54
Renjith Ramachandran22-Mar-05 15:54 
GeneralDatabase connection without DSN Pin
Renjith Ramachandran13-Mar-05 23:59
Renjith Ramachandran13-Mar-05 23:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.