Results 1 to 3 of 3
  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Chat, swing & sockets

    Hello everyone,

    I'm working on a java chat and there are some things I would like to discuss with you.
    My chat looks like that:
    On my panel I got a JTextField for the "typing line" (where there user actually chats), and a JTextArea which prints the whole chat.
    Now my question is how to I merge it with the sockets.
    Here is what's on my mind:
    The JTextField will have a keyListener to the "enter" key.
    The actionPerformed method will print first the text from the textField to the TextArea and then it create a thread that will connect to the server by socket, send him the string and then close the connection.
    Opening a connection in the start of the code might be more efficient instead of opening and closing a connection each time a message is sent.
    The big problem for me is what should I do with the server side, I mean I have to listen all the time if any data is recieved.
    Sounds like a perfect match for another thread, but when the thread will get the string from the server how will I send it to the chat which in another class.
    How will I send the data from one class (the one that listen to the server) to another (my chat class).

    I tryed to add a static method in the chat class which recieve as a parameter a string and it will set the text of the textArea.
    But it's impossible to access to the class members since it's a static method.

    Thanks for reading.

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Chat, swing & sockets

    no one?

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Chat, swing & sockets

    Opening a connection in the start of the code might be more efficient instead of opening and closing a connection each time a message is sent.
    Agreed

    I mean I have to listen all the time if any data is recieved.
    Thats what a server socket does, and this is the definition of persistent connections

    Sounds like a perfect match for another thread, but when the thread will get the string from the server how will I send it to the chat which in another class.
    Depends. When I've done this, I use a central server and have a bunch of clients, IRC style. A message sent to the server from any 1 client in the same room forwards it to all the other clients.

    How will I send the data from one class (the one that listen to the server) to another (my chat class).
    Using your sockets. The server has a ServerSocket which just waits for connections. When a connection is made, the ServerSocket returns a Socket. You can save that socket in a list with other sockets. One socket for every client. Each socket has it's own InputStream and OutputStream. So, you listen for input from clients on the Socket input stream and you send the message back out using all of the socket's individual output streams.

    I tryed to add a static method in the chat class which recieve as a parameter a string and it will set the text of the textArea.
    Noooooo! No static! Bad!

    Just use instance methods. Getters and setters for all data you need to share. Start small. Get client to server and back again working first. If you can get one client to talk back and forth, add a second. Get the two clients to talk to each other through the server. If you can get that much working, adding code for n clients shouldn't be much of a leap.

Similar Threads

  1. Sockets
    By Maya in forum Networking
    Replies: 6
    Last Post: 04-19-2011, 11:52 PM
  2. Replies: 4
    Last Post: 03-31-2011, 10:37 PM
  3. Replies: 3
    Last Post: 09-22-2010, 07:28 PM
  4. voice chat in java with sockets
    By krati in forum Networking
    Replies: 0
    Last Post: 05-12-2010, 09:17 AM
  5. tcp client server chat usinh applets/swing
    By klakshmi91 in forum Networking
    Replies: 0
    Last Post: 03-25-2010, 04:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •