Server side programming fails!

hello. i'm developing a messenger program. initial design had one client sending a message and the server echoing the message back to the client. it worked fine. now i'm making it for multiple clients. that is one client sending a message and multiple clients receiving the message like in chat rooms. the following is my code which runs without compile time or run time errors yet does not do its job of echoing the message.

package server;

import java.io.*;

import java.net.*;

import java.lang.*;

class Server

{

ServerSocket ss =null;

Socket soc =null;

BufferedReader in =null;

PrintWriter out =null;

static Server obj =null;

publicstaticvoid main(String[] args)throws Exception

{

obj =new Server();

obj.listen();

}

publicvoid listen()throws Exception

{

ServerSocket ss =new ServerSocket(9090);

while(true)

{

Socket soc = ss.accept();

Connection c =new Connection();

Thread t =new Thread(c);

t.start();

}

}

}

package server;

import java.net.*;

import java.io.*;

import java.lang.*;

class Connectionextends Thread

{

publicvoid run()

{

String str ="";

try

{

Server.obj.in =new BufferedReader(new InputStreamReader(Server.obj.soc.getInputStream()));

Server.obj.out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(Server.obj.soc.getOutputStream())),true);

str = Server.obj.in.readLine();

while(str!=null)

{

Server.obj.out.println(str);

str = Server.obj.in.readLine();

}

Server.obj.soc.close();

}

catch(Exception e)

{

}

}

}

[3663 byte] By [kothari_neerava] at [2007-11-26 19:07:02]
# 1

the client program is as follows....

/*

* Client.java

*

* Created on February 20, 2007, 12:56 PM

*/

package client;

import java.io.*;

import java.net.*;

import java.lang.*;

/**

*

* @author Neerav

*/

public class Client extends javax.swing.JFrame {

static String str = "";

static InetAddress ip = null;

static Socket soc = null;

static PrintWriter out = null;

static BufferedReader in = null;

static Receive rc = null;

static Send sd = null;

/** Creates new form Client */

public Client() {

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jScrollPane1 = new javax.swing.JScrollPane();

jTextArea1 = new javax.swing.JTextArea();

jScrollPane2 = new javax.swing.JScrollPane();

jTextArea2 = new javax.swing.JTextArea();

jLabel2 = new javax.swing.JLabel();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Message History");

jTextArea1.setColumns(20);

jTextArea1.setEditable(false);

jTextArea1.setRows(5);

jScrollPane1.setViewportView(jTextArea1);

jTextArea2.setColumns(20);

jTextArea2.setRows(5);

jScrollPane2.setViewportView(jTextArea2);

jLabel2.setText("Your Message");

jButton1.setText("Send");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

jButton2.setText("Clear");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

}

});

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()

.addContainerGap()

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)

.add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)

.add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)

.add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2)

.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()

.add(jButton1)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 284, Short.MAX_VALUE)

.add(jButton2))

.add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1))

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(layout.createSequentialGroup()

.add(jLabel1)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)

.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 165, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)

.add(jLabel2)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)

.add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)

.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)

.add(jButton1)

.add(jButton2))

.addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);

pack();

}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

sd.send();

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

str ="";

jTextArea2.setText(str);

}

/**

* @param args the command line arguments

*/

public static void main(String args[])

{

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Client().setVisible(true);

}

});

try

{

ip = InetAddress.getByName("127.0.0.1");

soc = new Socket(ip,9090);

out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(soc.getOutputStream())),true);

in = new BufferedReader(new InputStreamReader(soc.getInputStream()));

}

catch(Exception e)

{

}

sd = new Send();

rc= new Receive();

Thread receive = new Thread(rc);

receive.start();

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JScrollPane jScrollPane2;

public static javax.swing.JTextArea jTextArea1;

public static javax.swing.JTextArea jTextArea2;

// End of variables declaration

}

ackage client;

import java.io.*;

import java.net.*;

class Send

{

public void send()

{

Client.str = Client.jTextArea2.getText();

Client.out.println(Client.str);

Client.str="";

Client.jTextArea2.setText(Client.str);

}

}

package client;

import java.io.*;

import java.net.*;

import java.lang.*;

class Receive extends Thread

{

public void run()

{

while(true)

{

try

{

Client.str = Client.in.readLine();

while(Client.str!=null)

{

Client.jTextArea1.append(Client.str);

Client.jTextArea1.append("\n");

Client.str = Client.in.readLine();

}

}

catch(Exception e)

{

}

}

}

}

kothari_neerava at 2007-7-9 20:59:00 > top of Java-index,Archived Forums,Socket Programming...
# 2

Your server looks good for one client. Is it still working with one connection?

As for many clients - you should keep socket(s) and stream(s) for each connection. In your code - second client will overwrite data and all info about first client will be lost. Same for third client - data for second client will be lost etc.

Michael.Nazarov@sun.coma at 2007-7-9 20:59:01 > top of Java-index,Archived Forums,Socket Programming...