Server client prob..
HI
While i created a server (gui) and client (cui) side program for chat ..
all of them working fine..but the problem is when the client send some strings to server textarea , i cant see it at that right time it sents when i press ctrl c in client side then that window appears on the screen...
I think this info make u understand wat i mean...
Thanx...
[384 byte] By [
Reona] at [2007-11-27 4:55:12]

> I think this info make u understand wat i mean...No, it's very hard to understand your problem unless you show some code. The problem can be that you are creating the socket, and performs reading etc from it within the AWT thread.Kaj
kajbja at 2007-7-12 10:10:05 >

here the code is ...
this is server side code for chatting...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class S
{
static JButton button;
static JTextArea textarea;
static JFrame frame;
static JPanel panel;
static String line;
static String lineout;
static DataOutputStream out;
public static void main(String args[]) throws Exception
{
try
{
frame = new JFrame ("Server Side chat");;
panel = new JPanel();
JLabel label = new JLabel("");
textarea = new JTextArea(20,20);
button = new JButton("Send to Client !!!");
int port = 1234;
ServerSocket ss= new ServerSocket(port);
JOptionPane.showMessageDialog(frame,"Waiting for client !!!!","Server",JOptionPane.PLAIN_MESSAGE);
Socket socket = ss.accept();
label = new JLabel("Got a client !!!!");
//JOptionPane.showMessageDialog(frame,"Got a Client !!!!","Server",JOptionPane.PLAIN_MESSAGE);
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
out = new DataOutputStream(sout);
while(true)
{
line = in.readUTF();
textarea.setText("From Client : " + line );
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
try{
lineout = textarea.getText();
out.writeUTF(lineout);
out.flush();
}
catch(Exception e) {e.printStackTrace();}
}
});
}
}
catch(Exception e)
{
e.printStackTrace();
}
frame.getContentPane().add(panel);
panel.add(textarea);
panel.add(button);
frame.setSize(300,500);
frame.setVisible(true);
}
}
I think this code understand u wat i mean..
the problem here is while i send some strings from client side it is not showing any thing i mean any frame is not visible there....
i am using windows and while i press ctrl+c in client side command prompt..
then the gui window of server side will appear and we can see that string that was passed from the client , inside textarea....
Did u wish to see client side program...
Reona at 2007-7-12 10:10:05 >

Can't really tell what you are trying to do from the posted code. It doesn't really make any sense.
You have a "while(true)" statement. Inside that code you add an ActionListener to the button. So every time you loop you will be adding another ActionListener. Then you try to show the GUI.
Well the GUI code should be separate from the client/server code. You build the GUI and display it. Then you have a separate Thread that sends/receives messages to/from the client.
That's because you are getting an exception when the client close the connection, and you are displaying the frame after you have read the data. Kaj
kajbja at 2007-7-12 10:10:05 >

when i replaced frame it gets ok...
But now the problem is client can send as many messages as it can but the server can send only 3 messages to client...
Dont know wats hapening, in the code i posted in above post...
but camickr "While(true)"
i tried to but that button outside 'while(true)' but wat happened there is it -- showing errors....
Reona at 2007-7-12 10:10:05 >

> when i replaced frame it gets ok...
> But now the problem is client can send as many
> messages as it can but the server can send only 3
> messages to client...
> Dont know wats hapening, in the code i posted in
> above post...
>
> but camickr "While(true)"
> i tried to but that button outside 'while(true)' but
> wat happened there is it -- showing errors....
You probably need to step back. Take a pen and paper and think about your design. Try to breakup the code into smaller methods. Can you now see why it isn't working as expected?
Kaj
kajbja at 2007-7-12 10:10:05 >

ya actually it is coded it for cui ,after completely coded in cui...I think how is it possible to put this idea in gui...cui code works fine....!!then please tell me...how can i try to break the code in smaller methods, bcoz i tried it in cui first ?
Reona at 2007-7-12 10:10:05 >

> ya actually it is coded it for cui ,after completely
> coded in cui...
> I think how is it possible to put this idea in
> gui...
> cui code works fine....!!
> then please tell me...how can i try to break the code
> in smaller methods, bcoz i tried it in cui first ?
Whas is cui?
kajbja at 2007-7-12 10:10:05 >

character user interface -- cuiand gui graphical user interface...
Reona at 2007-7-12 10:10:05 >

> HI
> While i created a server (gui) and client (cui) side
> program for chat ..
> all of them working fine..but the problem is when the
> client send some strings to server textarea , i cant
> see it at that right time it sents when i press ctrl
> c in client side then that window appears on the
> screen...
I suspect this translates into that you are not flushing the socket on the client side.
> character user interface -- cui> and gui graphical user interface...Hmm.. I don't see why it would it maker harder to create short methods just because it used to have a character based ui.
kajbja at 2007-7-12 10:10:05 >

> > HI
> > While i created a server (gui) and client (cui)
> side
> > program for chat ..
> > all of them working fine..but the problem is when
> the
> > client send some strings to server textarea , i
> cant
> > see it at that right time it sents when i press
> ctrl
> > c in client side then that window appears on the
> > screen...
>
> I suspect this translates into that you are not
> flushing the socket on the client side.
Further I would drop the DataInput and DataOutput streams and just use PrintWriters and BufferedReaders. If you are just sending character data back and forth then readers and writers are more appropriate.
out.flush();
import java.net.*;
import java.io.*;
public class C {
public static void main(String[] ar) {
int serverPort = 1234;
String address = "127.0.0.1";
try {
InetAddress ipAddress = InetAddress.getByName(address);
System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
Socket socket = new Socket(ipAddress, serverPort);
System.out.println("Yes! I just got hold of the program.");
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
System.out.println();
while(true) {
line = keyboard.readLine();
System.out.println("Sending this line to the server...");
out.writeUTF(line);
out.flush();
line = in.readUTF();
System.out.println("The server was very polite. It sent me this : " + line);
System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
System.out.println();
}
} catch(Exception x) {
x.printStackTrace();
}
}
}
Reona at 2007-7-12 10:10:05 >

