Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Server - Client Chat problem

network socket gui

  • Please log in to reply
8 replies to this topic

#1 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 18 August 2012 - 11:06 AM

Hello

I am having abit of a problem here and would like to see if anyone know where the error is.

I am creating a server which shall recieve messages from the client and send the message back to all connected clients.
The problem at hand is I got the recieving part and sending part sortof working, it works when there is only one client connected, but when i do connect another client to the server, and try to write a message it doesn't work- it doesn't even show up in the client, it simple disappeares.

Another problem I have is the use of a graphical interface for the server (the client works as it should when i tried on another working server)
I would like it to start up with the server but then i have no clue how to recieve messages into the area in the server.

The server
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;

public class ChatServer
{

private static Socket clientSocket = null;
private static ServerSocket serverSocket = null;
 
// static ServerGUI gui = new ServerGUI();
static Vector<ClientThread> _clients = new Vector<ClientThread>();

public static void main(String[] args)
{
  // The default port
  int listening_port = 2000;
  /** Check whether the program has any extra arguments at start
   * otherwise go with the default port
   **/
  if ( args.length < 1 )
  {
   System.out.println("Usage: java ChatServer \n");
  } else
  {
   listening_port = Integer.parseInt(args[0]);
  }
 
  try
  {
   serverSocket = new ServerSocket(listening_port);
   System.out.println("Server started using port: "+ listening_port);
  
  } catch ( IOException ioe )
  {
   System.out.println("Can't start server on port: "+ listening_port + "\nServer Terminated!");
   System.exit(-1);
  }
 
  // Always accepts connection from clients.
  // Add Clients into the vector.
  while (true)
  {
   try
   {
    clientSocket = serverSocket.accept();
    ClientThread _temp = new ClientThread(clientSocket);
    _clients.add(_temp);
    _temp.start();
//    gui.write("Client Connected\n");
    break;  
   
   } catch (IOException ioe)
   {
    System.out.println(ioe);
   }
 
  }
 
 
}
}

The GUI
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ServerGUI
{
// Declare graphical components
private JFrame frame = new JFrame("CHATT SERVER");
private JTextArea _textArea;
private JScrollPane _scrollPane;


ServerGUI()
{
  _textArea = new JTextArea();
  _textArea.setEditable(false);
  _textArea.setLineWrap(true);
  _scrollPane = new JScrollPane(_textArea);
 
 
  frame.add(_scrollPane);
 
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(500, 500);
  frame.setVisible(true);
}

public void write( String _message )
{
  _textArea.append(_message + "\n");
}

}

The ClientThread for handling threads of clients

import java.io.*;
import java.net.*;
public class ClientThread extends Thread
{
private static BufferedReader in = null;
private static PrintStream out = null;
private static Socket clientSocket = null;
ServerGUI gui = new ServerGUI();

public ClientThread( Socket _clientSocket )
{
  this.clientSocket = _clientSocket;
 
}
 
public void run()
{
  String line;
 
  try
  {
   in = new BufferedReader(new InputStreamReader(
	 clientSocket.getInputStream()));
   out = new PrintStream(clientSocket.getOutputStream());
  
   while( !isInterrupted() )
   {
    line = in.readLine();
    out.println(line);
    gui.write(line);
    out.flush();
   
   
   }
  
	  in.close();
	  out.close();
	  clientSocket.close();
  }
  catch(IOException e)
  {
   System.err.println("Error message: "+ e);
  }
 
}
}

Hopefully someone can help abit where the problem resides.
  • 0

#2 Andre

Andre

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Location:Dakar, Sénégal
  • Programming Language:C, Java, C++, PHP, JavaScript, PL/SQL, Bash, Others
  • Learning:C#, Transact-SQL

Posted 19 August 2012 - 10:29 AM

Hi Sandbox,

For your first issue, your server must be able to handle multiple processes (or threads). Every client connected would be allowed its own thread. See the Thread class.

I don't understand what your second issue is, could you be more precice?
  • 0

#3 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 19 August 2012 - 11:09 PM

so as itis now it doesnt acept more than one process ? I thought it did ,

For the second problem i mean the following:

When I start the program (server) nothing pops up but when i start the client to connect to the server, the server interface shows up aswell as the client because i have it in clientThread and not in Draw, though if i put it in draw it starts with the server put wont be able to print text into the textarea of the server. I would like to be able to open the interface when i start server and not with client.
  • 0

#4 wim DC

wim DC

    Roar

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

Posted 19 August 2012 - 11:56 PM

1) Remove the 'static' from ClientThread.

2) Create the GUI in the server (commented line), and pass it to the constructor of ClientThread so you can access the GUI there.
  • 0

#5 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 20 August 2012 - 04:25 AM

1) Remove the 'static' from ClientThread.


Where do you mean remove it? from the vector ?

And the second question i had is solved now thanks :)
  • 0

#6 wim DC

wim DC

    Roar

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

Posted 20 August 2012 - 04:27 AM

private static BufferedReader in = null;
private static PrintStream out = null;
private static Socket clientSocket = null;
  • 0

#7 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 20 August 2012 - 05:44 AM

okey done, still doesnt seem to work with multiple clients :(
  • 0

#8 wim DC

wim DC

    Roar

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

Posted 20 August 2012 - 06:15 AM

Did you remove the 'break' from the while loop in the server? Because once the first client connects, the server stops waiting for clients like that.
  • 0

#9 sandmaster

sandmaster

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 70 posts

Posted 20 August 2012 - 06:32 AM

ah there i see ofcourse =) it works so far now, but it doesnt send to all connected clients :S

Anyone know why ? :(
  • 0





Also tagged with one or more of these keywords: network, socket, gui

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