Chat Server Help
I'm developing a chat server and I am handling it with threads. I developed a executable class to handle all and I'm putting all the users in two Hashtables. There are two kinds of users. I need to know how many users do you gurus believe it can support? and how could I make it better. Thanks
Ivan
the way you do it is okay and the size of the hashtables is just limited by the space the jvm has and the kind of keys you are using.
regards
Similar Messages
-
Chat Server help... outside IP adress error
In a program i am working on, whenever i replace the ip address to bind to from InetAddress.getLocalHost(); to InetAdress.getByName(">> REAL OUTSIDE IP IN HERE<<"); and run the program i get these errors...
exception in acceptNewConnections()
exception in run()
What can i do to fix this? some code attached
private void acceptNewConnections() {
try {
SocketChannel clientChannel;
// since sSockChan is non-blocking, this will return immediately
// regardless of whether there is a connection available
while ((clientChannel = sSockChan.accept()) != null) {
addNewClient(clientChannel);
log.info("got connection from: " + clientChannel.socket().getInetAddress());
sendBroadcastMessage("login from: " + clientChannel.socket().getInetAddress(), clientChannel);
sendMessage(clientChannel, "\n\nWelcome to ChatterBox, there are " +
clients.size() + " users online.\n");
sendMessage(clientChannel, "Type 'quit' to exit.\n");
catch (IOException ioe) {
log.warn("error during accept(): ", ioe);
catch (Exception e) {
log.error("exception in acceptNewConnections()", e);
public void run() {
initServerSocket();
log.info("ChatterServer running");
running = true;
int numReady = 0;
// block while we wait for a client to connect
while (running) {
// check for new client connections
acceptNewConnections();
// check for incoming mesgs
readIncomingMessages();
// sleep a bit
try {
Thread.sleep(100);
catch (InterruptedException ie) {
}Additional Code....
private void initServerSocket() {
try {
// open a non-blocking server socket channel
sSockChan = ServerSocketChannel.open();
sSockChan.configureBlocking(false);
// bind to localhost on designated port
InetAddress addr = InetAddress.getByName("You think i'd give my ip address?");
sSockChan.socket().bind(new InetSocketAddress(addr, PORT));
// get a selector for multiplexing the client channels
readSelector = Selector.open();
catch (Exception e) {
log.error("error initializing server", e);
} -
hi, i am new to java nio. i have the following general questions:
1. how can the server know who all is connected.
2. in non blocking io mode, can we govern the multiplexing sequence? i mean can we iterate the key to a specific client. if so, how?the way you do it is okay and the size of the hashtables is just limited by the space the jvm has and the kind of keys you are using.
regards -
Need some help on JAVA CHAT SERVER
i need some info about java chat server. Please any one who have developed give me the details about the logic and process flow.
Have you read any of these?
http://search.java.sun.com/search/java/index.jsp?qp=&nh=10&qt=%2B%22chat+server%22&col=javaforums -
Trying to produce a java chat server
Would like produce a working client/server chat system, as basic as possible but able to listen and talk to each other. Any chat servers how-to examples I've come across never seem to work.
Would like to understand why applets don't work when I open the web page but do work when I view using the appletviewer. I use Internet Explorer 4 i think, java version 1.3.0_02
would you understand at least part of this error which appears when I run a chat server
exception:com.ms.security.SecurityExceptionEx[Client.<int>]:cannot access "194.81.104.26":5660
(the error is all one line no spaces)
the following code is one of the programs I've been working on and I receive the above error, appletviewer doesn't work so i think that means something is wrong with the code, client side as the server side works well, it listens etc
// Client side
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Client extends Panel implements Runnable
// Components for the visual display of the chat windows
private TextField tf = new TextField();
private TextArea ta = new TextArea();
// The socket connecting us to the server
private Socket socket;
// The streams we communicate to the server; these come
// from the socket
private DataOutputStream dout;
private DataInputStream din;
// Constructor
public Client( String host,int port ) {
// Set up the screen
setLayout( new BorderLayout() );
add( "North", tf );
add( "Center", ta );
// Receive messages when someone types a line
// and hits return, using an anonymous class as
// a callback
tf.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
processMessage( e.getActionCommand() );
// Connect to the server
try {
// Initiate the connection
socket = new Socket( host, port );
// Recieved a connection
System.out.println( "connected to "+socket );
// Create DataInput/Output streams
din = new DataInputStream( socket.getInputStream() );
dout = new DataOutputStream( socket.getOutputStream() );
// Start a background thread for receiving messages
new Thread( this ).start();
} catch( IOException ie ) { System.out.println( ie ); }
// Called when the user types something
private void processMessage( String message ) {
try {
// Send it to the server
dout.writeUTF( message );
// Clear out text input field
tf.setText( "" );
} catch( IOException ie ) { System.out.println( ie ); }
// Background thread runs this: show messages from other window
public void run() {
try {
// Receive messages one-by-one, forever
while (true) {
// Get the next message
String message = din.readUTF();
// Print it to our text window
ta.append( message+"\n" );
} catch( IOException ie ) { System.out.println( ie ); }
// ClientApplet
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class ClientApplet extends Applet
public void init() {
//String host = getParameter( "host" );
String host="194.81.104.26";
//int port = Integer.parseInt( getParameter( "port" ) );
int port =5660;
setLayout( new BorderLayout() );
add( "Center", new Client( host, port ) );
// server
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
// The ServerSocket is used for accepting new connections
private ServerSocket ss;
// A mapping from sockets to DataOutputStreams. This will
// help us avoid having to create a DataOutputStream each time
// we want to write to a stream.
private Hashtable outputStreams = new Hashtable();
// Constructor and while-accept loop all in one.
public Server( int port ) throws IOException {
// listening
listen( port );
private void listen( int port ) throws IOException {
// Create the ServerSocket
ss = new ServerSocket( port);
// ServerSocket is listening
System.out.println( "Listening on "+ss );
// Keep accepting connections forever
while (true) {
// The next incoming connection
Socket s = ss.accept();
// Connection
System.out.println( "Connection from "+s );
// Create a DataOutputStream for writing data to the
// other side
DataOutputStream dout = new DataOutputStream( s.getOutputStream() );
// Save this stream so we don't need to make it again
outputStreams.put( s, dout );
// Create a new thread for this connection, and then forget
// about it
new ServerThread( this, s );
// Get an enumeration of all the OutputStreams, one for each client
// connected to us
Enumeration getOutputStreams() {
return outputStreams.elements();
// Send a message to all clients (utility routine)
void sendToAll( String message ) {
// We synchronise on this because another thread might be
// calling removeConnection()
synchronized( outputStreams ) {
// For each client ...
for (Enumeration e = getOutputStreams(); e.hasMoreElements(); ) {
// ... get the output stream ...
DataOutputStream dout = (DataOutputStream)e.nextElement();
// ... and send the message
try {
dout.writeUTF( message );
} catch( IOException ie ) { System.out.println( ie ); }
// Remove a socket, and it's corresponding output stream, from our
// list. This is usually called by a connection thread that has
// discovered that the connectin to the client is dead.
void removeConnection( Socket s ) {
// Synchronise so sendToAll() is okay while it walks
// down the list of all output streams
synchronized( outputStreams ) {
// Removing connection
System.out.println( "Removing connection to "+s );
// Remove it from our hashtable/list
outputStreams.remove( s );
// Make sure it's closed
try {
s.close();
} catch( IOException ie ) {
System.out.println( "Error closing "+s );
ie.printStackTrace();
// Main routine
// Usage: java Server <port>
static public void main( String args[] ) throws Exception {
// Get the port # from the command line
int port = Integer.parseInt( args[0] );
//int port = 5000;
// Create a Server object, which will automatically begin
// accepting connections.
new Server( port);
// ServerThread
import java.io.*;
import java.net.*;
public class ServerThread extends Thread
// The Server that spawned
private Server server;
// The Socket connected to our client
private Socket socket;
// Constructor.
public ServerThread( Server server, Socket socket ) {
// Save the parameters
this.server = server;
this.socket = socket;
// Start up the thread
start();
// This runs in a separate thread when start() is called in the
// constructor.
public void run() {
try {
// Create a DataInputStream for communication; the client
// is using a DataOutputStream to write to us
DataInputStream din = new DataInputStream( socket.getInputStream() );
// Over and over, forever ...
while (true) {
// ... read the next message ...
String message = din.readUTF();
// ... sending printed to screen ...
System.out.println( "Sending "+message );
// ... and have the server send it to all clients
server.sendToAll( message );
} catch( EOFException ie ) {
// This doesn't need an error message
} catch( IOException ie ) {
// This needs an error message
ie.printStackTrace();
} finally {
// The connection is closed for one reason or another,
// so have the server dealing with it
server.removeConnection( socket );
<body bgcolor="#FFFFFF">
<applet code="ClientApplet" width=600 height=400>
<param name=host value="127.0.0.1">
<param name=port value="5660">
[Chat applet]
</applet>
</body>Hi!
Go to
http://www.freecode.com/projects/jchat-java2clientserverchatmodule/
probably u will het the answer..
Jove -
Thanks to the help I got from the good people of this forum today I modified the chat server program which I am making and now it is working a bit better. Right now it can accept several (up to 8) clients and store their open sockets in a SocketCollection list which can later be accessed so that messages get sent to all of them. Problem is that when I send a message, it is displayed instantaneously on the client that sent that message (my machine), but is displayed only after the other clients press enter on the other clients' machines (for testing purposes it was the same machine, different port opened for communication with server which is also on the same machine). To clarify that - all clients and server are the same machine (that shouldn't be a problem, right?). Code for printing incoming data for client program is below:
while ((fromServer = in.readLine()) != null) {
if (fromServer!=null){
System.out.println(fromServer);
System.out.println("print on client screen msg received from server");
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}Code to deliver the message to all clients from the server application is below as well:
while ((inputLine = in.readLine()) != null) {
PrintWriter multiOut;
for (int x=0; x<8; x++){
System.out.println("INSIDE MULTI-TELL FOR LOOP");
if (socketCollection[x]!=null){
Socket c=socketCollection[x];
try{
System.out.println("tried to send to all\nSocket c = " + c);
out=new PrintWriter(c.getOutputStream(), true);
out.println(c.getInetAddress() + inputLine);
catch(IOException ioe){}
}In the server's DOS window I can clearly see that it enters to display the message to multiple clients and it should send it. The sockets are all different and they point to different ports on different clients' machines. Maybe the problem is just in the client program's code. If you could help me out, it will be greatly appreciated. Thank you very much.The sockets get created one by one when each client connects. Afterwards when the thread is made, that socket gets copied into the SocketCollection array which can afterwards be accessed in order for the server to distribute the message to all its connected clients.
The for loop in the second code example where it takes SocketCollection[x] and gets its outputStream and all that is where it prints the message and sends it to each client (sends it but it is not displayed on the client side until the client sends a message to the server again). My question is how to make it so the client does not have to send a message in order to see the server message (message to all clients) that was sent before. -
Hi!
I`m trying to do a chat server in applet whith sockets. I changed a chat server aplication to a applet but this server joing the browser.
Is possible to do this? If not, how can i do a applet to talk whith another (peer-to-peer) without use socket?
thanks for any help!
Alexandre CamyUse a Servlet or JSP on the webserver to transmit messages from one peer applet to another applet
-
I am rolling out a deployment of Lync 2013 with a Standard Edition server. The Planning tool recommended that I have a separate Persistent Chat server rather than collocated it on the SE server. If I make a separate PC server, we don't have
a SQL Backend server to put the DB on since our users will be on a SE server.
My question is, can I either put the PC Database on the PC server or the SE server? Or will I have to provision a backend SQL instance just for PC?
Thanks,
BrandonThanks Raju.
I think we're going to cram it onto the SE server rather than make a whole SQL backend server just for persistent chat.
Since this is a small deployment, it recommends a standard edition server in the Planning Tool yet recommends a separate PC and SQL servers. I now plan on collocating both PC and PC DB on the SE.
I appreciate your help!
Brandon -
High availability for Lync 2013 persistent chat server and office web app server
I have 1500 users, need HA in primary data center and DR also. looking for HA and DR solution for persistent chat server and office web app server.
is below correct?
1. 2 persistent chat server in a pool of primary data center and 1 in DR. can this be reduced or any changes?
2. 2 Office web app server in a pool of primary data center and 1 in DR. can this be reduced or any changes?
also do i need HLB for both roles?1) In Lync Server 2013, there are improvements in both high availability and disaster recovery:
High availability improvements: SQL Server mirroring is used to provide high availability for the Persistent Chat Server content database and Persistent Chat compliance database within a data center (in-site).
Disaster recovery improvements: Persistent Chat Server supports a stretched pool architecture that enables a single Persistent Chat Server pool to be stretched across two sites (that is, a single logical pool in the topology, with servers in the pool physically
located across two sites). SQL Server Log Shipping is used for cross-site disaster recovery.
For more information about high availability and disaster recovery, see
Configuring Persistent Chat Server for High Availability and Disaster Recovery in the Deployment documentation.
2) for HA & DR, you can 2 Office web app server in a pool of primary data center and 1 in DR. and You will need HLB for office web app servers
http://blogs.technet.com/b/meamcs/archive/2013/03/27/office-web-apps-2013-multi-servers-nlb-installation-and-deployment-for-sharepoint-2013-step-by-step-guide.aspx
Please remember, if you see a post that helped you please click "Vote As Helpful" and if it answered your question, please click "Mark As Answer"
Mai Ali | My blog: Technical | Twitter:
Mai Ali -
hai,
i am newbie in javapgms. I need to develop simple socket programmms for my lab including chat server.
PLZ HELp.
byehai,
i am newbie in javapgms. I need to develop simple
socket programmms for my lab including chat server.
PLZ HELp.
byeHai Bon Jovi,
I liked your hits, especially the one that goes:
"Shot thru the heart, and you're to blame, you give programmers a bad name". -
VIdeo Chat Server ?!
I want to start Video Chat Server on my machines.. I have both Linux Server and Windows ... but I dont know which Java Software should I use.. Can anyone tell me>?
I want to start VIdeo Chat Portal ...Is that going to help me start Live Video Chat
Streaming Server ?...I doubt.. unless you already know Java basics and know how to use JMF, I for one tried to make a media player that streams MPG content, it was a hell of a job(didn't work as good as i wanted..) but it worked more or less.
I'm not sure if i even have the sourcefiles of it because i coded this a long time ago but if i can find it you can have the code.
The streaming part works but i'm not sure what u mean by "video chat" i assume its just a wbcam thingy? -
Lync 2010 client connecting to 2013 persistent chat server
Since we never had Group Chat in our 2010 environment, I have no idea how it works and now that we have 2013 Persistent Chat, I am struggling with getting the Lync 2010 connected. The 2013 client works just fine as it is integrated into the client. I found
out Lync 2010 needs a separate Chat client to run along side the IM client.
I installed this client but from there I have no idea how to connect it to the 2013 Chat server. I created the endpoint on the Lync 2013 server according to the article
http://technet.microsoft.com/en-us/library/jj204901.aspx but something else needs to be done. I just don't know what. Not even sure where to start looking. Many of our Lync clients have
to use 2010 because the OS is XP so I need to find out how to get these users to work with Persistent Chat.
I am not sure what the point is of creating the End point according to the article. I am sure its necessary but not sure how it fits into the clients using it to connect. What does the article mean when it says "Next, configure Persistent Chat clients
to use that SIP address as their contact object". Do you need Group Chat functioning in 2010 for this to even work?
The problem is we never had Group Chat with 2010 so I am sure there are some settings missing like DNS records etc...If you have users running legacy clients (such as Microsoft Lync 2010 these users might find the default Persistent Chat URIs difficult to work with and difficult to use when pointing their legacy client towards the pool. Because of this,
administrators need to use the New-CsPersistentChatEndpoint cmdlet to create an additional contact object for the pool, a contact object that provides a friendlier, easier-to-use URI.
Lisa Zheng
TechNet Community Support -
J2ME J2EE(servlet jsp) chat server
want to make a chat server using J2ME(fronend) and J2EE(server) . Could anybdy tell me some sample applications on the net or tutorials that can help
Darryl.Burke wrote:
Why, is Google broken?Hey you dont have to be rude to the guy ... suggest a few or dont reply -
My simple chat server does not send messages to connected clients
Hi!
I´m developing a chat server. But I can not get it work. My client seems to make a connection to it, but my server does not send the welcome message it is supposed to send when a client connects. Why not?
removedEdited by: Roxxor on Nov 24, 2008 10:36 AMOk, I solved my previous problem and now I have got a new really annoying one.
This is a broadcasting server which meand it can handle multiple clients and when one client sends a message to the server, the server should broadcast the message to all connected clients. It almost works, except that the server just sends the message back to the last connected client. The last connected client seems to steal the PrintStream() from the other clients. How can I solve that?
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.Character;
import java.io.OutputStream;
import java.util.Vector;
public class ChatServer extends MIDlet implements CommandListener, Runnable
private Display disp;
private Vector connection = new Vector();
private TextField tf_port = new TextField("Port: ", "", 32, 2);
private Form textForm = new Form("Messages");
private Form start_serverForm = new Form("Start server", new Item[] { tf_port });
private Command mExit = new Command("Exit", Command.EXIT, 0);
private Command mStart = new Command("Start", Command.SCREEN, 0);
private Command mDisconnect = new Command("Halt server", Command.EXIT, 0);
ServerSocketConnection ssc;
SocketConnection sc;
PrintStream out;
public ChatServer()
start_serverForm.addCommand(mExit);
start_serverForm.addCommand(mStart);
start_serverForm.setCommandListener(this);
tf_port.setMaxSize(5);
textForm.addCommand(mDisconnect);
textForm.setCommandListener(this);
public void startApp()
disp = Display.getDisplay(this);
disp.setCurrent(start_serverForm);
public void pauseApp()
public void destroyApp(boolean unconditional)
public void commandAction(Command c, Displayable s)
if(c == mExit)
destroyApp(false);
notifyDestroyed();
else if(c == mStart)
Thread tr = new Thread(this);
tr.start();
else if(c == mDisconnect)
try
sc.close();
catch(IOException err) { System.out.println("IOException error: " + err.getMessage()); }
destroyApp(false);
notifyDestroyed();
public void run()
try
disp.setCurrent(textForm);
ssc = (ServerSocketConnection)Connector.open("socket://:2000");
while(true)
sc = (SocketConnection) ssc.acceptAndOpen();
connection.addElement(sc);
out = new PrintStream(sc.openOutputStream());
textForm.append(sc.getAddress() + " has connected\n");
ServerToClient stc = new ServerToClient(sc);
stc.start();
catch(IOException err) { System.out.println("IOException error: " + err.getMessage()); }
catch(NullPointerException err) { System.out.println("NullPointerException error: " + err.getMessage()); }
class ServerToClient extends Thread
String message;
InputStream in;
SocketConnection sc;
ServerToClient(SocketConnection sc)
this.sc = sc;
public void run()
try
in = sc.openInputStream();
int ch;
while((ch = in.read())!= -1)
System.out.print((char)ch);
char cha = (char)ch;
String str = String.valueOf(cha);
out.print(str);
//broadcast(str);
textForm.append(str);
in.close();
//out.close();
sc.close();
catch(IOException err) { System.out.println("IOException error: " + err.getMessage()); }
} -
Force user to connect to specific Persistent Chat server
We plan to have 2 persistent chat servers in a single pool that will span 2 sites.
1 Persistent Chat server in Site A
1 Persistent Chat server in Site B
Is there a way to force users in Site A to connect to the Persistent Chat server in Site A and for users in Site B to connect to the Site B Persistent Chat server?That is possible.
For example, you have on DNS server DC01 in site A, and you only have a DNS A record point to the IP address of persistent chat server in site A, then you use DC01 as the DNS server for users in site A.
You have on DNS server DC02 in site B, and you only have a DNS A record point to the IP address of persistent chat server in site B, then you use DC02 as the DNS server for users in site B.
But it is not recommended. If one persistent server is down, users in one site won’t have group chat feature.
Lisa Zheng
TechNet Community Support
Maybe you are looking for
-
Can't print on shared printer, Alert says "On Hold (authentication required)"
Mac Mini running 10.6.8 can't print using Mac Air running 10.8.5 with HP Laserjet 1022 USB connected. Both computers on same airport network. Both with latest updates and shared printer enabled. Same problem in other direction (Air to mini if prin
-
Analysis Authorization questions
How does Analysis Authorizations work in below cases 1)Infoobject "A" set as Authorization relevant (In Bex explorer tab) 2)Infoobject "B" which is ATTRIBUTE of infooobject "A" set as Authorization relevant 3)Infoobject "A" and "B" both set as Author
-
Re: using SEND command in script
Here is part of my login script. Thanks, Larry IF "%LOGIN_NAME" = "SHIP2" THEN BEGIN #Capture q=SHIPq NFF NB ti=10 nt l=1 END SEND /A=N EXIT rem IF "%OS_VERSION" = "V7.00" THEN BEGIN rem #capture /Endcap rem END rem IF "%OS_VERSION" = "V7.00" THEN BE
-
How to fix A6000 plus reboot loop issue? - Please help
Hi Lenovo Team, Now after couple of days with sudden reboot issue. Not my phone totally useless with roboot loop. Not able to reset to factory settings. Please help me.
-
What is the grey icon with a cross and circles on my homepage? I cannot remove it
What is the grey icon with a cross and circles that has appeared on my home page? I can't remove it.