Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

[SOLVED] Chat Client/server Problem


  • This topic is locked This topic is locked
4 replies to this topic

#1 sandmaster


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 04 July 2012 - 04:08 AM

Hi there!

I am gonna do a chat client which has an interface, and when the user presses enter the line of text in the textfield should be sent to the server and then to the textarea which is under the textfield, lets say the same look like skype or windows live messenger only the textfield in which you enter your message is above instead of below.

The Problem I have is that when i press enter, the message is sent to the server (not made the server so it's not on that end) and then back to the textarea. now to the problem: when i send the first message everything works as it should, but then it only sends back every second message.

Posted Image

I would like to know where the problem is and why it is doing it every second time i press "Enter"

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class GUI{
  // The host and port of the socket.
  private static String host = "";
  private static int port = 2000;
  //GUI Components
  private static JTextField input;
  private static JTextArea area;
  // Socket
  private static Socket socket;
  private static BufferedReader in;
  private static PrintWriter out;
  public static void main(String[]args)throws IOException
   JFrame frame = new JFrame("Client");
   frame.setLayout(new BorderLayout());
   if(args.length != 0)
    host = args[0];
    if(args.length != 1)
	 port = Integer.parseInt(args[1]);
    socket = new Socket(host, port);
    out = new PrintWriter(socket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(
   } catch (UnknownHostException e) {
		  System.err.println("Don't know about host: " + host);
	  } catch (IOException e) {
		  System.err.println("Couldn't get I/O for "
							 + "the connection to:" + host +", "+ port);
   input = new JTextField();
   input.addKeyListener(new buttonLyss());
   frame.add(input, BorderLayout.NORTH);
   area = new JTextArea();
   frame.add(area, BorderLayout.CENTER);
   frame.setSize(500, 500);
   BufferedReader stdIn = new BufferedReader
	 (new InputStreamReader(System.in));
   String userInput;
   while((userInput = stdIn.readLine()) != null)
    area.append("echo: " + in.readLine());
  public String getHost()
   return host;
  public int getPort()
   return port;
  public Socket getSocket()
   return socket;
  public void setInput(String tmp)
   area.append("User: " + tmp + "\n");
  public static class buttonLyss extends KeyAdapter
   public void keyPressed(KeyEvent ke)
    switch (ke.getKeyCode())
    case KeyEvent.VK_ENTER:
	  String tmp = input.getText();
//	  try{
//	   area.append("From server: " + in.readLine() + "\n");
//	  }catch (IOException io) {
//	   System.out.println("IO Error");
//	  }
  public static void run() throws IOException
//   try{
    area.append("From server: " + in.readLine() + "\n");
//   }catch (IOException io) {
//    System.out.println("IO Error");
//   }
   }while(in.readLine() != null);

Thanks :-)
  • 0

#2 kernelcoder


    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 04 July 2012 - 05:34 AM

I think problem is in your run method. There are two in.redLIne() and this is causing to read only second line of text. That the first readLline is adding a text to your text-area but the second line of input is wasting in checking condition for while loop.

You should change condition 'in.readLine() != null' for while loop in run method. I think you can use isInputShutdown method of Socket class or perhaps isClosed method.
  • 0

#3 wim DC

wim DC


  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 04 July 2012 - 06:22 AM

setting the variable and null-checking is usually done in one line in those cases:

String input;
while( (input=in.readLine()) != null ){

  • 0

#4 sandmaster


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 04 July 2012 - 08:30 AM

It did help to change the while loop in the run method to check so the socket is opened.

Thanks :-)
  • 0

#5 Orjan


    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 04 July 2012 - 09:18 AM

This topic has been marked as SOLVED. If you have a similar question or topic, go back to the subforum and start a new topic to continue discussions.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.

Also tagged with one or more of these keywords: Socket

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download