Chat program confusion please help me to solve...

I am beginner at swing.

So using netbeans IDE for making SWING applications.

I developing CHAT server and Chat client.. SO it is two way communications .....

i made serverCHAT window and one send button.

i put code of getting inputStream here but i could not got the

message from client or anything else

button just hangs..

HOw to use threads in this can u please help me out or suggest me any good examples ?

SERVER is multithreaded.

2) Second problem is what format sent by server , client must got in same format

ex.if i sent bold text to client , client got bold text from that side

what should i do ?

PLEASE HELP ME>>>

[712 byte] By [Ghanshyama] at [2007-11-27 0:50:34]
# 1

> I am beginner at swing.

> So using netbeans IDE for making SWING applications.

>

> I developing CHAT server and Chat client.. SO it is

> two way communications .....

>

> i made serverCHAT window and one send button.

> i put code of getting inputStream here but i could

> not got the

> message from client or anything else

>

> button just hangs..

>

>

> HOw to use threads in this can u please help me out

> or suggest me any good examples ?

In java, all the graphics painting and event handling is done by only one thread : EventDispatchThread. Then you must block it with on an accept statement.

What you may do is :

1) Declare a thread to do all the instructions from your EventListener

2) Replace the code of your event listener by :

- Create the Thread

- run the thread

Remember that, on the other side, all modification to your HMI must be done inside using SwingUtilities.invokeLater() if not you may have painting problems.

>

> SERVER is multithreaded.

>

> 2) Second problem is what format sent by server ,

> client must got in same format

> ex.if i sent bold text to client , client got

> bold text from that side

> what should i do ?

Please be more exmplicit, I don't understand.

olivieroa at 2007-7-11 23:20:45 > top of Java-index,Core,Core APIs...
# 2
dear friend..i will explain my problem in detailplease give your email id.I write all socket IO code in my send buttonbut there is no result and buttion hangskindly give me thread c0de to do that..
Ghanshyama at 2007-7-11 23:20:45 > top of Java-index,Core,Core APIs...
# 3
Hello, You can mail me at oetienne {at} chez {dot} comOlivier
olivieroa at 2007-7-11 23:20:45 > top of Java-index,Core,Core APIs...
# 4
These are public forums. Please continue the discussion here so we can all contribute and/or benefit.
ejpa at 2007-7-11 23:20:45 > top of Java-index,Core,Core APIs...
# 5

> I write all socket IO code in my send button

That's precisely the problem. It's an odd choise, consider how tough Java makes it to code in a non-modular fashion.

First, all the socket code should go into a seperate class. Most likely, more than one.

Second, it should go into another thread. The "send" button calls a method in a class for handling the actual chat(socket IO). That method doesn't start any IO, but rather queues the task to a seperate thread.

A simple way to do this is with the aid of java.util.Timer, which allows you to queue sub-classes of TimerTask for sequential execution in a seperate thread.

The actual socket IO stuff would go into the "run" method of your subclass of TimerTask.

This would guarantee that your button won't be stuck. That doesn't mean the communication would work, to fix that you'd have to go into more detail regarding how you actually do the socket IO(code snippets would be helpful), but at least your GUI will remain responsive.

SlugFillera at 2007-7-11 23:20:45 > top of Java-index,Core,Core APIs...
# 6

hi friend,

i you give me good suggestion reagarding task

now my server program is as follows :

GUI ( contains main method ) MyServer.java

ServerThread.java ( contains thread for SOCKET IO )

now i have written all my socket part in ServerThread.java

in run() method

//for server that accepting data not writing actually.

// so we here only include InputStream

public void run()

{

BufferedReader br = new BufferedReader(new InputStreamReader ( clientSocket.getInputStream());

while(true)

String data = br.readLine();

textarea.append(data +"\n");

}

even though i have make separate thread to work on IO and SOcket , my send button stucks..

wht will be the problem yaar

will it solve problem if we writeThread.sleep(100);

to make someinterval to data pass on ?

kindly send me the solution

for both client and server

actually i wanna to make both server & client

bidirectional chat program

please

or send solu @rathod.ghanshyam@gmail.com

Ghanshyama at 2007-7-11 23:20:45 > top of Java-index,Core,Core APIs...
# 7

while (true)

String data = br.readLine();

if (data == null)

break; // EOS

textarea.append(data +"\n");

}

and you should also set a read timeout on the socket before you start reading. (Socket.setSoTimeout()).

ejpa at 2007-7-11 23:20:45 > top of Java-index,Core,Core APIs...